目录
博文参考
源码地址
背景
代码实现
1.到alibab/Sentinel官网地址https://github.com/alibaba/Sentinel下载最新版源码到本地,用idea打开,这里主要用到Sentinel-dashboard。
2. 修改pom.xml中的sentinel-datasource-nacos的依赖,将test注释掉,这样才能在主程序中使用。
4.在com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包,用来编写针对Nacos的扩展实现。
5. 创建Nacos的配置类,具体代码如下:
6. 实现Nacos的配置拉取。创建FlowRuleNacosProvider类,具体代码如下:
7. 实现Nacos的配置推送。创建FlowRuleNacosPublisher类,具体代码如下:
8.修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2中DynamicRuleProvider和DynamicRulePublisher注入的Bean,改为上面我们编写的针对Apollo的实现:
9.启动DashboardApplication类的main方法,启动sentinel-dashboard应用,访问http://localhost:8080,登陆sentinel
10.限流测试参考Spring Cloud Alibaba(4)Sentinel使用nacos对存储规则持久化 带源码
http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-4/
https://gitee.com/acelee723/acelee-sentinel-dashboard-nacos
上一篇我们介绍了如何通过改造Sentinel Dashboard来实现修改规则之后自动同步到Apollo。下面通过这篇,详细介绍当使用Nacos作为配置中心之后,如何实现Sentinel Dashboard中修改规则同步到Nacos。关于下面改造的原理和分析可以见上一篇《Sentinel Dashboard中修改规则同步到Apollo》的头两节内容,这里不重复介绍了。
下面直接来看看如何实现的具体改造步骤,这里参考了Sentinel Dashboard
源码中关于Nacos实现的测试用例。但是由于考虑到与Spring Cloud Alibaba的结合使用,略作修改。
pom.xml
中的sentinel-datasource-nacos的依赖,将test
注释掉,这样才能在主程序中使用。webapp
/resources/app/scripts/directives/sidebar/sidebar.html
中的这段代码:
流控规则
修改为:
流控规则
com.alibaba.csp.sentinel.dashboard.rule
包下新建一个nacos包,用来编写针对Nacos的扩展实现。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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Properties;
/**
* TODO
*
* @Author Ace Lee
* @Date 2019/8/12 12:00
* @Version 1.0
**/
@Configuration
public class NacosConfig {
@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 {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
// properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
return ConfigFactory.createConfigService(properties);
}
}
如果用到了namespace隔离环境,可以在nacosConfigService
方法中再加入配置,比如:
properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");
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;
/**
* TODO
*
* @Author Ace Lee
* @Date 2019/8/12 14:13
* @Version 1.0
**/
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider> {
@Autowired
private ConfigService configService;
@Autowired
private Converter> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public List getRules(String appName) throws Exception {
String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
if (StringUtil.isEmpty(rules)) {
return new ArrayList<>();
}
return converter.convert(rules);
}
}
getRules
方法中的appName
参数是Sentinel中的服务名称。
configService.getConfig
方法是从Nacos中获取配置信息的具体操作。其中,DataId和GroupId分别对应客户端使用时候的对应配置。比如这里的例子对应了之前我们在《Sentinel使用Nacos存储规则》一文中的配置,具体如下:
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
注意:两边的DataId和GroupId必须对应上。
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* TODO
*
* @Author Ace Lee
* @Date 2019/8/12 14:20
* @Version 1.0
**/
@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher> {
@Autowired
private ConfigService configService;
@Autowired
private Converter, String> converter;
public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
public static final String GROUP_ID = "DEFAULT_GROUP";
@Override
public void publish(String app, List rules) throws Exception {
AssertUtil.notEmpty(app, "app name cannot be empty");
if (rules == null) {
return;
}
configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
}
}
- 这里的大部分内容与上一步中的实现一致。主要就是Nacos中存储配置的DataId和GroupId不要弄错。
com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
中DynamicRuleProvider
和DynamicRulePublisher
注入的Bean,改为上面我们编写的针对Apollo的实现:@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher> rulePublisher;
欢迎关注博主博客,后期博主会持续更新spring cloud alibaba 系列文章,敬请期待!