Spring Cloud Alibaba 整合 Sentinel 流控_第1张图片
前面我们都是直接通过集成sentinel的依赖,通过编码的方式配置规则等。对于集成到Spring Cloud中阿里已经有了一套开源框架spring-cloud-alibaba,就是用于将一系列的框架成功的整合到Spring Cloud中。
我这边Spring Cloud的版本是Finchley.SR2,Spring Boot的版本是2.0.6.RELEASE,下面开始集成步骤。

1. 整合步骤

1.1添加Maven依赖

1.  
2.      org.springframework.cloud
3.      spring-cloud-starter-alibaba-sentinel
4.      0.2.1.RELEASE
5.  

1.2 增加限流的配置
application.properties

1.  # 文件规则数据源
2.  spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
3.  # JSON格式的数据
4.  spring.cloud.sentinel.datasource.ds1.file.data-type=json
5.  # 规则类型
6.  spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

flowrule.json

1.  [
2.    {
3.      "resource": "hello",
4.      "controlBehavior": 0,
5.      "count": 1,
6.      "grade": 1,
7.      "limitApp": "default",
8.      "strategy": 0
9.    }
10. ]

1.3 @SentinelResource使用

1.  @GetMapping("/test")
2.  @SentinelResource(value="hello",blockHandler="handleException",blockHandlerClass=ExceptionUtil.class)
3.  public String test() {
4.      String result = restTemplate.getForObject("http://localhost:8087/user/name", String.class);
5.      return result;
6.  }

1.4 回退内容定义

1.  public class ExceptionUtil {
2.      public static String handleException(BlockException ex) {
3.          return "扛不住了啊....";
4.      }
5.  }

前面我们使用注解的话都是手动配置SentinelResourceAspect类,为什么今天不需要配置SentinelResourceAspect呢?
那是因为在spring-cloud-alibaba中已经默认配置好了,代码在org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration中,代码如下:

1.  @Bean
2.  @ConditionalOnMissingBean
3.  public SentinelResourceAspect sentinelResourceAspect() {
4.      return new SentinelResourceAspect();
5.  }

2. 整合Apollo持久化规则

利用spring-cloud-alibaba整合Apollo就比较简单了,直接通过配置就可以,不需要通过编码的方式手动注册动态数据源。
2.1 增加Apollo的Maven依赖

1.  
2.      com.alibaba.csp
3.      sentinel-datasource-apollo
4.      1.4.1
5.  

2.2 数据源配置

1.  # Apollo命名空间
2.  spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application
3.  # 规则配置Key
4.  spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = flowRules
5.  # 规则配置默认值
6.  spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = []
7.  # 规则类型
8.  spring.cloud.sentinel.datasource.ds4.apollo.rule-type = flow

2.3 Apollo相关的配置
关于Apollo的地址,appid等信息可以在配置文件中添加,我们为了演示方便就还是使用代码指定的方式。

1.  @SpringBootApplication
2.  public class SentinelApp {
3.      public static void main(String[] args) {
4.          // Apollo 中的应用名称,自己定义的
5.          String appId = "SampleApp";
6.          // Apollo 的地址
7.          String apolloMetaServerAddress = "http://localhost:8080";
8.          System.setProperty("app.id", appId);
9.          System.setProperty("apollo.meta", apolloMetaServerAddress);
10.         // 指定环境
11.         System.setProperty("env", "DEV");
12.         SpringApplication.run(SentinelApp.class, args);
13.     }
14. }

2.4 测试
在Apollo中添加限流的规则即可,比如:
1. flowRules = [{"grade":1,"count":1,"resource":"hello","controlBehavior":0}]
在org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter中打个端点调试下,启动时或者配置更新时都会在里面进行规则的转换。
在这边遇到了一个坑跟大家分享一下,最开始我配置了最简单的规则,就下面三个Key

1. flowRules = [{"grade":1,"count":1,"resource":"hello"}]

如果配置成上面的三个Key,限流将不会触发,后面自己调试JsonConverter中的代码才发现了原因。
有这么一段代码,是根据配置中心的json字符串转换成对应的规则类:

1.  List rules = Arrays.asList(convertFlowRule(itemJson),
2.                          convertDegradeRule(itemJson), convertSystemRule(itemJson),
3.                          convertAuthorityRule(itemJson), convertParamFlowRule(itemJson));

转换完了后会进行过滤,得到一个最终的List,然后判断数量,只有为1的时候才是正确的,由于我配置上面的规则,然后得出来的convertRuleList里面数量为2,这样就没法返回正确的规则。

1.  List convertRuleList = rules.stream()
2.                          .filter(rule -> !ObjectUtils.isEmpty(rule))
3.                          .collect(Collectors.toList());
4.  
5.  if (convertRuleList.size() == 0) {
6.      logger.warn(
7.              "Sentinel JsonConverter can not convert {} to any rules, ignore", itemJson);
8.  }
9.  else if (convertRuleList.size() > 1) {
10.     logger.warn(
11.             "Sentinel JsonConverter convert {} and match multi rules, ignore", itemJson);
12. }
13. else {
14.     ruleList.add(convertRuleList.get(0));
15. }

之所有数量为2是因为上面转换代码的convertFlowRule(itemJson)和convertParamFlowRule(itemJson),这两个转换的问题,由于我的配置只有三个key,而这三个Key又是这两个规则共同的,所以都转换成功了才导致数量为2。解决办法就是加一些独有的Key,比如controlBehavior。
当然这个问题如果我们对接了控制台的话,通过控制台去修改配置中心的值就不会出现这个问题了。但这也是在学习过程中遇到的一个问题,还是得通过调试源码的方式去发现问题的原因。
加入星球特权

1、从钱前端到后端玩转Spring Cloud
2、实战分库分表中间件Sharding-JDBC
3、实战分布式任务调度框架Elastic Job
4、配置中心Apollo实战
5、高并发解决方案之缓存
6、更多课程等你来解锁,20+课程

Spring Cloud Alibaba 整合 Sentinel 流控_第2张图片
尹吉欢
我不差钱啊
钟意作者