日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1命名规则 sentinel_Spring Cloud Alibaba 整合 Sentinel 流控 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面我們都是直接通過集成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 流控的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。