refactor(nacos): 重构Nacos配置管理并更新默认设置
- 使用@Autowired注入Nacos配置信息,替换原有@Value方式 - 新增NacosServerConfig类统一管理Nacos服务器配置 - 更新application.properties中的默认用户名和密码为admin/admin - 修改Nacos命名空间配置为namespace-sentinel - 调整Nacos配置工具类中的数据ID前缀和组ID常量 - 添加API分组和网关流控规则相关的数据ID生成方法 - 统一Nacos配置服务创建逻辑,使用新的配置注入方式
This commit is contained in:
parent
35dbe68c4d
commit
c48e742041
|
|
@ -21,7 +21,7 @@ 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.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
|
@ -34,22 +34,16 @@ import java.util.Properties;
|
|||
*/
|
||||
@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 -----------------------
|
||||
// 注入配置信息
|
||||
@Autowired
|
||||
private NacosServerConfig config;
|
||||
|
||||
/***************** System Start ******************/
|
||||
@Bean
|
||||
public Converter<List<AuthorityRuleEntity>, String> authorityRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<AuthorityRuleEntity>> authorityRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
|
||||
|
|
@ -62,6 +56,7 @@ public class NacosConfig {
|
|||
public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, DegradeRuleEntity.class);
|
||||
|
|
@ -74,6 +69,7 @@ public class NacosConfig {
|
|||
public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<SystemRuleEntity>> systemRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, SystemRuleEntity.class);
|
||||
|
|
@ -86,6 +82,7 @@ public class NacosConfig {
|
|||
public Converter<List<ParamFlowRuleEntity>, String> paramRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<ParamFlowRuleEntity>> paramRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
|
||||
|
|
@ -98,10 +95,12 @@ public class NacosConfig {
|
|||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
|
||||
return JSON::toJSONString;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
|
||||
return s -> JSON.parseArray(s, FlowRuleEntity.class);
|
||||
}
|
||||
|
||||
/***************** Flow End ******************/
|
||||
|
||||
|
||||
|
|
@ -109,11 +108,10 @@ public class NacosConfig {
|
|||
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");
|
||||
properties.setProperty(PropertyKeyConst.SERVER_ADDR, config.getServerAddr());
|
||||
properties.setProperty(PropertyKeyConst.NAMESPACE, config.getNamespace());
|
||||
properties.setProperty(PropertyKeyConst.USERNAME, config.getUsername());
|
||||
properties.setProperty(PropertyKeyConst.PASSWORD, config.getPassword());
|
||||
// 创建Nacos配置服务实例并返回
|
||||
return ConfigFactory.createConfigService(properties);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,14 +21,24 @@ package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
|||
*/
|
||||
public final class NacosConfigUtil {
|
||||
|
||||
public static final String GROUP_ID = "SENTINEL_GROUP";
|
||||
// public static final String GROUP_ID = "SENTINEL_GROUP";
|
||||
public static final String GROUP_ID = "DEFAULT_GROUP";
|
||||
public static final String GLOBAL_DATA_ID_POSTFIX = ".json";
|
||||
|
||||
public static final String FLOW_DATA_ID_PREFIX = "flow-rules-";
|
||||
public static final String PARAM_FLOW_DATA_ID_PREFIX = "param-rules-";
|
||||
public static final String DEGRADE_DATA_ID_PREFIX = "degrade-rules-";
|
||||
public static final String AUTHORITY_DATA_ID_PREFIX = "auth-rules-";
|
||||
public static final String SYSTEM_DATA_ID_PREFIX = "system-rules-";
|
||||
// 流控规则
|
||||
public static final String FLOW_DATA_ID_PREFIX = "flow-";
|
||||
// 热点参数规则
|
||||
public static final String PARAM_FLOW_DATA_ID_PREFIX = "param-";
|
||||
// 熔断规则
|
||||
public static final String DEGRADE_DATA_ID_PREFIX = "degrade-";
|
||||
// 授权规则
|
||||
public static final String AUTHORITY_DATA_ID_PREFIX = "auth-";
|
||||
// 系统规则
|
||||
public static final String SYSTEM_DATA_ID_PREFIX = "system-";
|
||||
// api分组
|
||||
public static final String API_GROUP_DATA_ID_PREFIX = "api-group-";
|
||||
// 网关流控规则
|
||||
public static final String GATEWAY_FLOW_DATA_ID_PREFIX = "gateway-flow-";
|
||||
|
||||
public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
|
||||
|
||||
|
|
@ -46,6 +56,18 @@ public final class NacosConfigUtil {
|
|||
private NacosConfigUtil() {
|
||||
}
|
||||
|
||||
|
||||
public static String getApiGroupDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.API_GROUP_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getGatewayFlowDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.GATEWAY_FLOW_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
|
||||
|
||||
public static String getSystemRuleDataId(String appName) {
|
||||
return String.format("%s%s%s", NacosConfigUtil.SYSTEM_DATA_ID_PREFIX, appName, NacosConfigUtil.GLOBAL_DATA_ID_POSTFIX);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package com.alibaba.csp.sentinel.dashboard.rule.nacos;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "nacos.config")
|
||||
public class NacosServerConfig {
|
||||
private String serverAddr;
|
||||
private String namespace;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
|
||||
public void setServerAddr(String serverAddr) {
|
||||
this.serverAddr = serverAddr;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
public void setNamespace(String namespace) {
|
||||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSONObject.toJSONString(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,32 +2,27 @@
|
|||
server.servlet.encoding.force=true
|
||||
server.servlet.encoding.charset=UTF-8
|
||||
server.servlet.encoding.enabled=true
|
||||
|
||||
#cookie name setting
|
||||
server.servlet.session.cookie.name=sentinel_dashboard_cookie
|
||||
|
||||
#logging settings
|
||||
logging.level.org.springframework.web=INFO
|
||||
logging.file.name=${user.home}/logs/csp/sentinel-dashboard.log
|
||||
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
#logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
|
||||
#auth settings
|
||||
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine,/version
|
||||
auth.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
|
||||
# If auth.enabled=false, Sentinel console disable login
|
||||
auth.username=sentinel
|
||||
auth.password=sentinel
|
||||
|
||||
auth.username=admin
|
||||
auth.password=admin
|
||||
# Inject the dashboard version. It's required to enable
|
||||
# filtering in pom.xml for this resource file.
|
||||
sentinel.dashboard.version=@project.version@
|
||||
|
||||
##############################################################
|
||||
# nacos config
|
||||
#nacos.config.server-addr=http://127.0.0.1:8848
|
||||
nacos.config.server-addr=http://172.16.8.70:8848
|
||||
nacos.config.namespace=
|
||||
nacos.config.namespace=namespace-sentinel
|
||||
nacos.config.username=nacos
|
||||
nacos.config.password=nacos
|
||||
##############################################################
|
||||
|
|
|
|||
Loading…
Reference in New Issue