Sentinel: 分布式系统流量防卫兵
一、简介
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。
二、特性
- 丰富的应用场景
- 完备的实时监控
- 广泛的开源生态
- 完善的SPI扩展机制
三、概念
-
资源名:唯一名称,默认请求路径
-
针对来源:可针对调用者进行限流,填微服务名称,默认default(不区分来源)
-
阈值类型:
-
QPS(每秒请求数): 达到阈值进行限流
-
线程数:线程数达到阈值进行限流
-
-
流控模式:
- 直接:api 达到限流规则直接限流
- 关联:当关联的资源达到阈值,限流自己
- 链路:只记录指定链路上的流量(指定资源从入口处进来的流量)【api级别针对来源】
-
流控效果:
-
快速失败:直接失败,返回异常
-
warm up:根据codeFactor(冷加载因子,默认3),从阈值/codeFactor,经过预热时长达到QPS阈值
给一个预热时长,到达预热时间后达到阈值,刚开始时阈值为最终阈值/3
-
排队等待
设置超时时间
-
四、安装
4.1 本地安装
4.2 docker 安装
version: "3.8"
services:
sentinel:
image: bladex/sentinel-dashboard:latest
container_name: sentinel
ports:
- "8858:8858"
networks:
- alibaba
networks:
alibaba:
name: spring-cloud-alibaba
五、实例
sentinel 默认是懒加载机制,只有当接口被调用时才会被 sentinel 监控
5.1 启动sentinel
5.2 模块配置
- pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
- application.yml
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
# 配置 sentinel dashboard 地址
dashboard: localhost:8858
# 默认 8719,如果被占用则自动++
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
- controller
@RestController
@RequestMapping("FlowLimit")
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "-----------testA";
}
@GetMapping("/testB")
public String testB() {
return "-----------testB";
}
@GetMapping("/testC/{id}")
public String testC(@PathVariable Integer id) {
log.info("-----------C");
int i = 10 / id;
return "-----------testC";
}
/**
* 热点数据限流
* 类似于 @HystrixCommand
* Sentinel 必须根据 hotKey 配置规则,blockHandler 用于处理热点数据异常
* 可根据情况设置参数例外项
*/
@GetMapping("/hotKey")
@SentinelResource(value = "hotKey", blockHandler = "deal_hotKey")
public String hotKey(@RequestParam(value = "p1", required = false) String p1,
@RequestParam(value = "p2", required = false) String p2) {
return "----------------hotKey";
}
// 热点数据降级方案
public String deal_hotKey(String p1, String p2, BlockException exception) {
// sentinel 默认提示 Blocked by sentinel (flow limiting)
return "----------deal_hotKey!!!";
}
}
六、持久化配置
- pom.xml
<!-- sentinel 限流规则持久化依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
- application.yml
spring:
application:
name: sentinel-openfeign-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
# 配置 sentinel dashboard 地址
dashboard: localhost:8858
# 默认 8719,如果被占用则自动++
port: 8719
datasource:
ds1:
nacos:
server-addr: localhost:8848 # nacos 配置中心地址
dataId: ${spring.application.name} # 服务名称
groupId: DEFAULT_GROUP # 分组
data-type: json # 文件类型
rule-type: flow # 规则类型
- nacos-service
// nacos 配置中心中对应的配置文件内容
[
{
"resource": "/order/get/{id}", // 资源名称
"limitApp": "default", // 来源应用
"grade": 1, // 阈值类型:0->线程数,1->QPS
"count": 1, // 单机阈值
"strategy": 0, // 流控模式:0直接,1关联,2链路
"controlBehavior": 0, // 流控效果:0快速失败,1 warm up,2排队等待
"clusterMode": false // 是否集群
}
]
七、注意
6.1 @SentinelResource 注解
该注解只管 sentinel 限流规则抛出的异常,不会处理 runtimeException
用于接口层次的限流
6.2 系统规则
系统层次的限流
- load自适应(仅对 linux/Unix有效):
- CPU usage: CPU 使用率超过某阈值触发
- 平均 RT:所有入口流量平均RT达到阈值触发
- 并发线程数:当入口流量并发线程数达到阈值触发
- 入口QPS:当入口QPS达到阈值触发