How to Sync Sentinel Dashboard Rules to Nacos: Step‑by‑Step Guide
This article walks through modifying Sentinel Dashboard so that rule changes are automatically synchronized to Nacos, detailing six implementation steps, required pom changes, UI adjustments, custom Nacos configuration classes, rule provider/publisher beans, and final integration tweaks.
Code Implementation
Below is a step‑by‑step guide to adapt Sentinel Dashboard for Nacos synchronization, based on the original Sentinel Dashboard source and adjusted for Spring Cloud Alibaba integration.
Step 1 : Modify pom.xml to include the sentinel-datasource-nacos dependency and comment out the <scope>test</scope> section so it can be used in the main program.
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<!--<scope>test</scope>-->
</dependency>Step 2 : Update the sidebar HTML (located at resources/app/scripts/directives/sidebar/sidebar.html) to change the flow rule link.
<li ui-sref-active="active">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控规则
</a>
</li>Step 3 : Create a new nacos package under com.alibaba.csp.sentinel.dashboard and add a configuration class.
@Configuration
public class NacosConfig {
@Bean
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
return s -> JSON.parseArray(s, FlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
return ConfigFactory.createConfigService(properties);
}
}Step 4 : Implement a Nacos rule provider.
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<FlowRuleEntity>> converter;
private static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
private static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public List<FlowRuleEntity> getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}Step 5 : Implement a Nacos rule publisher.
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<FlowRuleEntity>, String> converter;
private static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
private static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
}
}Step 6 : Replace the original DynamicRuleProvider and DynamicRulePublisher beans in FlowControllerV2 with the newly created Nacos implementations using @Qualifier annotations.
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;After completing these steps, the Sentinel Dashboard will read and write flow control rules from Nacos, and you can verify the integration using the examples from the earlier "Sentinel using Nacos to store rules" article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
