解決Sentinel鏈路模式規(guī)則無效問題
前言
如何使用Sentinel鏈路流控規(guī)則?
如何解決鏈路模式規(guī)則不生效?
解決方案
當(dā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)計問題)
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.啟動服務(wù)器
請求一下上面編輯的兩個接口,查看sentinel-dashboard控制臺,顯示出兩條鏈路

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

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



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

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

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

