解決Sentinel鏈路模式規(guī)則無效問題
前言
如何使用Sentinel鏈路流控規(guī)則?
如何解決鏈路模式規(guī)則不生效?
解決方案
當前項目的版本信息
- SpringBoot 版本2.3.2.RELEASE
- spring-cloud版本Hoxton.SR8
- spring-cloud-alibaba版本2.2.5.RELEASE
1.引入依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-web-servlet</artifactId>
</dependency>2.關閉sentinel的過濾器
(如果不設置或者不關閉,會出現(xiàn)重復統(tǒng)計問題)
spring:
cloud:
sentinel:
filter:
enabled: false3.添加上下文過濾器
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterContextConfig {
@Bean
public FilterRegistrationBean sentinelFilterRegistration() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new CommonFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.addInitParameter(CommonFilter.WEB_CONTEXT_UNIFY, "false");
registrationBean.setName("sentinelFilter");
registrationBean.setOrder(1);
return registrationBean;
}
}4.編寫測試代碼

5.啟動服務器
請求一下上面編輯的兩個接口,查看sentinel-dashboard控制臺,顯示出兩條鏈路

6.編寫鏈路規(guī)則,并保存
下圖表示鏈路/hello1對findById這個資源的訪問為每秒1次。

7.請求/hello1這個接口
頻繁刷新,會出現(xiàn)失敗。而頻繁請求/hello2這條鏈路則不會失敗。



問題
如果不關閉sentinel過濾器會發(fā)生什么?
發(fā)送一次請求,sentinel就會統(tǒng)計兩次請求。

所以請注意關閉sentinel過濾器

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
JPA如何使用nativequery多表關聯(lián)查詢返回自定義實體類
這篇文章主要介紹了JPA如何使用nativequery多表關聯(lián)查詢返回自定義實體類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Reactor 多任務并發(fā)執(zhí)行且結果按順序返回第一個
這篇文章主要介紹了Reactor 多任務并發(fā)執(zhí)行且結果按順序返回第一個,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09
java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法
這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別及使用方法的相關資料,在開發(fā)過程中經常會用到String 這個類進行操作,需要的朋友可以參考下2017-08-08
java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法
這篇文章主要介紹了java使用JDBC動態(tài)創(chuàng)建數(shù)據(jù)表及SQL預處理的方法,涉及JDBC操作數(shù)據(jù)庫的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關實現(xiàn)技巧,需要的朋友可以參考下2017-08-08

