1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控

12d4d911d51b2989788c07b899b893c2.png

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

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

加入星球特权

2fdad00149ee108a2baf0c5da3d2325e.png

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

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

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

4、配置中心Apollo实战

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

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

cb507f3601126efcac1ad4c98d878989.png

点个“好看”你懂得!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值