一旦重启应用,Sentinel规则将消失,生产环境需要将配置规则进行持久化。
推模式架构图
原理简述:
- 控制台推送规则:
- 将规则推送到Nacos或其他远程配置中心
- Sentinel客户端连接Nacos,获取规则配置;并监听Nacos配置变化,如发生变化,就更新本地缓存(从而让本地缓存总是和Nacos一致)
- 控制台监听Nacos配置变化,如发生变化就更新本地缓存(从而让控制台本地缓存总是和Nacos一致)
微服务改造
1,添加依赖
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
2,添加YML配置
spring: cloud: sentinel: datasource: # 名称随意 flow: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-flow-rules groupId: SENTINEL_GROUP # 规则类型,取值见: # org.springframework.cloud.alibaba.sentinel.datasource.RuleType rule-type: flow degrade: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-degrade-rules groupId: SENTINEL_GROUP rule-type: degrade system: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-system-rules groupId: SENTINEL_GROUP rule-type: system authority: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-authority-rules groupId: SENTINEL_GROUP rule-type: authority param-flow: nacos: server-addr: localhost:8848 dataId: ${spring.application.name}-param-flow-rules groupId: SENTINEL_GROUP rule-type: param-flow
3,在Nacos里面配置sentiel相关的流控规则。
系统演示:
1,POM文件
<dependencies> <!--nacos客户端 服务注册--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--sentinel 流量监控,服务熔断,服务降级等--> <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.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--web 监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
2,YML文件配置
server:
port: 6001
spring:
application:
name: sentienel-dashboard-service
cloud:
nacos:
discovery:
#nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#sentinel dashboard 地址
dashboard: localhost:8080
#默认8179端口
port: 8179
datasource: ds: nacos: server-addr: localhost:8848 dataId: sentienel-dashboard-service groupId: DEFAULT_GROUP data-type: json rule-type: flow
#监控暴露
management:
endpoints:
web:
exposure:
include: "*"
3,主启动类
@SpringBootApplication @EnableDiscoveryClient public class SentinelDashboard6001Main { public static void main(String[] args) { SpringApplication.run(SentinelDashboard6001Main.class,args); } }
4,业务类
@RestController public class SentinelController { @GetMapping("/testA") public String getA() { return "test A"; } @GetMapping("/testB") public String getB() { return "test B"; } @GetMapping("/testHot") @SentinelResource(value = "/hot",blockHandler = "hotHandler") public String testHot(@RequestParam(name = "a", required = false) String a, @RequestParam(name = "b", required = false) String b) { return a+" "+b; } public String hotHandler(String a, String b, BlockException exception){ return "hot key exception"; } }
5,在Nacos上配置Sentinel的流控规则
- resource:资源名称
- limitApp:来源应用
- grade:阀值类型,0表示线程数,1表示QPS
- count:单机阈值
- strategy:流控模式,0表示直接,1表示关联,2表示链路
- controllerBehavior:流控效果,0表示快速失败,1表示Warm up,2表示排队等待
- clusterMode:是否集群
6,测试,发现Sentinel能获取流控规则。