Spring Cloud Alibaba之Sentinel實(shí)現(xiàn)熔斷限流功能
微服務(wù)中為了防止某個(gè)服務(wù)出現(xiàn)問題,導(dǎo)致影響整個(gè)服務(wù)集群無法提供服務(wù)的情況,我們?cè)谙到y(tǒng)訪問量和業(yè)務(wù)量高起來了后非常有必要對(duì)服務(wù)進(jìn)行熔斷限流處理。 其中熔斷即服務(wù)發(fā)生異常時(shí)能夠更好的處理;限流是限制每個(gè)服務(wù)的資源(比如說訪問量)。
spring-cloud中很多使用的是Hystrix組件來進(jìn)行限流的,現(xiàn)在我們這里使用阿里的sentinel來實(shí)現(xiàn)熔斷限流功能。
sentinel簡介
這個(gè)在阿里云有企業(yè)級(jí)的商用版本 應(yīng)用高可用服務(wù) AHAS;現(xiàn)在有免費(fèi)的入門級(jí)可以先體驗(yàn)下,之后再?zèng)Q定是否使用付費(fèi)的專業(yè)版或者是自己搭建。
sentinel功能概述
- 流量控制:將隨機(jī)的請(qǐng)求調(diào)整為合適的形狀。即限制請(qǐng)求數(shù)量;
- 熔斷降級(jí):當(dāng)檢測(cè)到調(diào)用鏈路中某個(gè)資源出現(xiàn)不穩(wěn)定的表現(xiàn),如請(qǐng)求響應(yīng)時(shí)間長或者異常比例升高的時(shí)候,則對(duì)此資源的調(diào)用進(jìn)行限制,讓請(qǐng)求快速失敗,避免影響到其它的資源而導(dǎo)致級(jí)聯(lián)故障。 采用的手段:1.并發(fā)線程數(shù)的限制;2.通過響應(yīng)時(shí)間進(jìn)行降級(jí)
- 系統(tǒng)負(fù)載保護(hù):Sentinel提供系統(tǒng)維度的自適應(yīng)保護(hù)能力。即在系統(tǒng)負(fù)載較高時(shí),自動(dòng)將流量轉(zhuǎn)發(fā)到其它集群中的機(jī)器上去, 使系統(tǒng)的入口流量和系統(tǒng)的負(fù)載達(dá)到一個(gè)平衡,保護(hù)系統(tǒng)能力范圍內(nèi)處理最多的請(qǐng)求。
sentinel和Hystrix的區(qū)別
- 兩者的原則是一致的,都是當(dāng)一個(gè)資源出現(xiàn)問題時(shí),讓其快速失敗,不波及到其它服務(wù)。
- Hystrix采用的是線程池隔離的方式,優(yōu)點(diǎn)是做到了資源之間的隔離,缺點(diǎn)是增加了線程切換的成本
- Sentinel采用的是通過并發(fā)線程的數(shù)量和響應(yīng)時(shí)間來對(duì)資源限制。
Sentinel規(guī)則
Sentinel默認(rèn)定義如下規(guī)則:
流控規(guī)則
通過QPS或并發(fā)線程數(shù)來做限制,里面的針對(duì)來源可以對(duì)某個(gè)微服務(wù)做限制,默認(rèn)是都限制。
- 流控模式:
- 直接:接口達(dá)到限流條件,開啟限流;
- 關(guān)聯(lián):當(dāng)關(guān)聯(lián)的資源達(dá)到限流條件時(shí),開啟限流(適合做應(yīng)用讓步)
- 鏈路:當(dāng)從某個(gè)接口過來的資源達(dá)到限流條件時(shí),開啟限流(限制更細(xì)致)
關(guān)于配置規(guī)則:可以直接使用url地址來配置,也可以通過自定義名稱來配置(需要在方法上添加@SentinelResource("order")
注解才能達(dá)到效果,可以重復(fù))
鏈路限流不生效的問題:由于sentinel基于filter開發(fā)的攔截使用的鏈路收斂的模式,因此需要設(shè)置關(guān)閉鏈路收斂使鏈路收斂能夠生效,
spring: cloud: sentinel: filter: # 關(guān)閉鏈路收斂使鏈路收斂能夠生效 enabled: false
降級(jí)規(guī)則
當(dāng)滿足設(shè)置的條件,對(duì)服務(wù)進(jìn)行降級(jí)。
根據(jù)平均響應(yīng)時(shí)間:當(dāng)資源的平均響應(yīng)時(shí)間超過閥值(以ms為單位)之后,資源進(jìn)入準(zhǔn)降級(jí)狀態(tài)。
如果接下來1秒持續(xù)進(jìn)入的n個(gè)請(qǐng)求的RT都持續(xù)超過這個(gè)閥值,則在接下來的時(shí)間窗口(單位s)之內(nèi)就會(huì)使這個(gè)方法進(jìn)行服務(wù)降級(jí)。
注意Sentinel默認(rèn)的最大時(shí)間為4900ms,超過這個(gè)時(shí)間將被默認(rèn)設(shè)置為4900ms;可以通過啟動(dòng)配置 -Dcsp.sentinel.statistic.max.rt=xxx來修改。
異常降級(jí):通過設(shè)置異常數(shù)或者異常比例來進(jìn)行服務(wù)降級(jí)。
熱點(diǎn)規(guī)則
必須使用@SentinelResource("order")
注解來做標(biāo)記,將限流做到參數(shù)級(jí)別上去,并且可以配置排除參數(shù)值等于某個(gè)值時(shí)不做限流。
授權(quán)規(guī)則
通過配置黑白名單來設(shè)置是否允許通過。
自定義來源獲取規(guī)則:
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /** * <p> sentinel自定義授權(quán)來源獲取規(guī)則 </p> */ @Component public class RequestOriginParserDefinition implements RequestOriginParser { /** * 定義區(qū)分來源的規(guī)則:本質(zhì)上是通過獲取request域中獲取來源標(biāo)識(shí),然后交給流控應(yīng)用來進(jìn)行匹配處理 * * @param request request域 * @return 返回區(qū)分來源的值 */ @Override public String parseOrigin(HttpServletRequest request) { String client = request.getHeader("client"); if(StringUtils.isNotBlank(client)){ return "NONE"; } return client; } }
系統(tǒng)規(guī)則
系統(tǒng)保護(hù)規(guī)則是從應(yīng)用級(jí)別的入口流量進(jìn)行控制,從單臺(tái)機(jī)器的總體Load、RT、入口QPS、CPU使用率和線程數(shù)五個(gè)維度來監(jiān)控整個(gè)應(yīng)用數(shù)據(jù),讓系統(tǒng)跑到最大吞吐量的同時(shí)保證系統(tǒng)穩(wěn)定性。
- Load(僅對(duì) Linux/Unix-like 機(jī)器生效):當(dāng)系統(tǒng) load1 超過閾值,且系統(tǒng)當(dāng)前的并發(fā)線程數(shù)超過系統(tǒng)容量時(shí)才會(huì)觸發(fā)系統(tǒng)保護(hù)。系統(tǒng)容量由系統(tǒng)的 maxQps * minRt 計(jì)算得出。設(shè)定參考值一般是 CPU cores * 2.5。
- RT:當(dāng)單臺(tái)機(jī)器上所有入口流量的平均 RT 達(dá)到閾值即觸發(fā)系統(tǒng)保護(hù),單位是毫秒。
- 線程數(shù):當(dāng)單臺(tái)機(jī)器上所有入口流量的并發(fā)線程數(shù)達(dá)到閾值即觸發(fā)系統(tǒng)保護(hù)。
- 入口 QPS:當(dāng)單臺(tái)機(jī)器上所有入口流量的 QPS 達(dá)到閾值即觸發(fā)系統(tǒng)保護(hù)。
- CPU使用率:當(dāng)單臺(tái)機(jī)器上所有入口流量的 CPU使用率達(dá)到閾值即觸發(fā)系統(tǒng)保護(hù)。
sentinel的使用
下面我們通過一些簡單的示例來快速了解sentinel的使用。
安裝控制臺(tái)界面工具
在Sentinel的Github上下載安裝包https://github.com/alibaba/Sentinel/releases;就是一個(gè)jar包直接使用命令啟動(dòng)即可。
java -Dserver.port=9080 -Dcsp.sentinel.dashboard.server=localhost:9080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
-Dserver.port
是設(shè)置訪問的端口號(hào);
sentinel-dashboard.jar 就是剛剛下載的jar包名稱;
為方便使用可以創(chuàng)建一個(gè)bat啟動(dòng)文件,在里面輸入上面的命令行,后面啟動(dòng)直接點(diǎn)擊這個(gè)bat文件即可。
從 Sentinel 1.6.0 起,Sentinel 控制臺(tái)引入基本的登錄功能,默認(rèn)用戶名和密碼都是 sentinel;啟動(dòng)成功后瀏覽器輸入http://127.0.0.1:9080 即可訪問控制臺(tái)。
注意這個(gè)控制臺(tái)不是必須接入的,同時(shí)只有你的接口方法被訪問過后控制臺(tái)里面才會(huì)顯示。
服務(wù)中使用
添加如下依賴包
<!--由于我們使用的spring-cloud,因此這里因此 sentinel的集成包來簡化我們的配置 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--sentinel 對(duì)dubbo的支持--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-apache-dubbo-adapter</artifactId> </dependency>
注意如果沒有使用dubbo那么無需引入sentinel-apache-dubbo-adapter
; 比如之前使用的是feign和Hystrix搭配的,只需要將Hystrix的相關(guān)配置和依賴去掉,然后加入sentinel的依賴即可。
代碼中的使用示例1,如果我們只需對(duì)相關(guān)的http方法進(jìn)行限流,直接引入依賴的包即可;下面是我們向?qū)δ硞€(gè)方法進(jìn)行限流,因此使用使用@SentinelResource注解來配置。
@Service public class SentinelDemoServiceImpl implements SentinelDemoService { /** * sentinel 熔斷限流示例1 */ @SentinelResource(value = "SentinelDemoService#sentinelDemo1", defaultFallback = "sentinelDemo1Fallback") @Override public String sentinelDemo1() { return "sentinel 示例1"; } /** * 失敗的時(shí)候會(huì)調(diào)用此方法 */ public String sentinelDemo1Fallback(Throwable t) { if (BlockException.isBlockException(t)) { return "Blocked by Sentinel: " + t.getClass().getSimpleName(); } return "Oops, failed: " + t.getClass().getCanonicalName(); } }
然后在控制臺(tái)配置相關(guān)的策略規(guī)則即可。
自定義Sentinel的異常返回
通過實(shí)現(xiàn)BlockExceptionHandler
接口來自定義異常返回;注意之前的UrlBlockHandler
視乎已經(jīng)在新版中移除了。
@Component public class SentinelExceptionHandler implements BlockExceptionHandler { /** * 異常處理 * * @param request 請(qǐng)求 * @param response 響應(yīng) * @param e BlockException異常接口,包含Sentinel的五個(gè)異常 * FlowException 限流異常 * DegradeException 降級(jí)異常 * ParamFlowException 參數(shù)限流異常 * AuthorityException 授權(quán)異常 * SystemBlockException 系統(tǒng)負(fù)載異常 * @throws IOException IO異常 */ @Override public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception { JSONObject responseData = new JSONObject(); if (e instanceof FlowException) { responseData.put("message", "限流異常"); responseData.put("code", "C5001"); } else if (e instanceof DegradeException) { responseData.put("message", "降級(jí)異常"); responseData.put("code", "C5002"); } else if (e instanceof ParamFlowException) { responseData.put("message", "參數(shù)限流異常"); responseData.put("code", "C5003"); } else if (e instanceof AuthorityException) { responseData.put("message", "授權(quán)異常"); responseData.put("code", "C5004"); } else if (e instanceof SystemBlockException) { responseData.put("message", "系統(tǒng)負(fù)載異常"); responseData.put("code", "C5005"); } response.setContentType("application/json;charset=utf-8"); response.getWriter().write(responseData.toJSONString()); } }
基于文件實(shí)現(xiàn)Sentinel規(guī)則的持久化
Sentinel 控制臺(tái)通過 API 將規(guī)則推送至客戶端并更新到內(nèi)存中,接著注冊(cè)的寫數(shù)據(jù)源會(huì)將新的規(guī)則保存到本地的文件中。
編寫一個(gè)實(shí)現(xiàn)InitFunc接口的類,在里面定義持久化的方式,這里使用文件
public class FilePersistence implements InitFunc { @Value("spring.application.name") private String applicationName; @Override public void init() throws Exception { String ruleDir = System.getProperty("user.home") + "/sentinel-rules/" + applicationName; String flowRulePath = ruleDir + "/flow-rule.json"; String degradeRulePath = ruleDir + "/degrade-rule.json"; String systemRulePath = ruleDir + "/system-rule.json"; String authorityRulePath = ruleDir + "/authority-rule.json"; String paramFlowRulePath = ruleDir + "/param-flow-rule.json"; this.mkdirIfNotExits(ruleDir); this.createFileIfNotExits(flowRulePath); this.createFileIfNotExits(degradeRulePath); this.createFileIfNotExits(systemRulePath); this.createFileIfNotExits(authorityRulePath); this.createFileIfNotExits(paramFlowRulePath); // 流控規(guī)則 ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>( flowRulePath, flowRuleListParser ); FlowRuleManager.register2Property(flowRuleRDS.getProperty()); WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>( flowRulePath, this::encodeJson ); WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS); // 降級(jí)規(guī)則 ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>( degradeRulePath, degradeRuleListParser ); DegradeRuleManager.register2Property(degradeRuleRDS.getProperty()); WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>( degradeRulePath, this::encodeJson ); WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS); // 系統(tǒng)規(guī)則 ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>( systemRulePath, systemRuleListParser ); SystemRuleManager.register2Property(systemRuleRDS.getProperty()); WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>( systemRulePath, this::encodeJson ); WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS); // 授權(quán)規(guī)則 ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>( authorityRulePath, authorityRuleListParser ); AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty()); WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>( authorityRulePath, this::encodeJson ); WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS); // 熱點(diǎn)參數(shù)規(guī)則 ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>( paramFlowRulePath, paramFlowRuleListParser ); ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty()); WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>( paramFlowRulePath, this::encodeJson ); ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS); } private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject( source, new TypeReference<List<FlowRule>>() { } ); private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject( source, new TypeReference<List<DegradeRule>>() { } ); private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject( source, new TypeReference<List<SystemRule>>() { } ); private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject( source, new TypeReference<List<AuthorityRule>>() { } ); private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject( source, new TypeReference<List<ParamFlowRule>>() { } ); private void mkdirIfNotExits(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { file.mkdirs(); } } private void createFileIfNotExits(String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } } private <T> String encodeJson(T t) { return JSON.toJSONString(t); } }
在resources下創(chuàng)建配置目錄META-INF/services
,然后添加文件 com.alibaba.csp.sentinel.init.InitFunc
;在文件中添加上面寫的配置類的全路徑top.vchar.order.config.FilePersistence
使用Nacos實(shí)現(xiàn)動(dòng)態(tài)規(guī)則配置
添加如下依賴
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
添加如下配置(具體可以參考SentinelProperties
配置類):
spring: cloud: sentinel: datasource: flow: # 配置nacos的 nacos: rule-type: FLOW server-addr: 127.0.0.1:8848 namespace: public groupId: "DEFAULT_GROUP" dataId: dubbo-customer-demo-sentinel.rule username: nacos password: 123456
然后在nacos中創(chuàng)建一個(gè)配置文件 dubbo-customer-demo-sentinel.rule,類型為text; 具體配置參數(shù)見官網(wǎng)說明;下面是一個(gè)示例:
[ { "resource": "SentinelDemoService#sentinelDemo2", "count": 0, "grade": 1, "limitApp":"default", "strategy":0, "controlBehavior":0, "clusterMode":false } ]
實(shí)際使用不建議這樣做,還是建議使用控制臺(tái)的方式;因?yàn)槭褂霉俜教峁┑募煞绞綍r(shí),nacos的時(shí)候會(huì)瘋狂的拉取數(shù)據(jù),同時(shí)只支持一個(gè)規(guī)則的配置;因此要么自己去基于nacos實(shí)現(xiàn),要么使用控制臺(tái)的方式;
且配置項(xiàng)很多,因此還是建議使用控制臺(tái)的方式來實(shí)現(xiàn),或者是對(duì)接其rest api接口,在實(shí)際操作中還是建議使用界面化的操作。
關(guān)于熔斷降級(jí)是如何實(shí)現(xiàn)自動(dòng)調(diào)用我們配置的Fallback方法
sentinel使用了spring的AOP切面編程功能攔截有@SentinelResource
注解的類,具體查看com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
類,在執(zhí)行實(shí)際的方法時(shí)使用try-catch進(jìn)行異常捕獲,
如果異常是BlockException的時(shí)候會(huì)調(diào)用handleBlockException方法(注意我們也可以配置自己自定義的異常也走這個(gè)方法),通過反射執(zhí)行配置的Fallback方法。
到此這篇關(guān)于Spring Cloud Alibaba之Sentinel的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring?Cloud?Alibaba實(shí)現(xiàn)服務(wù)的無損下線功能(案例講解)
- Spring cloud alibaba之Gateway網(wǎng)關(guān)功能特征詳解
- Alibaba?SpringCloud集成Nacos、openFeign實(shí)現(xiàn)負(fù)載均衡的解決方案
- 聊聊SpringCloud和SpringCloudAlibaba的區(qū)別
- 使用SpringCloudAlibaba整合Dubbo
- SpringCloud Alibaba 基本開發(fā)框架搭建過程
- 微服務(wù)Spring?Cloud?Alibaba?的介紹及主要功能詳解
相關(guān)文章
Spring Data JPA實(shí)現(xiàn)動(dòng)態(tài)條件與范圍查詢實(shí)例代碼
本篇文章主要介紹了Spring Data JPA實(shí)現(xiàn)動(dòng)態(tài)條件與范圍查詢實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-06-06Maven配置文件settings.xml的實(shí)現(xiàn)
Maven是一個(gè)用于構(gòu)建和管理Java項(xiàng)目的強(qiáng)大工具,它依賴于設(shè)置文件來配置和管理其行為,其中最重要的之一便是settings.xml文件,本文主要介紹了Maven配置文件settings.xml的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01一文搞懂Spring中@Autowired和@Resource的區(qū)別
@Autowired?和?@Resource?都是?Spring/Spring?Boot?項(xiàng)目中,用來進(jìn)行依賴注入的注解。它們都提供了將依賴對(duì)象注入到當(dāng)前對(duì)象的功能,但二者卻有眾多不同,并且這也是常見的面試題之一,所以我們今天就來盤它2022-08-08springboot?aop配合反射統(tǒng)一簽名驗(yàn)證實(shí)踐
這篇文章主要介紹了springboot?aop配合反射統(tǒng)一簽名驗(yàn)證實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Boot Thymeleaf實(shí)現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實(shí)現(xiàn)國際化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Java實(shí)戰(zhàn)之實(shí)現(xiàn)物流配送系統(tǒng)示例詳解
這篇文章主要介紹了一個(gè)java實(shí)戰(zhàn)項(xiàng)目:通過java、SSM、JSP、mysql和redis實(shí)現(xiàn)一個(gè)物流配送系統(tǒng)。文中的示例代碼非常詳細(xì),需要的朋友可以參考一下2021-12-12