概况
官网:
https://sentinelguard.io/zh-cn/
https://github.com/alibaba/Sentinel/wiki
Sentinel官网介绍
Hystrix 与 Sentinel 对比
Hystrix:
- 需要我们直接搭建监控平台
- 没有一套web界面可以给我们更细粒度化的配置流控,速率控制,服务熔断,服务降级
Sentinel:
- 下载即可运行,不需要我们去建工程搭建
- 直接界面化的细粒度统一配置
Sentinel 下载安装
下载地址:https://github.com/alibaba/Sentinel/releases
下载完成后是一个jar包 ,使用java -jar 直接运行即可
访问地址:http://localhost:8080/
登录账户密码默认都是 sentinel
服务连接到Sentinel
添加pom依赖
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yml文件配置
sentinel:
transport:
dashboard: localhost:8080 #配置Sentinel dashboard地址
# 默认8719 ,加入端口被占用开始依次扫描,直到找到没有被占用的端口
port: 8719
## 监控端点
management:
endpoints:
web:
exposure:
include: '*'
主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudAlibabaProviderPaymentApplication {
Controller
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
return new CommonResult<Payment>(200, port, payment);
}
启动服务,查看Sentinel 页面没有任何服务
访问服务:
再次查看Sentinel 页面:
查看Sentinel 页面,可以看到,已经出现了我们刚才访问的微服务,Sentinel 采用的懒加载机制
,只有我们访问过了,Sentinel 页面才会出现注册在其上的微服务列表
Sentinel流控规则 (流量控制)
-
Sentinel流控规则可以通过Sentinel的图形界面来对需要流控的服务来设置,
-
可以对对应的服务的具体的一个api进行设置,如下图我们可以对
springcloud-alibaba-provider-payment 这个服务的 这个/payment/get/1 这个api来设置流控规则
QPS 直接失败
如下图:表示 访问/payment/getPaymentd 每秒只能有一个请求访问
如果在1秒内有多个请求,那么别的请求会报如下信息:
QPS 线程直接失败
配置如下图:
该配置表示:表示调用该api的线程数达到1的 时候就进行限流
QPS 流控模式 关联
当关联的资源达到阈值时,就限流直接
当与A关联的资源B达到阀值后,就限流A自己
如下图
QPS 流控效果 Warm Up (预热)
公式:设定阈值除以coldFactor(冷加载因子默认是3),经过设置预热时长后才会到达设定阀值
即QPS请求 阈值 / 3 开始,经过 设定预热时长 逐渐升至设定的 QPS 阈值
如下设置:
表示阈值 QPS 从10/3 = 3 开始 ,经过5秒中的预热时长,才会逐渐升到阈值为10
流控 排队等待
官网解释:
如下图配置:
Sentinel 服务降级
基本介绍
下面是官网介绍:
进一步说明:
降级策略
RT
如下配置表示:
请求的平均响应时间 大于200毫秒 且 在1秒内的时间窗口期内的请求数>=5,这2个条件同时满足,触发降级(断路器打开,服务不可用),窗口期过后,服务恢复正常
异常比例
异常数
热点规则
代码示例如下:@SentinelResource(value = "testHotKey", blockHandler = "fallbackMethod")
该注解表示:被@SentinelResource标注的api 会被sentinel 监控
@GetMapping("/payment/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "fallbackMethod")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return "------------------hot key";
}
public String fallbackMethod(String p1, String p2, BlockException exception) {
return "-----fallbackMethod";
}
下面我们来陪 热点规则如下图:
- 资源名:对应上面代码的注解@SentinelResource value的值
- 参数索引:api的第一个参数p1
- 单击阈值:表示QPS的阈值
下面配置表示 testHotKey 这个资源 的第一个参数 的每秒只能访问一次,如果超过就用兜底方法fallbackMethod
参数例外项
以上是配合上面的参数索引,当这个参数的值是某个我们设定的值时,我们可以另外对待
如下图:
表示参数索引为0的参数,在参数值是5的时候,它的QPS是200
@SentinelResource
按资源名称限流,资源名称就是注解@SentinelResource 配置的value的值
参数fallbackMethod 表示 如果该api的访问超过我们设定的阈值,那么把fallbackMethod 这个方法坐位兜底方案
自定义全局 blockHandler
方法注解配置:
@SentinelResource(value = “testHotKey”, blockHandlerClass = MyBlockHandler.class,blockHandler = “handlerException01”)
@GetMapping("/payment/testHotKey")
@SentinelResource(value = "testHotKey", blockHandlerClass = MyBlockHandler.class,blockHandler = "handlerException01")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return "------------------hot key";
}
上述配置表示 如果该api 超过了 sentinel 设定的阈值,那么久会执行blockHandlerClass 配置的类的 blockHandler 配置的方法
异常处理 fallback
使用@SentinelResource 注解的 fallbackClass 和 defaultFallback 或者
fallback 来指定 当该api 出现异常的时候有兜底方法
配置可以参考如下:
@GetMapping("/payment/testHotKey")
@SentinelResource(value = "testHotKey",
blockHandlerClass = MyBlockHandler.class,blockHandler = "handlerException01",
fallbackClass = MyBlockHandler.class,defaultFallback = "handlerException01")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return "------------------hot key";
}
Sentinel 服务熔断
注解@SentinelResource 有2个参数
- fallback:管运行异常
- blockHandler:管配置违规(比如QPS超过)
fallback 配置:
@GetMapping("/order/payment/{id}")
@SentinelResource(fallback = "fallback")
public CommonResult<Payment> paymentByOpenFeign (@PathVariable("id") Long id) {
int i = 10/0;
CommonResult forObject = restTemplate.getForObject(PAYMENT_URI + "/payment/get/" + id, CommonResult.class);
return forObject;
}
public CommonResult<Payment> fallback(Long id) {
return new CommonResult<>(444, "运行异常",null);
}
api 调用结果,会返回兜底方法 ballfack,不会直接返回错误页面
配合 OpenFeign
添加依赖
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
yml文件
feign:
sentinel:
enabled: true
主启动类:@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class SpringcloudAlibabaConsumerOrderApplication {
service
注意 @FeignClient 的2个参数,参考注解
@Component
@Component
// 参数1:要调用的微服务名,参数二:调用异常超时的兜底类,
@FeignClient(value = "springcloud-alibaba-provider-payment", fallback = MyFallback.class)
public interface OpenFeignPaymentServer {
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
@GetMapping("/payment/timeout/get/{id}")
public CommonResult<Payment> getPaymenttimeoutById(@PathVariable("id") Long id);
}
兜底类
@Component
public class MyFallback implements OpenFeignPaymentServer {
@Override
public CommonResult<Payment> getPaymentById(Long id) {
return new CommonResult<>(444,"getPaymentById调用出现异常",null);
}
@Override
public CommonResult<Payment> getPaymenttimeoutById(Long id) {
return new CommonResult<>(444,"getPaymenttimeoutById调用出现异常",null);
}
}
故意设置 服务提供者api 异常或者关闭服务提供者,
调用结果:
Sentinel 持久化
面临问题
当我们重启服务后,我们配置在Sentinel的规则就会消失,所以生成环境需要将配置规则进行持久化
解决方案
将限流规则持久化进Nacos保存
配置步骤
- 给需要sentinel配置持久化的服务添加pom依赖
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
- yml配置文件添加Nacos数据源配置
sentinel:
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
- Nacos上添加业务规则配置
该步骤需要登录Nacos图形化页面添加配置
如下图:
参数名称解释: