1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控
前面我們都是直接通過集成sentinel的依賴,通過編碼的方式配置規(guī)則等。對(duì)于集成到Spring Cloud中阿里已經(jīng)有了一套開源框架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
# 文件規(guī)則數(shù)據(jù)源
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
# JSON格式的數(shù)據(jù)
spring.cloud.sentinel.datasource.ds1.file.data-type=json
# 規(guī)則類型
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 回退內(nèi)容定義
public class ExceptionUtil {
public static String handleException(BlockException ex) {
return "扛不住了啊....";
}
}
前面我們使用注解的話都是手動(dòng)配置SentinelResourceAspect類,為什么今天不需要配置SentinelResourceAspect呢?
那是因?yàn)樵趕pring-cloud-alibaba中已經(jīng)默認(rèn)配置好了,代碼在org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration中,代碼如下:
@Bean
@ConditionalOnMissingBean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
2. 整合Apollo持久化規(guī)則
利用spring-cloud-alibaba整合Apollo就比較簡單了,直接通過配置就可以,不需要通過編碼的方式手動(dòng)注冊(cè)動(dòng)態(tài)數(shù)據(jù)源。
2.1 增加Apollo的Maven依賴
com.alibaba.csp
sentinel-datasource-apollo
1.4.1
2.2 數(shù)據(jù)源配置
# Apollo命名空間
spring.cloud.sentinel.datasource.ds4.apollo.namespace-name = application
# 規(guī)則配置Key
spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key = flowRules
# 規(guī)則配置默認(rèn)值
spring.cloud.sentinel.datasource.ds4.apollo.default-flow-rule-value = []
# 規(guī)則類型
spring.cloud.sentinel.datasource.ds4.apollo.rule-type = flow
2.3 Apollo相關(guān)的配置
關(guān)于Apollo的地址,appid等信息可以在配置文件中添加,我們?yōu)榱搜菔痉奖憔瓦€是使用代碼指定的方式。
@SpringBootApplication
public class SentinelApp {
public static void main(String[] args) {
// Apollo 中的應(yīng)用名稱,自己定義的
String appId = "SampleApp";
// Apollo 的地址
String apolloMetaServerAddress = "http://localhost:8080";
System.setProperty("app.id", appId);
System.setProperty("apollo.meta", apolloMetaServerAddress);
// 指定環(huán)境
System.setProperty("env", "DEV");
SpringApplication.run(SentinelApp.class, args);
}
}
2.4 測試
在Apollo中添加限流的規(guī)則即可,比如:
flowRules = [{"grade":1,"count":1,"resource":"hello","controlBehavior":0}]
在org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter中打個(gè)端點(diǎn)調(diào)試下,啟動(dòng)時(shí)或者配置更新時(shí)都會(huì)在里面進(jìn)行規(guī)則的轉(zhuǎn)換。
在這邊遇到了一個(gè)坑跟大家分享一下,最開始我配置了最簡單的規(guī)則,就下面三個(gè)Key
flowRules = [{"grade":1,"count":1,"resource":"hello"}]
如果配置成上面的三個(gè)Key,限流將不會(huì)觸發(fā),后面自己調(diào)試JsonConverter中的代碼才發(fā)現(xiàn)了原因。
有這么一段代碼,是根據(jù)配置中心的json字符串轉(zhuǎn)換成對(duì)應(yīng)的規(guī)則類:
List<AbstractRule> rules = Arrays.asList(convertFlowRule(itemJson),
convertDegradeRule(itemJson), convertSystemRule(itemJson),
convertAuthorityRule(itemJson), convertParamFlowRule(itemJson));
轉(zhuǎn)換完了后會(huì)進(jìn)行過濾,得到一個(gè)最終的List,然后判斷數(shù)量,只有為1的時(shí)候才是正確的,由于我配置上面的規(guī)則,然后得出來的convertRuleList里面數(shù)量為2,這樣就沒法返回正確的規(guī)則。
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));
}
之所有數(shù)量為2是因?yàn)樯厦孓D(zhuǎn)換代碼的convertFlowRule(itemJson)和convertParamFlowRule(itemJson),這兩個(gè)轉(zhuǎn)換的問題,由于我的配置只有三個(gè)key,而這三個(gè)Key又是這兩個(gè)規(guī)則共同的,所以都轉(zhuǎn)換成功了才導(dǎo)致數(shù)量為2。解決辦法就是加一些獨(dú)有的Key,比如controlBehavior。
當(dāng)然這個(gè)問題如果我們對(duì)接了控制臺(tái)的話,通過控制臺(tái)去修改配置中心的值就不會(huì)出現(xiàn)這個(gè)問題了。但這也是在學(xué)習(xí)過程中遇到的一個(gè)問題,還是得通過調(diào)試源碼的方式去發(fā)現(xiàn)問題的原因。
加入星球特權(quán)
1、從錢前端到后端玩轉(zhuǎn)Spring Cloud
2、實(shí)戰(zhàn)分庫分表中間件Sharding-JDBC
3、實(shí)戰(zhàn)分布式任務(wù)調(diào)度框架Elastic Job
4、配置中心Apollo實(shí)戰(zhàn)
5、高并發(fā)解決方案之緩存
6、更多課程等你來解鎖,20+課程
點(diǎn)個(gè)“好看”你懂得!
總結(jié)
以上是生活随笔為你收集整理的1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echarts切换折线图变大_这个月,我
- 下一篇: 一副防蓝光眼镜大概多少钱