Spring Cloud Alibaba之Sentinel實現(xiàn)熔斷限流功能
微服務(wù)中為了防止某個服務(wù)出現(xiàn)問題,導致影響整個服務(wù)集群無法提供服務(wù)的情況,我們在系統(tǒng)訪問量和業(yè)務(wù)量高起來了后非常有必要對服務(wù)進行熔斷限流處理。 其中熔斷即服務(wù)發(fā)生異常時能夠更好的處理;限流是限制每個服務(wù)的資源(比如說訪問量)。
spring-cloud中很多使用的是Hystrix組件來進行限流的,現(xiàn)在我們這里使用阿里的sentinel來實現(xiàn)熔斷限流功能。
sentinel簡介
這個在阿里云有企業(yè)級的商用版本 應(yīng)用高可用服務(wù) AHAS;現(xiàn)在有免費的入門級可以先體驗下,之后再決定是否使用付費的專業(yè)版或者是自己搭建。
sentinel功能概述
- 流量控制:將隨機的請求調(diào)整為合適的形狀。即限制請求數(shù)量;
- 熔斷降級:當檢測到調(diào)用鏈路中某個資源出現(xiàn)不穩(wěn)定的表現(xiàn),如請求響應(yīng)時間長或者異常比例升高的時候,則對此資源的調(diào)用進行限制,讓請求快速失敗,避免影響到其它的資源而導致級聯(lián)故障。 采用的手段:1.并發(fā)線程數(shù)的限制;2.通過響應(yīng)時間進行降級
- 系統(tǒng)負載保護:Sentinel提供系統(tǒng)維度的自適應(yīng)保護能力。即在系統(tǒng)負載較高時,自動將流量轉(zhuǎn)發(fā)到其它集群中的機器上去, 使系統(tǒng)的入口流量和系統(tǒng)的負載達到一個平衡,保護系統(tǒng)能力范圍內(nèi)處理最多的請求。
sentinel和Hystrix的區(qū)別
- 兩者的原則是一致的,都是當一個資源出現(xiàn)問題時,讓其快速失敗,不波及到其它服務(wù)。
- Hystrix采用的是線程池隔離的方式,優(yōu)點是做到了資源之間的隔離,缺點是增加了線程切換的成本
- Sentinel采用的是通過并發(fā)線程的數(shù)量和響應(yīng)時間來對資源限制。
Sentinel規(guī)則
Sentinel默認定義如下規(guī)則:
流控規(guī)則
通過QPS或并發(fā)線程數(shù)來做限制,里面的針對來源可以對某個微服務(wù)做限制,默認是都限制。
- 流控模式:
- 直接:接口達到限流條件,開啟限流;
- 關(guān)聯(lián):當關(guān)聯(lián)的資源達到限流條件時,開啟限流(適合做應(yīng)用讓步)
- 鏈路:當從某個接口過來的資源達到限流條件時,開啟限流(限制更細致)
關(guān)于配置規(guī)則:可以直接使用url地址來配置,也可以通過自定義名稱來配置(需要在方法上添加@SentinelResource("order")
注解才能達到效果,可以重復(fù))
鏈路限流不生效的問題:由于sentinel基于filter開發(fā)的攔截使用的鏈路收斂的模式,因此需要設(shè)置關(guān)閉鏈路收斂使鏈路收斂能夠生效,
spring: cloud: sentinel: filter: # 關(guān)閉鏈路收斂使鏈路收斂能夠生效 enabled: false
降級規(guī)則
當滿足設(shè)置的條件,對服務(wù)進行降級。
根據(jù)平均響應(yīng)時間:當資源的平均響應(yīng)時間超過閥值(以ms為單位)之后,資源進入準降級狀態(tài)。
如果接下來1秒持續(xù)進入的n個請求的RT都持續(xù)超過這個閥值,則在接下來的時間窗口(單位s)之內(nèi)就會使這個方法進行服務(wù)降級。
注意Sentinel默認的最大時間為4900ms,超過這個時間將被默認設(shè)置為4900ms;可以通過啟動配置 -Dcsp.sentinel.statistic.max.rt=xxx來修改。
異常降級:通過設(shè)置異常數(shù)或者異常比例來進行服務(wù)降級。
熱點規(guī)則
必須使用@SentinelResource("order")
注解來做標記,將限流做到參數(shù)級別上去,并且可以配置排除參數(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域中獲取來源標識,然后交給流控應(yīng)用來進行匹配處理 * * @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)保護規(guī)則是從應(yīng)用級別的入口流量進行控制,從單臺機器的總體Load、RT、入口QPS、CPU使用率和線程數(shù)五個維度來監(jiān)控整個應(yīng)用數(shù)據(jù),讓系統(tǒng)跑到最大吞吐量的同時保證系統(tǒng)穩(wěn)定性。
- Load(僅對 Linux/Unix-like 機器生效):當系統(tǒng) load1 超過閾值,且系統(tǒng)當前的并發(fā)線程數(shù)超過系統(tǒng)容量時才會觸發(fā)系統(tǒng)保護。系統(tǒng)容量由系統(tǒng)的 maxQps * minRt 計算得出。設(shè)定參考值一般是 CPU cores * 2.5。
- RT:當單臺機器上所有入口流量的平均 RT 達到閾值即觸發(fā)系統(tǒng)保護,單位是毫秒。
- 線程數(shù):當單臺機器上所有入口流量的并發(fā)線程數(shù)達到閾值即觸發(fā)系統(tǒng)保護。
- 入口 QPS:當單臺機器上所有入口流量的 QPS 達到閾值即觸發(fā)系統(tǒng)保護。
- CPU使用率:當單臺機器上所有入口流量的 CPU使用率達到閾值即觸發(fā)系統(tǒng)保護。
sentinel的使用
下面我們通過一些簡單的示例來快速了解sentinel的使用。
安裝控制臺界面工具
在Sentinel的Github上下載安裝包https://github.com/alibaba/Sentinel/releases;就是一個jar包直接使用命令啟動即可。
java -Dserver.port=9080 -Dcsp.sentinel.dashboard.server=localhost:9080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
-Dserver.port
是設(shè)置訪問的端口號;
sentinel-dashboard.jar 就是剛剛下載的jar包名稱;
為方便使用可以創(chuàng)建一個bat啟動文件,在里面輸入上面的命令行,后面啟動直接點擊這個bat文件即可。
從 Sentinel 1.6.0 起,Sentinel 控制臺引入基本的登錄功能,默認用戶名和密碼都是 sentinel;啟動成功后瀏覽器輸入http://127.0.0.1:9080 即可訪問控制臺。
注意這個控制臺不是必須接入的,同時只有你的接口方法被訪問過后控制臺里面才會顯示。
服務(wù)中使用
添加如下依賴包
<!--由于我們使用的spring-cloud,因此這里因此 sentinel的集成包來簡化我們的配置 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--sentinel 對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,如果我們只需對相關(guān)的http方法進行限流,直接引入依賴的包即可;下面是我們向?qū)δ硞€方法進行限流,因此使用使用@SentinelResource注解來配置。
@Service public class SentinelDemoServiceImpl implements SentinelDemoService { /** * sentinel 熔斷限流示例1 */ @SentinelResource(value = "SentinelDemoService#sentinelDemo1", defaultFallback = "sentinelDemo1Fallback") @Override public String sentinelDemo1() { return "sentinel 示例1"; } /** * 失敗的時候會調(diào)用此方法 */ public String sentinelDemo1Fallback(Throwable t) { if (BlockException.isBlockException(t)) { return "Blocked by Sentinel: " + t.getClass().getSimpleName(); } return "Oops, failed: " + t.getClass().getCanonicalName(); } }
然后在控制臺配置相關(guān)的策略規(guī)則即可。
自定義Sentinel的異常返回
通過實現(xiàn)BlockExceptionHandler
接口來自定義異常返回;注意之前的UrlBlockHandler
視乎已經(jīng)在新版中移除了。
@Component public class SentinelExceptionHandler implements BlockExceptionHandler { /** * 異常處理 * * @param request 請求 * @param response 響應(yīng) * @param e BlockException異常接口,包含Sentinel的五個異常 * FlowException 限流異常 * DegradeException 降級異常 * ParamFlowException 參數(shù)限流異常 * AuthorityException 授權(quán)異常 * SystemBlockException 系統(tǒng)負載異常 * @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", "降級異常"); 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)負載異常"); responseData.put("code", "C5005"); } response.setContentType("application/json;charset=utf-8"); response.getWriter().write(responseData.toJSONString()); } }
基于文件實現(xiàn)Sentinel規(guī)則的持久化
Sentinel 控制臺通過 API 將規(guī)則推送至客戶端并更新到內(nèi)存中,接著注冊的寫數(shù)據(jù)源會將新的規(guī)則保存到本地的文件中。
編寫一個實現(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); // 降級規(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); // 熱點參數(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實現(xiàn)動態(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)建一個配置文件 dubbo-customer-demo-sentinel.rule,類型為text; 具體配置參數(shù)見官網(wǎng)說明;下面是一個示例:
[ { "resource": "SentinelDemoService#sentinelDemo2", "count": 0, "grade": 1, "limitApp":"default", "strategy":0, "controlBehavior":0, "clusterMode":false } ]
實際使用不建議這樣做,還是建議使用控制臺的方式;因為使用官方提供的集成方式時,nacos的時候會瘋狂的拉取數(shù)據(jù),同時只支持一個規(guī)則的配置;因此要么自己去基于nacos實現(xiàn),要么使用控制臺的方式;
且配置項很多,因此還是建議使用控制臺的方式來實現(xiàn),或者是對接其rest api接口,在實際操作中還是建議使用界面化的操作。
關(guān)于熔斷降級是如何實現(xiàn)自動調(diào)用我們配置的Fallback方法
sentinel使用了spring的AOP切面編程功能攔截有@SentinelResource
注解的類,具體查看com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
類,在執(zhí)行實際的方法時使用try-catch進行異常捕獲,
如果異常是BlockException的時候會調(diào)用handleBlockException方法(注意我們也可以配置自己自定義的異常也走這個方法),通過反射執(zhí)行配置的Fallback方法。
到此這篇關(guān)于Spring Cloud Alibaba之Sentinel的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Data JPA實現(xiàn)動態(tài)條件與范圍查詢實例代碼
本篇文章主要介紹了Spring Data JPA實現(xiàn)動態(tài)條件與范圍查詢實例代碼,非常具有實用價值,需要的朋友可以參考下2017-06-06Maven配置文件settings.xml的實現(xiàn)
Maven是一個用于構(gòu)建和管理Java項目的強大工具,它依賴于設(shè)置文件來配置和管理其行為,其中最重要的之一便是settings.xml文件,本文主要介紹了Maven配置文件settings.xml的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01一文搞懂Spring中@Autowired和@Resource的區(qū)別
@Autowired?和?@Resource?都是?Spring/Spring?Boot?項目中,用來進行依賴注入的注解。它們都提供了將依賴對象注入到當前對象的功能,但二者卻有眾多不同,并且這也是常見的面試題之一,所以我們今天就來盤它2022-08-08springboot?aop配合反射統(tǒng)一簽名驗證實踐
這篇文章主要介紹了springboot?aop配合反射統(tǒng)一簽名驗證實踐,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實現(xiàn)國際化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-10-10Java實戰(zhàn)之實現(xiàn)物流配送系統(tǒng)示例詳解
這篇文章主要介紹了一個java實戰(zhàn)項目:通過java、SSM、JSP、mysql和redis實現(xiàn)一個物流配送系統(tǒng)。文中的示例代碼非常詳細,需要的朋友可以參考一下2021-12-12