Spring Cloud Alibaba整合Sentinel流控

栏目: Java · 发布时间: 5年前

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

前面我们都是直接通过集成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流控


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

iOS编程实战

iOS编程实战

[美] Rob Napier、[美] Mugunth Kumar / 美团移动 / 人民邮电出版社 / 2014-9 / 79.00元

本书深入介绍iOS 7新特性和新功能,涵盖iOS 7大部分新增特性,包括新的后台操作、Core Bluetooth、UIKit动力学以及TextKit。另外还介绍了如何处理新的扁平化UI,并新增了一章你可能不知道的“小技巧”。如果读者熟练掌握C和C++,读完本书即可创建性能优异的iPhone、iPad和iPod touch应用。 本书主要内容包括:  iOS 7新特性和新功能概览; ......一起来看看 《iOS编程实战》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具