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提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...
随机推荐
- 奥展项目笔记05--域名、端口、Nginx相关知识笔记
1.我国的电信运营商是默认封闭80端口的. 中国电信屏蔽ADSL用户80端口 只是做应用服务器的话你可以使用别的端口. 80端口电脑上同时有各种各样的程序在运行,他们都需要借助网络来进行通信.例如,你 ...
- Mysql相关问题-----1045 Access denied for user 'root'@'localhost' (using password: YES)报错
MySQL 连接错误,使用Navicat连接MySQL出现错误:1045 Access denied for user 'root'@'localhost' (using password: YES) ...
- 如何让 FFmpeg 支持异步并行转码、截图等等操作?
直接贴代码了: ffmpegTest02.cs public partial class ffmpegTest02 : FormBase { private static readonly strin ...
- 解锁云原生 AI 技能|在 Kubernetes 上构建机器学习系统
本系列将利用阿里云容器服务,帮助您上手 Kubeflow Pipelines. 介绍 机器学习的工程复杂度,除了来自于常见的软件开发问题外,还和机器学习数据驱动的特点相关.而这就带来了其工作流程链路更 ...
- [IDA] Oops! internal error 40343 occured.
问题描述: 解决方案: 安装 IDA 时,其路径下不要出现中文.
- dedecms5.7的获取本文章的TAG
tag调用标签如下: {dede:tag row='10' getall='1' sort='month'} <li><a href='[field:link/]'>[fiel ...
- 对象数组自定义排序--System.Collections.ArrayList.Sort()
使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...
- SQLServer 跨服务器链接 Access数据库
最近做了一个链接Access的实例,记录一笔. 如果你的Access数据库文件和SQLServer数据库在同一服务器上,可直接在数据库手动创建数据库链接 步骤如下: 打开新建链接,给你的链接起一个顺眼 ...
- [git] your branch is ahead of ... 解决办法
出现该问题表示在次之前已经有X个commite了,执行命令git reset --hard HEAD~X解决其中X表示有多少次提交,此命令的意思是回退到x个commit之前.git reset --h ...
- SimpleTagSupport 获取request、session
开发jsp系统时,我们经常会用到tag来写java的逻辑代码,一般会继承两个类,一个是SimpleTagSupport,另一个是TagSupport,由于TagSupport书写配置比较复杂(我个人才 ...