feat(dashboard): 集成Nacos配置中心支持
- 添加Nacos配置属性以连接配置中心 - 引入sentinel-datasource-nacos依赖用于规则持久化 - 实现FlowRuleNacosProvider从Nacos获取流控规则 - 实现FlowRuleNacosPublisher向Nacos发布流控规则 - 创建NacosConfig配置类管理Nacos连接和服务实例 - 定义NacosConfigUtil常量类统一配置标识符 - 配置文件中增加默认Nacos服务器地址和认证信息
This commit is contained in:
parent
a08dc695ba
commit
01e806b87b
|
|
@ -104,7 +104,11 @@
|
|||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-api</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</dependency>
|
||||
<!-- for Apollo rule publisher sample -->
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -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<List<FlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<String, List<FlowRuleEntity>> converter;
|
||||
|
||||
/**
|
||||
* 获取规则
|
||||
*
|
||||
* @param appName 应用名称
|
||||
* @return 规则列表
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
public List<FlowRuleEntity> 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<List<FlowRuleEntity>> {
|
||||
|
||||
@Autowired
|
||||
private ConfigService configService;
|
||||
@Autowired
|
||||
private Converter<List<FlowRuleEntity>, String> converter;
|
||||
|
||||
@Override
|
||||
public void publish(String app, List<FlowRuleEntity> 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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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<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 {
|
||||
// 创建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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {}
|
||||
}
|
||||
|
|
@ -22,3 +22,11 @@ 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@
|
||||
|
||||
##############################################################
|
||||
# nacos config
|
||||
nacos.config.server-addr=http://127.0.0.1:8848
|
||||
nacos.config.namespace=
|
||||
nacos.config.username=nacos
|
||||
nacos.config.password=nacos
|
||||
##############################################################
|
||||
|
|
|
|||
Loading…
Reference in New Issue