問題描述
Sentinel Dashboard中添加的規則是存儲在內存中的,只要項目一重啓規則就丟失了git
此處將規則持久化到nacos中,在nacos中添加規則,而後同步到dashboard中;github
後面研究若是將dashboard中添加的規則自動添加到nacos中web
官網教程地址:https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinelspring
實現過程
1.導入依賴包json
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.9.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.5.2</version> </dependency> </dependencies>
2.在application.properties中配置sentinel-nacos信息app
spring.application.name=mz server.port=8003 # sentinel dashboard spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.datasource.ds1.nacos.server-addr=localhost:8848 spring.cloud.sentinel.datasource.ds1.nacos.data-id=mz-sentinel spring.cloud.sentinel.datasource.ds1.nacos.group-id=DEFAULT_GROUP spring.cloud.sentinel.datasource.ds1.nacos.data-type=json spring.cloud.sentinel.datasource.ds1.nacos.rule-type=flow
3.在nacos中添加規則spring-boot
[ { "resource": "/hello", "limitApp": "default", "grade": 1, "count": 5, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]
4.建立測試類測試
@RestController public class Test { @GetMapping("/hello") public String hello() { return "hello sentinel"; } }
訪問幾回接口以後,就能夠在Sentinel Dashboard 中看到在nacos中配置的規則信息了,而且項目服務重啓依然存在spa
本文參考:http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-1/3d