sentinel 热点限流

官网:https://sentinelguard.io/zh-cn/docs/parameter-flow-control.html

热点限流

sentinel使用lru算法统计最近最常使用的热点参数,结合令牌桶算法进行限流

# 热点数据限流:统计热点参数,根据配置的限流阈值与模式,对包含热点参数的资源进行限流
参数为商品id,统计一段时间内最常购买的商品ID并进行限制
参数为用户id,针对一段时间内频繁访问的用户ID进行限制

***********

基本使用

相关依赖

        <!-- spring-cloud-alibaba-starter-sentinel中有该依赖 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>x.y.z</version></dependency>

ParamFlowRule

public class ParamFlowRule extends AbstractRule {private int grade = 1;              //限流模式:QPS(默认)、并发线程数private Integer paramIdx;           //热点参数索引private double count;               //限流阈值private int controlBehavior = 0;    //流控行为:快速失败(默认)、匀速排队模式private int maxQueueingTimeMs = 0;  //匀速排队模式时排队等待时间private int burstCount = 0;         //处理突发流量private long durationInSec = 1L;    //限流窗口时长,默认1sprivate List<ParamFlowItem> paramFlowItemList = new ArrayList();//针对指定的参数值单独设置限流阈值,不受count影响//仅支持基本类型和字符串类型private Map<Object, Integer> hotItems = new HashMap();private boolean clusterMode = false;  //是否为集群模式,集群模式传入的参数只支持基本类型和字符串类型private ParamFlowClusterConfig clusterConfig;  //集群限流配置public ParamFlowRule() {}public ParamFlowRule(String resourceName) {this.setResource(resourceName);}

AbstractRule

public abstract class AbstractRule implements Rule {private String resource;    //需要限流的资源名称private String limitApp;    //特定资源、default(不限资源)、otherpublic AbstractRule() {}

ParamFlowClusterConfig

public class ParamFlowClusterConfig {private Long flowId;                    //热点参数集群流控id,需保证全局唯一private int thresholdType = 0;          //阀值模式:0(单机均摊)、1(全局模式)private boolean fallbackToLocalWhenFail = false;  //client连接失败或通信失败时,是否退化到本地的限流模式private int sampleCount = 10;           //窗口数量private int windowIntervalMs = 1000;    //窗口统计时长,默认1spublic ParamFlowClusterConfig() {}

设置热点参数规则

