SpringBoot 2.0 + Nacos + Sentinel 流控规则集中存储

点击▲关注 “爪哇笔记”   给公众号标星置顶

更多精彩 第一时间直达

640?wx_fmt=jpeg

前言

Sentinel 原生版本的规则管理通过API 将规则推送至客户端并直接更新到内存中,并不能直接用于生产环境。不过官方也提供了一种 Push模式,扩展读数据源 ReadableDataSource,规则中心统一推送,客户端通过注册监听器的方式时刻监听变化,比如使用 Nacos、Zookeeper 等配置中心。这种方式有更好的实时性和一致性保证。这里我们通过配置 Nacos 来实现流控规则的统一存储配置。

架构

640?wx_fmt=png

控制台推送规则至配置中心,客户端通过监听事件从配置中心获取流控规则。

客户端配置

pom.xml 引入:

<dependency>	
      <groupId>com.alibaba.csp</groupId>	
      <artifactId>sentinel-datasource-nacos</artifactId>	
      <version>1.6.3</version>	
 </dependency>

配置文件:

# nacos的访问地址,配置参考 https://blog.52itstyle.vip/archives/4174/	
spring.cloud.sentinel.datasource.ds.nacos.server-addr=47.104.187.19:8848	
#nacos中存储规则的dataId,对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置	
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-flow-rules	
#nacos中存储规则的groupId	
spring.cloud.sentinel.datasource.ds.nacos.groupId=SENTINEL_GROUP	
#定义存储的规则类型	
spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow

控制台配置

修改 pom.xml,原来的 <scope>test</scope>去掉:

<dependency>	
      <groupId>com.alibaba.csp</groupId>	
      <artifactId>sentinel-datasource-nacos</artifactId>	
</dependency>

获取控制台源码,把 src/test 下面的包 com.alibaba.csp.sentinel.dashboard.rule.nacos 拷贝到 src/main/java 下面。

修改 NacosConfig:

/**	
 * @author Eric Zhao	
 * @since 1.4.0	
 */	
@Configuration	
public class NacosConfig {	
    @Value("${nacos.address}")	
    private String address;	
    @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("serverAddr",address);	
        return ConfigFactory.createConfigService(properties);	
    }	
}

application.properties 配置引入 Nacos:

# nacos的访问地址,配置参考 https://blog.52itstyle.vip/archives/4174/	
nacos.address=47.104.197.19:8848

FlowControllerV2 指定对应的 Bean 开启 Nacos 适配。

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

修改 sidebar.html页面, 流控规则路由从 dashboard.flowV1 改成 dashboard.flow

<-- nacos 动态规则配置-->	
<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>

如图所示,界面会多了一个回到单机页面的按钮,这里我们新增一个流控规则。

640?wx_fmt=png

登录 Nacos 后台,配置管理->配置列表:

640?wx_fmt=png

点击进入配置详情,配置内容如下:

[{	
    "app": "blog",	
    "clusterConfig": {	
        "fallbackToLocalWhenFail": true,	
        "sampleCount": 10,	
        "strategy": 0,	
        "thresholdType": 0,	
        "windowIntervalMs": 1000	
    },	
    "clusterMode": false,	
    "controlBehavior": 0,	
    "count": 2.0,	
    "gmtCreate": 1568617671812,	
    "gmtModified": 1568622253889,	
    "grade": 1,	
    "id": 6,	
    "ip": "10.136.168.88",	
    "limitApp": "default",	
    "port": 8720,	
    "resource": "blogView",	
    "strategy": 0	
}]

小结

生产环境下,推送规则正确做法应该是 配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 数据源 → Sentinel。

案例

https://gitee.com/52itstyle/spring-boot-blog

参考

https://github.com/alibaba/Sentinel

https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard

640?wx_fmt=png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值