## 规则管理及推送 | 推送模式 | 说明 | 优点 | 缺点 | | :-: | :-- | :-- | :-- | |Push 模式|扩展读数据源(ReadableDataSource),规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。生产环境下一般采用 push 模式的数据源。|规则持久化;一致性;快速|引入第三方依赖| ![](https://img.kancloud.cn/5e/47/5e472fcf7c448eef2e7bc766862ae817_1506x876.png) ## 规则配置 ### Sentinel控制台改造 控制台改造主要是为规则实现 * DynamicRuleProvider:从Nacos上读取配置 * DynamicRulePublisher:将规则推送到Nacos上 1. pom.xml的文件修改,找到: ```java <!-- for Nacos rule publisher sample --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <scope>test</scope> </dependency> ``` 将 <scope>test</scope> 这一行注释掉,即改为如下: ```java <!-- for Nacos rule publisher sample --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <!--<scope>test</scope>--> </dependency> ``` 2. java代码的修改 找到 sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos目录,将整个目录拷贝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos 修改 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2 ,找到 ```java @Autowired @Qualifier("flowRuleDefaultProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleDefaultPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; ``` 修改为: ```java @Autowired @Qualifier("flowRuleNacosProvider") private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; @Autowired @Qualifier("flowRuleNacosPublisher") private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; ``` 3. html的修改 修改 sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html,找到: ```html <!--<li ui-sref-active="active">--> <!--<a ui-sref="dashboard.flow({app: entry.app})">--> <!--<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 V1</a>--> <!--</li>--> ``` 把注释解开,即改为: ```html <li ui-sref-active="active"> <a ui-sref="dashboard.flow({app: entry.app})"> <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 V1</a> </li> ``` >引用自 https://github.com/alibaba/Sentinel/wiki/在生产环境中使用-Sentinel