Skip to content

Conversation

Serein-K
Copy link

What changes were made?

  • 新增 Nacos 持久化规则配置支持

Why are these changes needed?

  • 当前 Sentinel Dashboard 无法保存用户配置的规则,重启sentinel之后又得重新配置,于是想到了关于用nacos持久化的问题,

How to verify these changes?

  1. 先在nacos中创建一个命名空间 sentinel-namespace
  2. 将Sentinel Dashboard服务注册到了nacos中 ,在服务列表可以看到这个服务名 sentinel-dashboard-service
  3. 在Sentinel Dashboard增加配置的时候,nacos会在命名空间 sentinel-namespace的配置列表看到相应的配置,比如说,对应Sentinel Dashboard来说注册上来的服务名叫做hello,然后再Sentinel Dashboard中新添了流控规则,则在nacos中就会有这样的文件名hello-flow-rules,里面有数据
  4. 把Sentinel Dashboard服务关了,再重启,发现配置文件没有丢失(其实就是在控制层的时候,会去读一个nacos中相应的配置文件,把数据渲染上去,之后增删改操作都会去nacos中改相应的内容)
配置 服务列表 配置列表

@CLAassistant
Copy link

CLAassistant commented Jul 21, 2025

CLA assistant check
All committers have signed the CLA.

@mikePenW
Copy link

官方是不是不维护了啊?

@LearningGp LearningGp requested a review from Copilot August 7, 2025 06:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds Nacos-based rule persistence support to the Sentinel Dashboard, addressing the issue where rules are lost after dashboard restarts. The implementation includes both rule providers and publishers for various rule types, along with configuration for Nacos service discovery.

  • Integration of Nacos as a persistence layer for Sentinel rules
  • Support for multiple rule types (flow, degrade, authority, system, param flow, cluster flow)
  • Modified controllers to use Nacos providers/publishers instead of direct client communication
  • Added service discovery registration to Nacos

Reviewed Changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
pom.xml Added Spring Cloud and Nacos dependencies
application.properties Added Nacos discovery configuration
DashboardApplication.java Enabled discovery client for Nacos registration
NacosConfig.java Configuration bean for Nacos ConfigService
*RuleNacosProvider.java Rule providers for reading from Nacos
*RuleNacosPublisher.java Rule publishers for writing to Nacos
Various controllers Modified to use Nacos providers/publishers and import optimizations
Comments suppressed due to low confidence (1)

sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java:158

  • The publishRulesToNacos call should be wrapped in a try-catch block to handle exceptions properly and avoid breaking the rule delete operation.
            publishRulesToNacos(oldEntity.getApp());

public void init() throws Exception {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddr);
if (namespace != null && !namespace.isEmpty()) {
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Use StringUtil.isNotBlank() for consistency with other parts of the codebase instead of null check and isEmpty().

Suggested change
if (namespace != null && !namespace.isEmpty()) {
if (StringUtils.isNotBlank(namespace)) {

Copilot uses AI. Check for mistakes.

}

// === 新增:发布规则到 Nacos 的方法 ===
private void publishRulesToNacos(String app) throws Exception {
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method throws generic Exception. Consider catching and wrapping with a more specific exception or logging the error instead of propagating.

Suggested change
private void publishRulesToNacos(String app) throws Exception {
private void publishRulesToNacos(String app) {

Copilot uses AI. Check for mistakes.

Comment on lines +278 to +280
private void publishRulesToNacos(String app) throws Exception {
List<ParamFlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method throws generic Exception. Consider catching and wrapping with a more specific exception or logging the error instead of propagating.

Suggested change
private void publishRulesToNacos(String app) throws Exception {
List<ParamFlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
private void publishRulesToNacos(String app) {
try {
List<ParamFlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
} catch (Exception e) {
logger.error("Error publishing parameter flow rules to Nacos for app: {}", app, e);
throw new RuntimeException("Failed to publish parameter flow rules to Nacos for app: " + app, e);
}

Copilot uses AI. Check for mistakes.

Comment on lines +295 to +297
private void publishRulesToNacos(String app) throws Exception {
List<FlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method throws generic Exception. Consider catching and wrapping with a more specific exception or logging the error instead of propagating.

Suggested change
private void publishRulesToNacos(String app) throws Exception {
List<FlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
private void publishRulesToNacos(String app) {
try {
List<FlowRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
} catch (Exception e) {
logger.error("Error publishing rules to Nacos for app: {}", app, e);
throw new RuntimeException("Failed to publish rules to Nacos for app: " + app, e);
}

Copilot uses AI. Check for mistakes.

Comment on lines +171 to +173
private void publishRulesToNacos(String app) throws Exception {
List<DegradeRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method throws generic Exception. Consider catching and wrapping with a more specific exception or logging the error instead of propagating.

Suggested change
private void publishRulesToNacos(String app) throws Exception {
List<DegradeRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
private void publishRulesToNacos(String app) {
try {
List<DegradeRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
} catch (Exception e) {
logger.error("Failed to publish degrade rules to Nacos for app: {}", app, e);
throw new RuntimeException("Failed to publish degrade rules to Nacos for app: " + app, e);
}

Copilot uses AI. Check for mistakes.

Comment on lines +218 to +220
private void publishRulesToNacos(String app) throws Exception {
List<AuthorityRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method throws generic Exception. Consider catching and wrapping with a more specific exception or logging the error instead of propagating.

Suggested change
private void publishRulesToNacos(String app) throws Exception {
List<AuthorityRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
private void publishRulesToNacos(String app) {
try {
List<AuthorityRuleEntity> rules = repository.findAllByApp(app);
rulePublisher.publish(app, rules);
} catch (Exception e) {
logger.error("Failed to publish authority rules to Nacos for app: {}", app, e);
throw new RuntimeException("Failed to publish authority rules to Nacos for app: " + app, e);
}

Copilot uses AI. Check for mistakes.

entity.setGmtModified(date);
try {
entity = repository.save(entity);
publishRulesToNacos(entity.getApp());
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publishRulesToNacos call should be wrapped in a try-catch block to handle exceptions properly and avoid breaking the rule save operation.

Copilot uses AI. Check for mistakes.

entity.setGmtModified(new Date());
try {
entity = repository.save(entity);
publishRulesToNacos(entity.getApp());
Copy link
Preview

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publishRulesToNacos call should be wrapped in a try-catch block to handle exceptions properly and avoid breaking the rule update operation.

Copilot uses AI. Check for mistakes.

<!-- Nacos 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider add nacos-discovery?


private final Logger logger = LoggerFactory.getLogger(AuthorityRuleController.class);

// === 新增:注入 Nacos Provider/Publisher ===
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of comment seems unnecessary - we can safely remove it to keep the code concise. (the same applies to other similar comments below)

@LearningGp LearningGp added area/dashboard Issues or PRs about Sentinel Dashboard wait-for-response PRs that require further response labels Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dashboard Issues or PRs about Sentinel Dashboard wait-for-response PRs that require further response
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants