现在,sentinel 的所有规则都是内存存储,重启后所有规则都会丢失。在生产环境下,我们必须确保这些规则的持久化,避免丢失。
规则管理模式
规则是否能持久化,取决于规则管理模式,Sentinel 支持三种规则管理模式:
- 原始模式:Sentinel 的默认模式,将规则保存在内存,重启服务会丢失。
- pull 模式
- push 模式
pull 模式
pull 模式:控制台将配置的规则推送到 Sentinel 客户端,而客户端会将配置规则保存在本地文件或数据库中。以后会定时去本地文件或数据库中查询,更新本地规则。

存在时效性问题,客户端之间规则不统一
push 模式
push 模式:控制台将配置规则推送到远程配置中心,例如 Nacos。Sentinel 客户端监听 Nacos,获取配置变更的推送消息,完成本地配置更新。

实现 push 模式
修改 order-service 服务
引入依赖,在 order-service 中引入 sentinel 监听 nacos 的依赖:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency>
配置 nacos 地址,在 order-service 中的 application.yml 文件配置 nacos 地址及监听的配置信息:
spring:
cloud:
sentinel:
datasource:
flow:
nacos:
server-addr: halo:8848 # nacos地址
dataId: orderservice-flow-rules
groupId: SENTINEL_GROUP
rule-type: flow # 还可以是:degrade、authority、param-flow
修改 sentinel-dashboard 源码
修改完成后,重新打包运行
java -jar -Dnacos.addr=halo:8848 -DServer.port=8090 sentinel-dashboard.jar
