暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

sentinel dashboard集成nacos

轻聊浅谈闲码 2020-12-31
771

sentinel dashboard配置

修改pom配置文件
  1. <dependency>

  2. <groupId>com.alibaba.csp</groupId>

  3. <artifactId>sentinel-datasource-nacos</artifactId>

  4. <!--<scope>test</scope>-->

  5. </dependency>

修改resources/app/scripts/directives/sidebar/sidebar.html文件
  1. <li ui-sref-active="active" ng-if="!entry.isGateway">

  2. <a ui-sref="dashboard.flowV1({app: entry.app})">

  3. <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>

  4. </li>

修改为:

  1. <li ui-sref-active="active" ng-if="!entry.isGateway">

  2. <a ui-sref="dashboard.flow({app: entry.app})">

  3. <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>

  4. </li>

修改后会调用FlowControllerV2中的接口

application.properties 中增加配置
  1. sentinel.nacos.serverAddr=localhost:8848

  2. sentinel.nacos.namespace=

  3. sentinel.nacos.group-id=DEFAULT_GROUP

加入nacos配置
  1. @EnableConfigurationProperties(NacosPropertiesConfiguration.class)

  2. @Configuration

  3. public class NacosConfiguration {


  4. @Bean

  5. public com.alibaba.csp.sentinel.datasource.Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {

  6. return JSON::toJSONString;

  7. }


  8. @Bean

  9. public com.alibaba.csp.sentinel.datasource.Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {

  10. return s -> JSON.parseArray(s, FlowRuleEntity.class);

  11. }


  12. @Bean

  13. public ConfigService nacosConfigService(NacosPropertiesConfiguration nacosPropertiesConfiguration) throws NacosException {

  14. Properties properties = new Properties();

  15. properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfiguration.getServerAddr());

  16. properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfiguration.getNamespace());

  17. return ConfigFactory.createConfigService(properties);

  18. }

  19. }

从Nacos配置中心获取控流配置
  1. @Component

  2. public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {


  3. private static Logger log = LoggerFactory.getLogger(FlowRuleNacosProvider.class);


  4. @Autowired

  5. private NacosPropertiesConfiguration nacosPropertiesConfiguration;


  6. @Autowired

  7. private ConfigService configService;


  8. @Autowired

  9. private Converter<String, List<FlowRuleEntity>> converter;


  10. @Override

  11. public List<FlowRuleEntity> getRules(String appName) throws Exception {

  12. String dataId = new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString();

  13. String rules = configService.getConfig(dataId, nacosPropertiesConfiguration.getGroupId(), 3000);

  14. log.info("pull rule config: {}", rules);

  15. if (StringUtil.isEmpty(rules)) {

  16. return new ArrayList<>();

  17. }

  18. return converter.convert(rules);

  19. }

  20. }

动态刷新从Nacos配置中心获取的控流配置
  1. @Component

  2. public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {


  3. private static Logger log = LoggerFactory.getLogger(FlowRuleNacosProvider.class);


  4. @Autowired

  5. private NacosPropertiesConfiguration nacosPropertiesConfiguration;


  6. @Autowired

  7. private ConfigService configService;


  8. @Autowired

  9. private Converter<List<FlowRuleEntity>, String> converter;


  10. @Override

  11. public void publish(String app, List<FlowRuleEntity> rules) throws Exception {

  12. if (rules == null || rules.size() == 0) {

  13. return;

  14. }

  15. String dataId = new StringBuilder(app).append(NacosConstants.DATA_ID_POSTFIX).toString();

  16. configService.publishConfig(dataId, nacosPropertiesConfiguration.getGroupId(), converter.convert(rules));

  17. }

  18. }

修改FlowControllerV2改为注入nacos
  1. @Autowired

  2. @Qualifier("flowRuleNacosProvider")

  3. private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;


  4. @Autowired

  5. @Qualifier("flowRuleNacosPublisher")

  6. private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

打包发布

客户端:

引入依赖
  1. <dependency>

  2. <groupId>com.alibaba.cloud</groupId>

  3. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

  4. <version>2.1.1.RELEASE</version>

  5. </dependency>

yml文件加入配置
  1. spring:

  2. cloud:

  3. sentinel:

  4. datasource:

  5. - nacos:

  6. server-addr: 81.69.162.162:8848

  7. data-id: ${spring.application.name}-sentinel-flow

  8. group-id: DEFAULT_GROUP

  9. data-type: json&

  10. rule-type: flow

启动完成后,在sentinel dashboard界面增加流控规则

可以发现nacos中自动增加了一条配置

文章转载自轻聊浅谈闲码,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论