Spring Cloud Alibaba整合Sentinel流控
前面我们都是直接通过集成sentinel的依赖,通过编码的方式配置规则等。对于集成到Spring Cloud中阿里已经有了一套开源框架spring-cloud-alibaba,就是用于将一系列的框架成功的整合到Spring Cloud中。
我这边Spring Cloud的版本是Finchley.SR2,Spring Boot的版本是2.0.6.RELEASE,下面开始集成步骤。
1. 整合步骤
1.1添加Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
1.2 增加限流的配置
application.properties
# 文件规则数据源
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
# JSON格式的数据
spring.cloud.sentinel.datasource.ds1.file.data-type=json
# 规则类型
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
flowrule.json
[
{
"resource": "hello",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
1.3 @SentinelResource使用
@GetMapping("/test")
@SentinelResource(value="hello",blockHandler="handleException",blockHandlerClass=ExceptionUtil.class)
public String test() {
String result = restTemplate.getForObject("http://localhost:8087/user/name", String.class);
return result;
}
1.4 回退内容定义
public class ExceptionUtil {
public static String handleException(BlockException ex) {
return "扛不住了啊....";
}
}
前面我们使用注解的话都是手动配置SentinelResourceAspect类,为什么今天不需要配置SentinelResourceAspect呢?
那是因为在spring-cloud-alibaba中已经默认配置好了,代码在org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration中,代码如下:
@Bean
@ConditionalOnMissingBean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
2. 整合Apollo持久化规则
利用spring-cloud-alibaba整合Apollo就比较简单了,直接通过配置就可以,不需要通过编码的方式手动注册动态数据源。
2.1 增加Apollo的Maven依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-apollo</artifactId>
<version>1.4.1</version>
</dependency>
2.2 数据源配置
# Apollo命名空间
spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application
# 规则配置Key
spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = flowRules
# 规则配置默认值
spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = []
# 规则类型
spring.cloud.sentinel.datasource.ds4.apollo.rule-type = flow
2.3 Apollo相关的配置
关于Apollo的地址,appid等信息可以在配置文件中添加,我们为了演示方便就还是使用代码指定的方式。
@SpringBootApplication
public class SentinelApp {
public static void main(String[] args) {
// Apollo 中的应用名称,自己定义的
String appId = "SampleApp";
// Apollo 的地址
String apolloMetaServerAddress = "http://localhost:8080";
System.setProperty("app.id", appId);
System.setProperty("apollo.meta", apolloMetaServerAddress);
// 指定环境
System.setProperty("env", "DEV");
SpringApplication.run(SentinelApp.class, args);
}
}
2.4 测试
在Apollo中添加限流的规则即可,比如:
flowRules = [{"grade":1,"count":1,"resource":"hello","controlBehavior":0}]
在org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter中打个端点调试下,启动时或者配置更新时都会在里面进行规则的转换。
在这边遇到了一个坑跟大家分享一下,最开始我配置了最简单的规则,就下面三个Key
flowRules = [{"grade":1,"count":1,"resource":"hello"}]
如果配置成上面的三个Key,限流将不会触发,后面自己调试JsonConverter中的代码才发现了原因。
有这么一段代码,是根据配置中心的json字符串转换成对应的规则类:
List<AbstractRule> rules = Arrays.asList(convertFlowRule(itemJson),
convertDegradeRule(itemJson), convertSystemRule(itemJson),
convertAuthorityRule(itemJson), convertParamFlowRule(itemJson));
转换完了后会进行过滤,得到一个最终的List,然后判断数量,只有为1的时候才是正确的,由于我配置上面的规则,然后得出来的convertRuleList里面数量为2,这样就没法返回正确的规则。
List<AbstractRule> convertRuleList = rules.stream()
.filter(rule -> !ObjectUtils.isEmpty(rule))
.collect(Collectors.toList());
if (convertRuleList.size() == 0) {
logger.warn(
"Sentinel JsonConverter can not convert {} to any rules, ignore", itemJson);
}
else if (convertRuleList.size() > 1) {
logger.warn(
"Sentinel JsonConverter convert {} and match multi rules, ignore", itemJson);
}
else {
ruleList.add(convertRuleList.get(0));
}
之所有数量为2是因为上面转换代码的convertFlowRule(itemJson)和convertParamFlowRule(itemJson),这两个转换的问题,由于我的配置只有三个key,而这三个Key又是这两个规则共同的,所以都转换成功了才导致数量为2。解决办法就是加一些独有的Key,比如controlBehavior。
当然这个问题如果我们对接了控制台的话,通过控制台去修改配置中心的值就不会出现这个问题了。但这也是在学习过程中遇到的一个问题,还是得通过调试源码的方式去发现问题的原因。
欢迎加入我的知识星球,一起交流技术,免费学习猿天地的课程(http://cxytiandi.com/course)
PS:目前星球中正在星主的带领下组队学习Sentinel,等你哦!


Spring Cloud Alibaba整合Sentinel流控的更多相关文章
- Spring Cloud Alibaba整合Sentinel
Spring Cloud Alibaba 整合 Sentinel 一.需求 二.实现步骤 1.下载 sentinel dashboard 2.服务提供者和消费者引入sentinel依赖 3.配置控制台 ...
- Spring Cloud Alibaba(10)---Sentinel控制台搭建+整合SpringCloudAlibaba
上一篇博客讲了Sentinel一些概念性的东西 Spring Cloud Alibaba(9)---Sentinel概述 这篇博客主要讲 Sentinel控制台搭建,和 整合SpringCloudAl ...
- Spring Cloud Alibaba(11)---Sentinel+Nacos持久化
Sentinel+Nacos持久化 有关Sentinel之前有写过两篇 Spring Cloud Alibaba(9)---Sentinel概述 Spring Cloud Alibaba(10)--- ...
- Spring Cloud alibaba网关 sentinel zuul 四 限流熔断
spring cloud alibaba 集成了 他内部开源的 Sentinel 熔断限流框架 Sentinel 介绍 官方网址 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentine ...
- Spring Cloud Alibaba 使用Sentinel实现接口限流
Sentinel是什么 Sentinel的官方标题是:分布式系统的流量防卫兵.从名字上来看,很容易就能猜到它是用来作服务稳定性保障的.对于服务稳定性保障组件,如果熟悉Spring Cloud的用户,第 ...
- Spring Cloud Alibaba 之 Sentinel 限流规则和控制台实例
这一节我们通过一个简单的实例,学习Sentinel的基本应用. 一.Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则. ...
- spring cloud gateway整合sentinel作网关限流
说明: sentinel可以作为各微服务的限流,也可以作为gateway网关的限流组件. spring cloud gateway有限流功能,但此处用sentinel来作为替待. 说明:sentine ...
- Spring Cloud Alibaba 整合 Nacos 实现服务配置中心
在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...
- Spring Cloud Alibaba:Sentinel实现熔断与限流
一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...
随机推荐
- mysql启动报错:Failed to start LSB: start and stop MySQL
报错信息: [root@youxx- bin]# service mysql status Redirecting to /bin/systemctl status mysql.service ¡ñ ...
- 转 让FPGA替代GPU的6大顾虑,你确定不看看吗?
最近FPGA又频频被各AI领域的巨头看好,比如微软.百度.科大讯飞都对FPGA应用前景有所期待.那么如果让你选择FPGA作为AI计算系统的主力军,你会有什么样的顾虑? 这几天,已经退役的AlphaGo ...
- Dev控件使用CheckedListBoxControl获取items.count为0 的解决方法
CheckedListBoxControl,我使用DataSource属性,给其绑定了一个List对象.界面显示都挺正常的,当若干个项的复选框被选中的后,它的checkedListBoxControl ...
- 解决 Windows Server 2008 R2 上 Windows Update 无法失败,提示 8024402F
1.同步服务器时间 2.打开 services.msc,停止 Windows Update Service 3.手动下载 KB3138615 补丁:https://support.microsoft. ...
- python3的hashlib库sha256、pbkdf2_hmac、blake2b基本用法
hashlib.sha256: import hashlib x = hashlib.sha256()x.update(b"asd")print("x_1 = " ...
- jQuery-ready与load
// ready 在DOM加载完成时运行的代码 $(document).ready(function(){ // 在这里写代码... }) // 可以简写为 $(function(){ // 在这 ...
- 前端技术扫盲-rem和px以及em关系
首先rem,em和px都是长度单位,下面单独介绍各个单位的概念: px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.PC端用比较多的. em是相对长度单位.相对于当前对象内 ...
- 从未被Google过 #NerverBeenGoogled
我相信大家都用Google搜索互联网上的东西,Google会跟踪你搜索的所有内容,但是你或许不知道,他们也记录着从未被Google过的内容.我有个清单,这些是有史以来从未被Google过的一些东西1. ...
- Scrum冲刺第一篇
一.各个成员在 Alpha 阶段认领的任务 负责人和协作者 任务内容 陈嘉欣 设计编码规范 邓镇港 UI设计 肖烈涛 数据库设计 林德泽 设计测试计划 余晓东 用户注册登陆验证模块 陈嘉欣 余晓东 林 ...
- Vue实战狗尾草博客管理系统第一章
Vue实战狗尾草博客后台管理系统第一章 这里准备采用的技术栈为:vue全家桶+element-ui 这里因为是后台管理系统,没有做SSR的必要.所以这里就采用前后端分离来昨晚这个项目~ 项目搭建 vu ...