前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >24.Sentinel 同步流控规则到nacos

24.Sentinel 同步流控规则到nacos

作者头像
AI码师
发布2023-12-11 15:52:21
1420
发布2023-12-11 15:52:21
举报

想将修改后的流控规则同步到nacos,需要修改sentinel源码,所以大家需要提前讲sentinel源码下载下来:

下载源码

代码语言:javascript
复制
git clone https://github.com/alibaba/Sentinel.git

定义nacos 服务地址

代码语言:javascript
复制
/**
 * @Author 乐哥聊编程
 * @Doc 关注公众号"乐哥聊编程"获取文档和源码
 * @Date 2023/6/18
 * @Description
 */
@ConfigurationProperties(prefix = "sentinel.nacos")
public class NacosPropertiesConfig {
    private String serverAddr;
    private String dataId;
    private String groupId="DEFAULT_GROUP";
    private String namespace="";
    }

application.yaml中配置

代码语言:javascript
复制
sentinel.nacos.server-addr=192.168.64.2:8848

NacosConfigService 初始化

代码语言:javascript
复制
@Configuration
@EnableConfigurationProperties(NacosPropertiesConfig.class)
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(NacosPropertiesConfig nacosPropertiesConfig) throws Exception {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfig.getServerAddr());
        properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfig.getNamespace());
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfig.getServerAddr());
        return ConfigFactory.createConfigService(properties);
    }
}

创建Nacos 配置发布器

代码语言:javascript
复制
@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;
        }
        configService.publishConfig(app + NacosConfigUtil.FLOW_DATA_ID_POSTFIX,
            NacosConfigUtil.GROUP_ID, converter.convert(rules));
    }
}

定义nacos 配置规则解析器

代码语言:javascript
复制
@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {

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

    @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);
    }
}

更新FlowControllerV2,配置nacos作为数据来源

代码语言:javascript
复制
    @Autowired
    @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

修改页面接口调用

sidebar.html

代码语言:javascript
复制
<li ui-sref-active="active" ng-if="!entry.isGateway">
            <a ui-sref="dashboard.flowV1({app: entry.app})">
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>
          </li>
          
修改为

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

项目配置修改

代码语言:javascript
复制
spring:
  application:
    name: sentinel-nacos-sync
  cloud:
    sentinel:
      transport:
        dashboard: 127.0.0.1:8089
      datasource:
        flow_ds:
          nacos:
            server-addr: 192.168.64.2:8848
            data-id: sentinel-nacos-sync-flow-rules
            data-type: json
            group-id: SENTINEL_GROUP
            rule-type: flow

Nacos 添加流控规则

代码语言:javascript
复制
[
    {
        "app": "sentinel-nacos-sync",
        "clusterMode": false,
        "controlBehavior": 0,
        "count": 554.0,
        "gmtModified": 1687070437587,
        "grade": 1,
        "id": 1,
        "limitApp": "default",
        "resource": "/test1",
        "strategy": 0
    },
    {
        "app": "sentinel-nacos-sync",
        "clusterMode": false,
        "controlBehavior": 0,
        "count": 1.0,
        "gmtModified": 1687071108157,
        "grade": 1,
        "id": 2,
        "limitApp": "default",
        "resource": "/test2",
        "strategy": 0
    },
    {
        "app": "sentinel-nacos-sync",
        "clusterMode": false,
        "controlBehavior": 0,
        "count": 55.0,
        "grade": 1,
        "id": 3,
        "limitApp": "default",
        "resource": "/test3",
        "strategy": 0
    },
    {
        "app": "sentinel-nacos-sync",
        "clusterMode": false,
        "controlBehavior": 0,
        "count": 55.0,
        "grade": 1,
        "id": 4,
        "limitApp": "default",
        "resource": "/test4",
        "strategy": 0
    }
]

启动验证

成功

??

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-12-08,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 乐哥聊编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 下载源码
  • 定义nacos 服务地址
  • NacosConfigService 初始化
  • 创建Nacos 配置发布器
  • 定义nacos 配置规则解析器
  • 更新FlowControllerV2,配置nacos作为数据来源
  • 修改页面接口调用
  • 项目配置修改
  • Nacos 添加流控规则
  • 启动验证
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com