From 01e806b87bef25189448439ae00e9b435c19340f Mon Sep 17 00:00:00 2001
From: mshe <666666666@666666666.666666666>
Date: Thu, 20 Nov 2025 09:29:42 +0800
Subject: [PATCH] =?UTF-8?q?feat(dashboard):=20=E9=9B=86=E6=88=90Nacos?=
=?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=B8=AD=E5=BF=83=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 添加Nacos配置属性以连接配置中心
- 引入sentinel-datasource-nacos依赖用于规则持久化
- 实现FlowRuleNacosProvider从Nacos获取流控规则
- 实现FlowRuleNacosPublisher向Nacos发布流控规则
- 创建NacosConfig配置类管理Nacos连接和服务实例
- 定义NacosConfigUtil常量类统一配置标识符
- 配置文件中增加默认Nacos服务器地址和认证信息
---
sentinel-dashboard/pom.xml | 6 +-
.../rule/nacos/FlowRuleNacosProvider.java | 57 +++++++++++++++
.../rule/nacos/FlowRuleNacosPublisher.java | 52 +++++++++++++
.../dashboard/rule/nacos/NacosConfig.java | 73 +++++++++++++++++++
.../dashboard/rule/nacos/NacosConfigUtil.java | 42 +++++++++++
.../src/main/resources/application.properties | 10 ++-
6 files changed, 238 insertions(+), 2 deletions(-)
create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java
create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java
create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfig.java
create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java
diff --git a/sentinel-dashboard/pom.xml b/sentinel-dashboard/pom.xml
index 75452650..8ee444f6 100644
--- a/sentinel-dashboard/pom.xml
+++ b/sentinel-dashboard/pom.xml
@@ -104,7 +104,11 @@
com.alibaba.csp
sentinel-datasource-nacos
- test
+
+
+ com.alibaba.nacos
+ nacos-api
+ 2.5.1
diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java
new file mode 100644
index 00000000..bf8534b8
--- /dev/null
+++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
+import com.alibaba.csp.sentinel.datasource.Converter;
+import com.alibaba.csp.sentinel.util.StringUtil;
+import com.alibaba.nacos.api.config.ConfigService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+@Component("flowRuleNacosProvider")
+public class FlowRuleNacosProvider implements DynamicRuleProvider> {
+
+ @Autowired
+ private ConfigService configService;
+ @Autowired
+ private Converter> converter;
+
+ /**
+ * 获取规则
+ *
+ * @param appName 应用名称
+ * @return 规则列表
+ * @throws Exception 异常
+ */
+ @Override
+ public List getRules(String appName) throws Exception {
+ String rules = configService.getConfig(appName + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
+ NacosConfigUtil.GROUP_ID, 3000);
+ if (StringUtil.isEmpty(rules)) {
+ return new ArrayList<>();
+ }
+ return converter.convert(rules);
+ }
+}
diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java
new file mode 100644
index 00000000..df0f5712
--- /dev/null
+++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
+import com.alibaba.csp.sentinel.datasource.Converter;
+import com.alibaba.csp.sentinel.util.AssertUtil;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.config.ConfigType;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+@Component("flowRuleNacosPublisher")
+public class FlowRuleNacosPublisher implements DynamicRulePublisher> {
+
+ @Autowired
+ private ConfigService configService;
+ @Autowired
+ private Converter, String> converter;
+
+ @Override
+ public void publish(String app, List rules) throws Exception {
+ AssertUtil.notEmpty(app, "app name cannot be empty");
+ if (rules == null) {
+ return;
+ }
+ // 发布配置到nacos
+ configService.publishConfig(app + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
+ NacosConfigUtil.GROUP_ID, converter.convert(rules));
+// NacosConfigUtil.GROUP_ID, converter.convert(rules), ConfigType.JSON.getType());
+ }
+}
diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfig.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfig.java
new file mode 100644
index 00000000..2a6fc873
--- /dev/null
+++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfig.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
+import com.alibaba.csp.sentinel.datasource.Converter;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.config.ConfigFactory;
+import com.alibaba.nacos.api.config.ConfigService;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+@Configuration
+public class NacosConfig {
+ // ----------------------- 读取配置文件中设置的值 start -----------------------
+ @Value("${nacos.config.server-addr:http://127.0.0.1:8848}")
+ private String nacosServerAddr;
+
+ @Value("${nacos.config.namespace}")
+ private String nacosNamespace;
+
+ @Value("${nacos.config.username:nacos}")
+ private String nacosUsername;
+
+ @Value("${nacos.config.password:nacos}")
+ private String nacosPassword;
+ // ----------------------- 读取配置文件中设置的值 end -----------------------
+
+ @Bean
+ public Converter, String> flowRuleEntityEncoder() {
+ return JSON::toJSONString;
+ }
+
+ @Bean
+ public Converter> flowRuleEntityDecoder() {
+ return s -> JSON.parseArray(s, FlowRuleEntity.class);
+ }
+
+ @Bean
+ public ConfigService nacosConfigService() throws Exception {
+ // 创建Nacos配置服务
+ Properties properties = new Properties();
+ properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosServerAddr);
+ properties.setProperty(PropertyKeyConst.NAMESPACE, nacosNamespace);
+ properties.setProperty(PropertyKeyConst.USERNAME, nacosUsername);
+ properties.setProperty(PropertyKeyConst.PASSWORD, nacosPassword);
+ // return ConfigFactory.createConfigService("localhost");
+ // 创建Nacos配置服务实例并返回
+ return ConfigFactory.createConfigService(properties);
+ }
+}
diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java
new file mode 100644
index 00000000..0c666313
--- /dev/null
+++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+/**
+ * @author Eric Zhao
+ * @since 1.4.0
+ */
+public final class NacosConfigUtil {
+
+ public static final String GROUP_ID = "SENTINEL_GROUP";
+
+ public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
+ public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules";
+ public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
+
+ /**
+ * cc for `cluster-client`
+ */
+ public static final String CLIENT_CONFIG_DATA_ID_POSTFIX = "-cc-config";
+ /**
+ * cs for `cluster-server`
+ */
+ public static final String SERVER_TRANSPORT_CONFIG_DATA_ID_POSTFIX = "-cs-transport-config";
+ public static final String SERVER_FLOW_CONFIG_DATA_ID_POSTFIX = "-cs-flow-config";
+ public static final String SERVER_NAMESPACE_SET_DATA_ID_POSTFIX = "-cs-namespace-set";
+
+ private NacosConfigUtil() {}
+}
diff --git a/sentinel-dashboard/src/main/resources/application.properties b/sentinel-dashboard/src/main/resources/application.properties
index 1bbb0819..eecec86a 100755
--- a/sentinel-dashboard/src/main/resources/application.properties
+++ b/sentinel-dashboard/src/main/resources/application.properties
@@ -21,4 +21,12 @@ auth.password=sentinel
# Inject the dashboard version. It's required to enable
# filtering in pom.xml for this resource file.
-sentinel.dashboard.version=@project.version@
\ No newline at end of file
+sentinel.dashboard.version=@project.version@
+
+##############################################################
+# nacos config
+nacos.config.server-addr=http://127.0.0.1:8848
+nacos.config.namespace=
+nacos.config.username=nacos
+nacos.config.password=nacos
+##############################################################