前言
- 前面的例子雖然實現了可視化動態流控規則配置,即在
sentinel-dashboard
配置並實時監控。
- 但是這些規則沒有持久化,而是存儲在內存中,並且
sentinel-dashboard
配置的規則也是通過接口寫入到客戶端應用內存的,所以客戶端重啟后規則就沒了。
- 由於所在項目主使用阿里系技術(
springcloud-alibaba
),並且重度使用 nacos
,所以本篇主要結合官方文檔研究規則持久化到 nacos
的方式。
新建 cloud-alibaba-sentinel 工程,連接 sentinel-dashboard
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>${springcloud-alibaba.version}</version>
</dependency>
server:
port: 7772
spring:
application:
name: sentinel-cloud-nacos1
cloud:
sentinel:
transport:
dashboard: localhost:8080
@RestController
public class TestController {
@GetMapping("/hello")
public Object hello(@RequestParam(required = false, defaultValue = "7") Integer i) {
return LocalDateTime.now() + " hello " + i;
}
}
- 啟動sentinel-dashboard和本工程
- 密集訪問 /hello 驗證dashboard,圖↓

- 可以看到
@RequestMapping
接口已被監控
引入 Nacos
<!--nacos的配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${springcloud-alibaba.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
server:
port: 7772
spring:
application:
name: sentinel-cloud-nacos1
cloud:
sentinel:
transport:
dashboard: localhost:8080
nacos:
config:
# 配置中心的地址
server-addr: localhost:8848
#默認為Public命名空間,可以省略不寫
#namespace: Public #命名空間,用於區分環境
#指定配置群組 --如果是Public命名空間 則可以省略群組配置
#group: DEFAULT_GROUP
#文件名 -- 如果沒有配置則默認為 ${spring.appliction.name}
prefix: ${spring.application.name}
# 指定配置中心中配置文件的格式
file-extension: yml
# discovery:
# #指定注冊中心的地址,如果你不需要注冊該服務,也可以去掉該項,並刪除discovery依賴
# server-addr: localhost:8848
# #默認為Public命名空間,可以省略不寫
# namespace: Public #命名空間,用於區分環境
- 啟動本地Nacos服務,添加簡單的yml配置
- 編寫Properties對象,映射Nacos配置,並進行訪問測試驗證
@ConfigurationProperties("app")
@Component
public class AppProperties {
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "AppProperties [author=" + author + "]";
}
}
@Autowired
private AppProperties appProps;
@GetMapping("/appProps")
public Object appProps() {
return appProps.toString();
}
DataSource拓展的推拉模式
- 官方文檔頁
- 拉(pull)模式,就是把內存中的規則持久化到文件/Naocs/Zookeeper等,應用啟動加載規則,並定期輪詢拉取規則。
- 推(push)模式,Dashboard可視化編輯規則到數據源(推送更新),應用監聽數據源規則變動,實時更新流控規則。
- 很明顯,官方推薦推模式。“我們推薦通過動態配置源的控制台來進行規則寫入和推送,而不是通過 Sentinel 客戶端直接寫入到動態配置源中。”

改造dashboard推送配置到Nacos
<!-- for Nacos rule publisher sample -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!-- <scope>test</scope> -->
</dependency>
- 修改流控部分源碼(細節參見文末鏈接)
- Nacos添加流控配置文件(JSON),圖示↓

- 啟動dashboard,並配置一個流控規則,保存查看Nacos對應文件,圖示↓


客戶端服務監聽Nacos規則變化
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>${sentinel.version}</version>
</dependency>
@Component
public class FlowRuleNacosDataSourceConfig {
@Value("${spring.cloud.nacos.config.server-addr}")
private String sentinelNacosServerAddr;
@Value("${spring.cloud.nacos.config.namespace}")
private String sentinelNacosNamspace;
@Value("${spring.application.name}")
private String springApplicationName;
@PostConstruct
private void init() {
// remoteAddress is the address of Nacos
// groupId and dataId are concepts of Nacos
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(sentinelNacosServerAddr, sentinelNacosNamspace, "SENTINEL_GROUP", springApplicationName+"-flow-rules",
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
}
}
- 啟動服務,dashboard推送配置,客戶端服務監聽規則變化 ↓
2021-11-15 18:06:59.663 INFO 19996 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8848-st1] [subscribe] sentinel-cloud-nacos1-flow-rules+SENTINEL_GROUP+st1
2021-11-15 18:06:59.665 INFO 19996 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848-st1] [add-listener] ok, tenant=st1, dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, cnt=1
2021-11-15 18:06:59.671 INFO 19996 --- [alhost_8848-st1] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8848-st1] [polling-resp] config changed. dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, tenant=st1
。。。。。。
2021-11-15 18:10:07.833 INFO 19996 --- [alhost_8848-st1] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8848-st1] [polling-resp] config changed. dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, tenant=st1
2021-11-15 18:10:07.836 INFO 19996 --- [alhost_8848-st1] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8848-st1] [data-received] dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, tenant=st1, md5=2c83f9e67d81745ea45a24dd1a7d0c63, content=[{"app":"sentinel-cloud-nacos1","clusterConfig":{"fallbackToLocalWhenFail":true,"sampleCount":10,"st...
2021-11-15 18:10:07.836 INFO 19996 --- [alhost_8848-st1] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848-st1] [notify-listener] time cost=0ms in ClientWorker, dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, md5=2c83f9e67d81745ea45a24dd1a7d0c63, listener=cn.richard.demo.sentinel.cloudnacos1.NacosDataSource$1@24312219
2021-11-15 18:10:07.837 INFO 19996 --- [update-thread-1] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848-st1] [notify-ok] dataId=sentinel-cloud-nacos1-flow-rules, group=SENTINEL_GROUP, md5=2c83f9e67d81745ea45a24dd1a7d0c63, listener=cn.richard.demo.sentinel.cloudnacos1.NacosDataSource$1@24312219
其他注意點
- “注:一般推薦將 @SentinelResource 注解加到服務實現上,而在 Web 層直接使用 Spring Cloud Alibaba 自帶的 Web 埋點適配。”
有效參考