    private static void initParamFlowRules() {// QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY).setParamIdx(0).setGrade(RuleConstant.FLOW_GRADE_QPS)//.setDurationInSec(3)//.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)//.setMaxQueueingTimeMs(600).setCount(5);// We can set threshold count for specific parameter value individually.// Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)// in index 0 will be 10, rather than the global threshold (5).ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B)).setClassType(int.class.getName()).setCount(10);rule.setParamFlowItemList(Collections.singletonList(item));ParamFlowRuleManager.loadRules(Collections.singletonList(rule));}

使用示例

***********

相关依赖

        <!-- 服务注册与发现 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- 服务调用 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><!-- sentinel dubbo适配器 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo-adapter</artifactId><version>1.8.0</version></dependency><!-- dubbo应用向控制台传输数据 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.0</version></dependency><!-- 热点限流 --><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-parameter-flow-control</artifactId><version>1.8.0</version></dependency>

***********

服务端

application.yml

spring:application:name: nacos-providercloud:nacos:discovery:server-addr: localhost:8848dubbo:protocol:name: dubboport: -1

HelloService

public interface HelloService {String hello(String name);
}

HelloServiceImpl

@DubboService
public class HelloServiceImpl implements HelloService {@Overridepublic String hello(String name) {return "hello " + name;}
}

           

DemoApplication

@EnableDubbo
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

***********

消费端

application.yml

spring:application:name: nacos-consuemrcloud:nacos:discovery:server-addr: localhost:8848dubbo:protocol:name: dubboport: -1server:port: 8081

HelloService

public interface HelloService {String hello(String name);
}

HelloController

@RestController
public class HelloController {@DubboReferenceprivate HelloService helloService;@RequestMapping("/hello")public String hello(){System.out.println(helloService.hello("瓜田李下"));return "success";}
}

***********

本地限流配置

CustomParamFlowRule

public class CustomParamFlowRule implements InitFunc {@Overridepublic void init() throws Exception {ParamFlowRule paramFlowRule = new ParamFlowRule("com.example.demo.service.HelloService:hello(java.lang.String)");paramFlowRule.setParamIdx(0).setGrade(RuleConstant.FLOW_GRADE_QPS).setCount(2);ParamFlowItem paramFlowItem = new ParamFlowItem();paramFlowItem.setObject("瓜田李下")    //name=瓜田李下的限流阀值是4//name为其他值的限流阀值是2.setClassType(String.class.getName()).setCount(4);paramFlowRule.setParamFlowItemList(Collections.singletonList(paramFlowItem));ParamFlowRuleManager.loadRules(Collections.singletonList(paramFlowRule));}
}

META-INF/services/com.alibaba.csp.sentinel.init.InitFunc

com.example.demo.config.CustomParamFlowRule

jmeter测试

              

              

              

              

***********

控制台限流配置

控制台启动

java -Dserver.port=8000 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar参数说明:
-Dserver.port=8080:指定控制台启动端口
-Dcsp.sentinel.dashboard.server:指定控制台地址和端口
-Dproject.name=sentinel-dashboard:指定控制台项目名称

服务端启动

-Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=nacos-provider

消费端启动

-Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8000 -Dproject.name=nacos-consumer

sentinel 控制台:localhost:8000

              

              

jmeter 测试

sentinel 热点限流相关推荐

  1. Sentinel系列之热点限流

    热点数据:就是那些经常被访问或者经常出现的数据,所以针对这种数据就会进行限流,比如针对刷单,同一个账号或者同一个ip进来的用户进行限流等等 Sentinel提供了热点参数限流的策略,其实就是一种特殊的 ...

  2. Sentinel之限流、降级、系统保护、热点、授权规则

    简介 Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度来帮助用户保护服务的稳定性. 本文主要讲限 ...

  3. java 限流熔断_SpringCloud-Alibaba-Sentinel服务降级,热点限流,服务熔断

    前言:除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.一个服务常常会调用别的模块,可能是另外的一个远程服务.数据库,或者第三方 api 等.例如,支付的时候,可能需 ...

  4. SpringCloud 第八期 Sentinel 熔断限流

    sentinel可以作为监控平台使用,下载jar包运行 官网说明文档,有中文 Wiki - Gitee.com sentinel下载地址 https://github.com/alibaba/Sent ...

  5. sentinel 网关限流

    sentinel 网关限流 官网:https://sentinelguard.io/zh-cn/docs/api-gateway-flow-control.html 网关限流 sentinel支持对主 ...

  6. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    Spring Cloud Alibaba | Sentinel: 服务限流高级篇 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明 ...

  7. 流量治理神器-Sentinel的限流模式,选单机还是集群?

    大家好,架构摆渡人.这是我的第5篇原创文章,还请多多支持. 上篇文章给大家推荐了一些限流的框架,如果说硬要我推荐一款,我会推荐Sentinel,Sentinel的限流模式分为两种,分别是单机模式和集群 ...

  8. Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!

    前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...

  9. 【alibaba-cloud】网关整合sentinel实现限流

    网关整合sentinel实现限流 还是在前几篇博客的基础上搭建的,需要有服务端,客户端,网关等 客户端服务端的搭建:https://blog.csdn.net/wangyunzhao007/artic ...

最新文章

  1. 蚂蚁金服开源增强版Spring Boot 的研发框架!
  2. 商业航天:通往太空旅程的新门票
  3. Response.Write详细介绍
  4. select点击option获取文本输入框的焦点事件
  5. 从0到1打造企业数字化运营闭环白皮书
  6. LeetCode 70 爬楼梯
  7. Eclipse导入Android Project遇到的问题
  8. HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容
  9. windows下python3用pip安装kivy和kivy示例
  10. 串的复制——串传送指令MOVSB和方向标志位DF和CLD和REP
  11. plc 上位机编译算法_基于西门子PLC的Socket通信深度剖析
  12. H5基础阶段二(表格、表单)
  13. 500G JAVA视频网盘分享 (JEECG开源社区)
  14. VIVADO 下载mcs 文件
  15. Markdown 常用数学符号和公式
  16. 计算机密码的怎么查询,电脑密码如何查看? 手把手教你查看方法
  17. java web 使用 Freemarker 导出word,zip包导出多个word
  18. 良好的代码习惯(一)
  19. 图数据库 Dgraph 学习笔记
  20. WebGL 实践篇(三)—— 二维图形的平移、旋转、缩放

热门文章

  1. 负面信息处理的有效途径有哪些?负面信息对企业的影响
  2. 计算机组装期末学生总结,期末教学总结
  3. 词云分析——西游记词云分析函数代码及分析
  4. Android 设置横屏,竖屏或者禁止横屏的方法
  5. HTML中clospan不起作用,HTML colspan和rowspan不起作用
  6. 办公室职位职务词汇2
  7. Word 插入η、θ、α等数学公式中的希腊字母
  8. loginshell nologinshell
  9. 架设某大型网站服务器全部详细过程(郁闷少年)
  10. Jemer安装问题总结及实例