SpringCloudAlibaba-Sentinel
一、介绍
官网:https://github.com/alibaba/Sentinel/
下载jar包,启动,访问http://localhost:8080/
创建module添加如下依赖
<!--SpringCloud ailibaba sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
添加配置
server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: localhost:8848 #Nacos服务注册中心地址sentinel:transport:dashboard: localhost:8080 #配置Sentinel dashboard地址port: 8719management:endpoints:web:exposure:include: '*'

二、流控规则
1、直接失败(默认)
QPS:表示每秒只能有一个访问

线程数:表示只能有以一个线程

2、关联
当关联的资源达到阈值时,限流自己

流控效果
Warm up
阈值/coldFactor(默认值为3),经过预热时长后才会达到阈值

排队等待
匀速排队,让请求以均匀的速度通过,阈值类型必须是QPS,

三、降级规则
RT:平均响应时间(秒级)超出阈值且在时间窗口通过的请求>=5,两个条件同时满足后触发降级
窗口期过后关闭断路器
最大4900(更大需要-Dcsp.sentinel.statistic.max.rt=xxx才能生效)

异常比例:QPS>=5(秒级)超过阈值时,触发降级,时间窗口结束后,关闭降级

异常数:(分钟统计)超过阈值时触发降级,时间窗口结束后关闭降级

四、热点key
热点参数限流会统计传入参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用进行限流。
测试代码
@GetMapping("/testHotKey")@SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")public String testHotKey(@RequestParam(value = "p1",required = false) String p1,@RequestParam(value = "p2",required = false) String p2){//int age = 10/0;return "------testHotKey";}public String deal_testHotKey (String p1, String p2, BlockException exception){return "------deal_testHotKey,o(╥﹏╥)o"; //sentinel系统默认的提示:Blocked by Sentinel (flow limiting)}
包括参数例外项配置
