1.下载dashboard源码
源代码地址:https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard
本笔记下载的版本为:1.8.6
2.定位登录验权代码
可自行通过实现AuthService接口,自定义自己的鉴权方案。
3.定位规则调整代码
在源代码的测试代码里已经有着对规则持久化的demo;分别有着apollo、nacos、zk三种持久化方案demo。
针对其进行nacos持久化,需在controller层的接口进行调整
4.规则持久化例子
4.1 持久化接口
由于客户端是基于spring cloud gateway的,所以流控规则的接口是使用gateway的controller;
一般默认情况下,都是调用FlowControllerV1接口
4.2 在rule包下新建NacosConfig、NacosConfigUtil、GatewayFlowRuleNacosPublisher、GatewayFlowRuleNacosProvider
@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 Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
return JSON::toJSONString;
}
@Bean
public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
}
@Bean
public ConfigService nacosConfigService() throws Exception {
return ConfigFactory.createConfigService("localhost");
}
}
/**
* @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() {}
}
@Component("gatewayFlowRuleNacosPublisher")
public class GatewayFlowRuleNacosPublisher implements DynamicRulePublisher<List<GatewayFlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<List<GatewayFlowRuleEntity>, String> converter;
@Override
public void publish(String app, List<GatewayFlowRuleEntity> rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
NacosConfigUtil.GROUP_ID, converter.convert(rules));
}
}
@Component("gatewayFlowRuleNacosProvider")
public class GatewayFlowRuleNacosProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {
@Autowired
private ConfigService configService;
@Autowired
private Converter<String, List<GatewayFlowRuleEntity>> converter;
@Override
public List<GatewayFlowRuleEntity> 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);
}
}
4.3 持久化接口调整
引入nacos的发布、获取bean
private void publishRulesToNacos(String app,String ip, Integer port){
try {
List<GatewayFlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
rulePublisher.publish(app,rules);
} catch (Exception e) {
e.printStackTrace();
}
}
5.客户端监听nacos配置
5.1 客户端引入nacos依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
5.2 在应用启动完成后,创建监听事件
@PostConstruct
public void listenerNacos(){
String dataId="api-gateway-flow-rules";
String group="SENTINEL_GROUP";
final ConfigService configService;
try {
configService = NacosFactory.createConfigService("127.0.0.1");
configService.addListener(dataId, group, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
System.err.println("接收到调整信息:" + configInfo);
FlowRuleManager.loadRules(JSON.parseArray(configInfo, FlowRule.class));
System.out.println("流控规则刷新完成!");
}
});
} catch (NacosException e) {
e.printStackTrace();
}
}