Sentinel集成Nacos实现动态流控规则

Sentinel集成Nacos实现动态流控规则

相信大家对Sentinel已经有了初步的认识。在Spring Cloud Alibaba的整合封装之下,接口限流这件事情可以非常轻易的整合到我们的Spring Cloud应用中。但是,依然还不能完美的满足我们日常的生产需求。其中,非常重要的一点就是限流规则的持久化问题。Dashboard中设置的限流规则在应用重启之后就丢失了的问题。那么,接下来我们就来说说Sentinel的规则持久化如何实现。

使用Nacos存储限流规则
Sentinel自身就支持了多种不同的数据源来持久化规则配置,目前包括以下几种方式:

文件配置
Nacos配置
ZooKeeper配置
Apollo配置
本文我们就来一起动手尝试一下,使用Spring Cloud Alibaba的中整合的配置中心Nacos存储限流规则。

准备工作
下面我们将同时使用到Nacos和Sentinel Dashboard,所以可以先把Nacos和Sentinel Dashboard启动起来。

默认配置下启动后,它们的访问地址(后续会用到)为:

Nacos:http://localhost:8848/
Sentinel Dashboard:http://localhost:8080/
如果还没入门Nacos和Sentinel Dashboard可以通过文末的系列目录先学习之前的内容。

应用配置
第一步:在Spring Cloud应用的pom.xml中引入Spring Cloud Alibaba的Sentinel模块和Nacos存储扩展:

org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.csp sentinel-datasource-nacos 1.5.2 第二步:在Spring Cloud应用中添加配置信息:

spring.application.name=alibaba-sentinel-datasource-nacos
server.port=8003

sentinel dashboard

spring.cloud.sentinel.transport.dashboard=localhost:8080

sentinel datasource nacos :http://blog.didispace.com/spring-cloud-alibaba-sentinel-2-1/

spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId= s p r i n g . a p p l i c a t i o n . n a m e − s e n t i n e l s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . g r o u p I d = D E F A U L T G R O U P s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . r u l e − t y p e = f l o w s p r i n g . c l o u d . s e n t i n e l . t r a n s p o r t . d a s h b o a r d : s e n t i n e l d a s h b o a r d 的 访 问 地 址 , 根 据 上 面 准 备 工 作 中 启 动 的 实 例 配 置 s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . s e r v e r − a d d r : n a c o s 的 访 问 地 址 , , 根 据 上 面 准 备 工 作 中 启 动 的 实 例 配 置 s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . g r o u p I d : n a c o s 中 存 储 规 则 的 g r o u p I d s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . d a t a I d : n a c o s 中 存 储 规 则 的 d a t a I d s p r i n g . c l o u d . s e n t i n e l . d a t a s o u r c e . d s . n a c o s . r u l e − t y p e : 该 参 数 是 s p r i n g c l o u d a l i b a b a 升 级 到 0.2.2 之 后 增 加 的 配 置 , 用 来 定 义 存 储 的 规 则 类 型 。 所 有 的 规 则 类 型 可 查 看 枚 举 类 : o r g . s p r i n g f r a m e w o r k . c l o u d . a l i b a b a . s e n t i n e l . d a t a s o u r c e . R u l e T y p e , 每 种 规 则 的 定 义 格 式 可 以 通 过 各 枚 举 值 中 定 义 的 规 则 对 象 来 查 看 , 比 如 限 流 规 则 可 查 看 : c o m . a l i b a b a . c s p . s e n t i n e l . s l o t s . b l o c k . f l o w . F l o w R u l e 这 里 对 于 d a t a I d 使 用 了 {spring.application.name}-sentinel spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP spring.cloud.sentinel.datasource.ds.nacos.rule-type=flow spring.cloud.sentinel.transport.dashboard:sentinel dashboard的访问地址,根据上面准备工作中启动的实例配置 spring.cloud.sentinel.datasource.ds.nacos.server-addr:nacos的访问地址,,根据上面准备工作中启动的实例配置 spring.cloud.sentinel.datasource.ds.nacos.groupId:nacos中存储规则的groupId spring.cloud.sentinel.datasource.ds.nacos.dataId:nacos中存储规则的dataId spring.cloud.sentinel.datasource.ds.nacos.rule-type:该参数是spring cloud alibaba升级到0.2.2之后增加的配置,用来定义存储的规则类型。所有的规则类型可查看枚举类:org.springframework.cloud.alibaba.sentinel.datasource.RuleType,每种规则的定义格式可以通过各枚举值中定义的规则对象来查看,比如限流规则可查看:com.alibaba.csp.sentinel.slots.block.flow.FlowRule 这里对于dataId使用了 spring.application.namesentinelspring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULTGROUPspring.cloud.sentinel.datasource.ds.nacos.ruletype=flowspring.cloud.sentinel.transport.dashboardsentineldashboard访spring.cloud.sentinel.datasource.ds.nacos.serveraddrnacos访spring.cloud.sentinel.datasource.ds.nacos.groupIdnacosgroupIdspring.cloud.sentinel.datasource.ds.nacos.dataIdnacosdataIdspring.cloud.sentinel.datasource.ds.nacos.ruletypespringcloudalibaba0.2.2org.springframework.cloud.alibaba.sentinel.datasource.RuleTypecom.alibaba.csp.sentinel.slots.block.flow.FlowRuledataId使{spring.application.name}变量,这样可以根据应用名来区分不同的规则配置。

注意:由于版本迭代关系,Github Wiki中的文档信息不一定适用所有版本。比如:在这里适用的0.2.1版本中,并没有spring.cloud.sentinel.datasource.ds2.nacos.rule-type这个参数。所以,读者在使用的时候,可以通过查看org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePropertiesConfiguration和org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties两个类来分析具体的配置内容,会更为准确。

比如,Nacos存储的具体配置类源码如下:

public class NacosDataSourceProperties extends AbstractDataSourceProperties {

private String serverAddr;
private String groupId;
private String dataId;

// commercialized usage

private String endpoint;
private String namespace;
private String accessKey;
private String secretKey;

}
上面我们使用了前三个属性,后四个属性是阿里云的商业化产品使用的,这里就不具体介绍了,有使用阿里云商业产品的童鞋可以了解一下。

第三步:创建应用主类,并提供一个rest接口,比如:

@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
}

@Slf4j
@RestController
static class TestController {

    @GetMapping("/hello")
    public String hello() {
        return "didispace.com";
    }

}

}
第四步:Nacos中创建限流规则的配置,比如:

upload successful

其中:Data ID、Group就是上面第二步中配置的内容。配置格式选择JSON,并在配置内容中填入下面的内容:

[
{
“resource”: “/hello”,
“limitApp”: “default”,
“grade”: 1,
“count”: 5,
“strategy”: 0,
“controlBehavior”: 0,
“clusterMode”: false
}
]
可以看到上面配置规则是一个数组类型,数组中的每个对象是针对每一个保护资源的配置对象,每个对象中的属性解释如下:

resource:资源名,即限流规则的作用对象
limitApp:流控针对的调用来源,若为 default 则不区分调用来源
grade:限流阈值类型(QPS 或并发线程数);0代表根据并发数量来限流,1代表根据QPS来进行流量控制
count:限流阈值
strategy:调用关系限流策略
controlBehavior:流量控制效果(直接拒绝、Warm Up、匀速排队)
clusterMode:是否为集群模式
这里我们只做简单的配置解释,以便于理解这里的配置作用。实际上这里还有非常多可配置选项和规则,更复杂的配置后面我们单独开一篇来深入学习。

第五步:启动应用。如果一些顺利,可以看到类似下面的日志,代表已经成功从Nacos加载了一条限流规则:

2019-04-16 14:24:42.919 INFO 89484 — [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource ds-sentinel-nacos-datasource start to loadConfig
2019-04-16 14:24:42.938 INFO 89484 — [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource ds-sentinel-nacos-datasource load 1 FlowRule
通过postman或者curl访问几下localhost:8003/hello接口:

$ curl localhost:8003/hello
didispace.com
此时,在Sentinel Dashboard中就可以看到当前我们启动的alibaba-sentinel-datasource-nacos服务。点击左侧菜单中的流控规则,可以看到已经存在一条记录了,具体如下:

upload successful

这条记录就是上面我们在Nacos中配置的限流规则。

深入思考
在完成了上面的整合之后,对于接口流控规则的修改就存在两个地方了:Sentinel控制台、Nacos控制台。

这个时候,需要注意当前版本的Sentinel控制台不具备同步修改Nacos配置的能力,而Nacos由于可以通过在客户端中使用Listener来实现自动更新。所以,在整合了Nacos做规则存储之后,需要知道在下面两个地方修改存在不同的效果:

Sentinel控制台中修改规则:仅存在于服务的内存中,不会修改Nacos中的配置值,重启后恢复原来的值。
Nacos控制台中修改规则:服务的内存中规则会更新,Nacos中持久化规则也会更新,重启后依然保持。
代码示例
本文介绍内容的客户端代码,示例读者可以通过查看下面仓库中的alibaba-sentinel-datasource-nacos项目:

Github:https://github.com/dyc87112/SpringCloud-Learning/
Gitee:https://gitee.com/didispace/SpringCloud-Learning/
本文由 程序猿DD-翟永超 创作,采用 CC BY 3.0 CN协议 进行许可。 可自由转载、引用,但需署名作者且注明文章出处
本文转载自:https://blog.csdn.net/yangbaggio/article/details/106034984

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值