解決Sentinel鏈路模式規(guī)則無效問題
前言
如何使用Sentinel鏈路流控規(guī)則?
如何解決鏈路模式規(guī)則不生效?
解決方案
當(dāng)前項(xiàng)目的版本信息
- 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.關(guān)閉sentinel的過濾器
(如果不設(shè)置或者不關(guān)閉,會出現(xiàn)重復(fù)統(tǒng)計(jì)問題)
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.啟動(dòng)服務(wù)器
請求一下上面編輯的兩個(gè)接口,查看sentinel-dashboard控制臺,顯示出兩條鏈路

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

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



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

所以請注意關(guān)閉sentinel過濾器

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級聯(lián)動(dòng)效果(兩種實(shí)現(xiàn)方式)
這篇文章主要介紹了MyBatis之自查詢使用遞歸實(shí)現(xiàn) N級聯(lián)動(dòng)效果,本文給大家分享兩種實(shí)現(xiàn)方式,需要的的朋友參考下吧2017-07-07
jdk-logging?log4j?logback日志系統(tǒng)實(shí)現(xiàn)機(jī)制原理介紹
這篇文章主要介紹了jdk-logging、log4j、logback日志介紹以及三個(gè)日志系統(tǒng)的實(shí)現(xiàn)機(jī)制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Spring boot集成Kafka消息中間件代碼實(shí)例
這篇文章主要介紹了Spring boot集成Kafka消息中間件代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Spring Cloud多個(gè)微服務(wù)之間調(diào)用代碼實(shí)例
這篇文章主要介紹了Spring Cloud多個(gè)微服務(wù)之間調(diào)用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
使用JDBC實(shí)現(xiàn)數(shù)據(jù)訪問對象層(DAO)代碼示例
這篇文章主要介紹了使用JDBC實(shí)現(xiàn)數(shù)據(jù)訪問對象層(DAO)代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
Spring Security和Shiro的相同點(diǎn)與不同點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于Spring Security和Shiro的相同不同點(diǎn)整理,需要的朋友們可以參考下。2020-02-02
spring boot中各個(gè)版本的redis配置問題詳析
這篇文章主要給大家介紹了關(guān)于spring boot中各個(gè)版本的redis配置問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
SpringCloud Zuul實(shí)現(xiàn)負(fù)載均衡和熔斷機(jī)制方式
這篇文章主要介紹了SpringCloud Zuul實(shí)現(xiàn)負(fù)載均衡和熔斷機(jī)制方式,具有很好的參考價(jià)值,希望對大家有所幫助。2021-07-07

