一、介绍
官网: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: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard地址
port: 8719
management:
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)
}
包括参数例外项配置
五、系统规则
1、系统自适应限流
Sentinel系统自适应限流从整体维度对应用入口流量进行控制,结合应用的load,CPU使用率、总体平均RT、入口QPS和并发线程数等几个维度的监控指标、通过自适应的留空策略,让系统的入口流量和系统的负载达到一个平衡,让系统尽可能跑在最大吞吐量的同时保证系统的稳定性
Load自适应(仅对Linux/Unix机器生效):系统的load1作为启发指标,进行自适应系统保护。当系统load1超过设定的启发值,且系统当前的并发线程数超过估算的系统容量时才会触发系统保护。系统容量由,maxQps*minRt估算得出,设定参考值一般是CUP cores*2.5
CPU 使用率:当系统CPU使用率超过阈值即触发系统保护(取值范围0.0-1.0),比较灵敏。
平均RT:当单台机器上所有入口流量的平均RT达到阈值即出发系统保护,单位是毫秒。
并发线程数:当单台机器上所有入口流量的并发线程数达到阈值即出发系统保护。
入口QPS:当单台机器上所有入口流量的QPS达到阈值即触发系统保护。
六、@SentinelResource
业务示例代码
如果没有自定义降级方法,则会使用系统默认的
@RestController
public class RateLimitController
{
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandler = "handleException")
public CommonResult byResource()
{
return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));
}
public CommonResult handleException(BlockException exception)
{
return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");
}
@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl()
{
return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
}
}
自定义限流
业务示例代码,这里需要注意需要根据value来配置规则,而不是路径
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException2")
public CommonResult customerBlockHandler()
{
return new CommonResult(200,"按客戶自定义",new Payment(2020L,"serial003"));
}
public class CustomerBlockHandler
{
public static CommonResult handlerException(BlockException exception)
{
return new CommonResult(4444,"按客戶自定义,global handlerException----1");
}
public static CommonResult handlerException2(BlockException exception)
{
return new CommonResult(4444,"按客戶自定义,global handlerException----2");
}
}
七、服务降级
示例代码
@RequestMapping("/consumer/fallback/{id}")
// 当fallback 和 blockHandler 都进行了配置,被限流降级而抛出BlockException时只会进入blockHandler处理逻辑
//@SentinelResource(value = "fallback") //没有配置
//@SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback只负责业务异常
//@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规
@SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler",
exceptionsToIgnore = {IllegalArgumentException.class})
public CommonResult<Payment> fallback(@PathVariable Long id)
{
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
}else if (result.getData() == null) {
throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
}
return result;
}
//本例是fallback
public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>(444,"兜底异常handlerFallback,exception内容 "+e.getMessage(),payment);
}
//本例是blockHandler
public CommonResult blockHandler(@PathVariable Long id,BlockException blockException) {
Payment payment = new Payment(id,"null");
return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment);
}
八、openFeign服务降级
开启配置即可,其他参考前面文章
# 激活Sentinel对Feign的支持
feign:
sentinel:
enabled: true
九、配置规则持久化
将限流规则持久化进Nacos保存,只要刷新某个Rest地址,sentinel控制台的流控规则就能看到,只要Nacos里面的配置不删除,针对sentinel上的流控规则就持续有效。
添加依赖
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
添加配置datasource部分
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
sentinel:
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
添加Nacos业务规则配置
"resource":"/rateLimit/byUrl", 资源名称
"limitApp":"default", 来源应用
"grade":1, 阈值类型 0 表示线程数 1表示QPS
"count":1, 单机阈值
"strategy":0, 流控模式 0表示直接,1表示关联,2表示链路
"controlBehavior":0, 流控效果,0表示快速失败,1表示Warm up 2表示排队等待
"clusterMode":false 是否集群
1249

被折叠的 条评论
为什么被折叠?



