一旦重啟應用,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能獲取流控規則。