前面我们都是直接通过集成sentinel的依赖,通过编码的方式配置规则等。对于集成到Spring Cloud中阿里已经有了一套开源框架spring-cloud-alibaba,就是用于将一系列的框架成功的整合到Spring Cloud中。

我这边Spring Cloud的版本是Finchley.SR2,Spring Boot的版本是2.0.6.RELEASE,下面开始集成步骤。

1. 整合步骤

1.1添加Maven依赖

org.springframework.cloud

spring-cloud-starter-alibaba-sentinel

0.2.1.RELEASE

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依赖

com.alibaba.csp

sentinel-datasource-apollo

1.4.1

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。

当然这个问题如果我们对接了控制台的话,通过控制台去修改配置中心的值就不会出现这个问题了。但这也是在学习过程中遇到的一个问题,还是得通过调试源码的方式去发现问题的原因。

加入星球特权

1、从钱前端到后端玩转Spring Cloud

2、实战分库分表中间件Sharding-JDBC

3、实战分布式任务调度框架Elastic Job

4、配置中心Apollo实战

5、高并发解决方案之缓存

6、更多课程等你来解锁,20+课程

点个“好看”你懂得!

1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控相关推荐

  1. Spring Cloud Alibaba:Sentinel 流控规则

    文章目录 1. 前言 2. 阈值类型 2.1 QPS 2.2 线程数 3. 流控模式 3.1 直接 3.2 关联 3.3 链路 4. 流控效果 4.1 快速失败 4.2 Warm Up 4.3 排队等 ...

  2. spring cloud alibaba整合sentinel

    1. 添加依赖 <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-c ...

  3. apollo持久化sentinel_Spring Cloud Alibaba基础教程:Sentinel使用Apollo存储规则

    上一篇我们介绍了如何通过Nacos的配置功能来存储限流规则.Apollo是国内用户非常多的配置中心,所以,今天我们继续说说Spring Cloud Alibaba Sentinel中如何将流控规则存储 ...

  4. sentinel 打包_SpringCloud Alibaba整合Sentinel

    欢迎关注H寻梦人公众号 SpringCloud Alibaba整合Sentinel Sentinel 控制台 1. 概述 Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理. ...

  5. Alibaba Sentinel | 流控规则设置

    文章目录 一.Sentinel概述 强大的优势: 组成部分: 二.微服务集成Sentinel 版本说明 安装Sentinel控制台: 1.docke安装 2.jar安装 启动控制台 微服务集成Sent ...

  6. Sentinel流控规则

    Sentinel流控规则 流控规则基本介绍 名词解释 资源名:唯一名称,默认请求路径 针对来源:Sentinel可以针对调用者进行限流,填写微服务名,默认default(不区分来源) 阈值类型/单机阈 ...

  7. 什么?Sentinel流控规则可以这样玩?

    点赞再看,养成习惯,微信搜索[牧小农]关注我获取更多资讯,风里雨里,小农等你,很高兴能够成为你的朋友. 项目源码地址:公众号回复 sentinel,即可免费获取源码 前言 上一篇文章中,我们讲解了关于 ...

  8. 可视化界面 Sentinel 流控卫兵 限流 熔断 系统保护

    Sentinel 流控卫兵 Sentinel分为两个部分: 核心库(Java客户端):客户端调用通用的应用 控制台(Dashboard):基于springboot开发的,打包后可以直接运行,不需要额外 ...

  9. spring cloud alibaba系列sentinel规则持久化

    "秋来相顾尚飘蓬,未就丹砂愧葛洪. 痛饮狂歌空度日,飞扬跋扈为谁雄" 总结 我上一篇:spring cloud alibaba sentinel 介绍的sentinel可以定义很多 ...

最新文章

  1. python asyncio文件操作_Python asyncio文档阅读摘要
  2. 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程
  3. VS2015 编译输出的*.lib,*.dll,*.exe的区别
  4. uni-app echart 使用mpvueEcharts踩坑与流程
  5. 项目实施管理之系统演示
  6. 关于空白模板插件的使用
  7. 贵安新区构建大数据+物联网+智能制造产业链
  8. idea添加scala环境_Scala篇:Scala环境及IDEA配置
  9. 富士通Fujitsu DPK8400E+ 打印机驱动
  10. Vue 3 Composition API - “ref”和“reactive”
  11. 一花一世界,在微服务中定义边界
  12. IntelliJ idea2017 安装破解
  13. Cesium geojson 区域拉伸高度
  14. Android9.0源代码中替换默认静态壁纸
  15. 弥散张量成像之DTI简介
  16. 一份来自蚂蚁金服大佬的数据库设计总结(纯干货)
  17. 王者荣耀服务器什么时候增加人数,王者荣耀正式服凌晨更新,新增几个细节改动...
  18. xshell常用命令
  19. 餐厅订位短信解决方案
  20. 超级马里奥Demo工程源文件

热门文章

  1. 2021年大数据ELK(八):Elasticsearch安装IK分词器插件
  2. INSTALL_FAILED_INVALID_APK
  3. Kotlin setText 使用
  4. 一位资深Java架构师的晋级心得
  5. 计算机房一般在办公楼建设吗,写字楼大厦机房建设技术方案.doc
  6. Java fork join ForkJoinPool 用法例子
  7. 【C#公共帮助类】10年代码,最全的系统帮助类
  8. inconfont 字体库应用
  9. 十五天精通WCF——第六天 你必须要了解的3种通信模式
  10. pig脚本不需要后缀名(python tempfile模块生成pig脚本临时文件,执行)