Spring Cloud Alibaba(7)Sentinel Dashboard中修改规则同步到Nacos 带源码

15 篇文章 3 订阅
14 篇文章 0 订阅

目录

博文参考

源码地址

背景

代码实现

1.到alibab/Sentinel官网地址https://github.com/alibaba/Sentinel下载最新版源码到本地,用idea打开,这里主要用到Sentinel-dashboard。

2. 修改pom.xml中的sentinel-datasource-nacos的依赖,将test注释掉,这样才能在主程序中使用。

3. 找到webapp/resources/app/scripts/directives/sidebar/sidebar.html中的这段代码: 

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的结合使用,略作修改。

1.到alibab/Sentinel官网地址https://github.com/alibaba/Sentinel下载最新版源码到本地,用idea打开,这里主要用到Sentinel-dashboard。

2. 修改pom.xml中的sentinel-datasource-nacos的依赖,将<scope>test</scope>注释掉,这样才能在主程序中使用。

3. 找到webapp/resources/app/scripts/directives/sidebar/sidebar.html中的这段代码: 

<li ui-sref-active="active">
    <a ui-sref="dashboard.flowV1({app: entry.app})">
        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则
    </a>
</li>

修改为:

<li ui-sref-active="active">
    <a ui-sref="dashboard.flow({app: entry.app})">
        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则
    </a>
</li>

4.在com.alibaba.csp.sentinel.dashboard.rule包下新建一个nacos包,用来编写针对Nacos的扩展实现。

5. 创建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<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 {
        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");

6. 实现Nacos的配置拉取。创建FlowRuleNacosProvider类,具体代码如下:

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<List<FlowRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<String, List<FlowRuleEntity>> converter;

    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
    public static final String GROUP_ID = "DEFAULT_GROUP";

    @Override
    public List<FlowRuleEntity> 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中获取配置信息的具体操作。其中,DataIdGroupId分别对应客户端使用时候的对应配置。比如这里的例子对应了之前我们在《Sentinel使用Nacos存储规则》一文中的配置,具体如下:

spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel

 注意:两边的DataIdGroupId必须对应上。

7. 实现Nacos的配置推送。创建FlowRuleNacosPublisher类,具体代码如下: 

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<List<FlowRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<List<FlowRuleEntity>, 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<FlowRuleEntity> 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中存储配置的DataIdGroupId不要弄错。

8.修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2DynamicRuleProviderDynamicRulePublisher注入的Bean,改为上面我们编写的针对Apollo的实现:

@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

9.启动DashboardApplication类的main方法,启动sentinel-dashboard应用,访问http://localhost:8080,登陆sentinel

10.限流测试参考Spring Cloud Alibaba(4)Sentinel使用nacos对存储规则持久化 带源码


欢迎关注博主博客,后期博主会持续更新spring cloud alibaba 系列文章,敬请期待!   

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Cloud Alibaba是一个基于Spring Cloud的一组开源框架和组件,旨在为微服务架构提供解决方案。Sentinel Gateway是Spring Cloud Alibaba的一部分,是一个高性能的API网关,用于管理和保护微服务的访问权限。 2022年的Sentinel Gateway将在当前的功能基础上进行进一步的优化和增强。首先,它将提供更加灵活和强大的流量控制和熔断降级机制,以便更好地应对高并发和流量高峰情况。这将更好地保护后端微服务的稳定性和可靠性,确保系统的正常运行。 其次,Sentinel Gateway将提供更好的安全防护能力,通过集成常见的安全防护机制,如黑名单、白名单、IP过滤等,保护系统免受恶意攻击和非法访问。这将提升整个微服务架构的安全性,保护敏感数据和业务逻辑的安全性。 此外,Sentinel Gateway还将提供更加强大和灵活的路由配置功能,允许用户根据具体需求和策略进行动态路由转发。这将使得微服务架构在面对复杂的网络环境和多样化的服务调用场景时能够更加灵活和高效地进行消息传递和数据交换。 最后,Sentinel Gateway将进一步完善其监控和统计功能,提供更加全面和准确的系统运行状态和性能指标数据。这将帮助用户更好地理解和掌握系统的运行状况,及时发现和解决潜在的问题,提升整个微服务架构的可管理性和可维护性。 综上所述,2022年的Sentinel Gateway将在流量控制、熔断降级、安全防护、动态路由、监控统计等方面进行进一步的优化和增强,为微服务架构提供更加强大和可靠的API网关解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值