在使用Sentinel我们发现,只要重新启动Sentinel的Java 客户端服务,Sentinel控制台配置的限流规则,就清空不存在了,下面介绍怎么持久化Sentinel规则
本例介绍Sentinel从Nacos配置中心读取应用的限流降级配置规则
1、搭建项目,参考:【SpringCloud】Spring Cloud Alibaba 之 Sentinel哨兵介绍入门(二十九)
2、修改POM文件,增加依赖sentinel数据nacos,如下:
1 <!-- sentinel-datasource-nacos --> 2 <dependency> 3 <groupId>com.alibaba.csp</groupId> 4 <artifactId>sentinel-datasource-nacos</artifactId> 5 </dependency>
完整pom,如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>test-springcloud</artifactId> 7 <groupId>com.test</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>springcloud-sentinel-service8401</artifactId> 13 14 <dependencies> 15 16 <!-- sentinel-datasource-nacos --> 17 <dependency> 18 <groupId>com.alibaba.csp</groupId> 19 <artifactId>sentinel-datasource-nacos</artifactId> 20 </dependency> 21 22 <!-- alibaba nacos sentinel --> 23 <dependency> 24 <groupId>com.alibaba.cloud</groupId> 25 <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> 26 </dependency> 27 28 <!-- alibaba nacos discovery --> 29 <dependency> 30 <groupId>com.alibaba.cloud</groupId> 31 <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 32 </dependency> 33 34 <!-- spring boot --> 35 <dependency> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-starter-web</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-actuator</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-devtools</artifactId> 46 <scope>runtime</scope> 47 <optional>true</optional> 48 </dependency> 49 <dependency> 50 <groupId>org.projectlombok</groupId> 51 <artifactId>lombok</artifactId> 52 <optional>true</optional> 53 </dependency> 54 <dependency> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-starter-test</artifactId> 57 <scope>test</scope> 58 </dependency> 59 60 </dependencies> 61 </project>