Spring Cloud Alibaba之Sentinel實現(xiàn)熔斷限流功能
微服務中為了防止某個服務出現(xiàn)問題,導致影響整個服務集群無法提供服務的情況,我們在系統(tǒng)訪問量和業(yè)務量高起來了后非常有必要對服務進行熔斷限流處理。 其中熔斷即服務發(fā)生異常時能夠更好的處理;限流是限制每個服務的資源(比如說訪問量)。spring-cloud中很多使用的是Hystrix組件來進行限流的,現(xiàn)在我們這里使用阿里的sentinel來實現(xiàn)熔斷限流功能。
sentinel簡介這個在阿里云有企業(yè)級的商用版本 應用高可用服務 AHAS;現(xiàn)在有免費的入門級可以先體驗下,之后再決定是否使用付費的專業(yè)版或者是自己搭建。
官方文檔地址
Sentinel的github
本文示例代碼
sentinel功能概述 流量控制:將隨機的請求調整為合適的形狀。即限制請求數(shù)量; 熔斷降級:當檢測到調用鏈路中某個資源出現(xiàn)不穩(wěn)定的表現(xiàn),如請求響應時間長或者異常比例升高的時候,則對此資源的調用進行限制,讓請求快速失敗,避免影響到其它的資源而導致級聯(lián)故障。 采用的手段:1.并發(fā)線程數(shù)的限制;2.通過響應時間進行降級 系統(tǒng)負載保護:Sentinel提供系統(tǒng)維度的自適應保護能力。即在系統(tǒng)負載較高時,自動將流量轉發(fā)到其它集群中的機器上去, 使系統(tǒng)的入口流量和系統(tǒng)的負載達到一個平衡,保護系統(tǒng)能力范圍內處理最多的請求。sentinel和Hystrix的區(qū)別 兩者的原則是一致的,都是當一個資源出現(xiàn)問題時,讓其快速失敗,不波及到其它服務。 Hystrix采用的是線程池隔離的方式,優(yōu)點是做到了資源之間的隔離,缺點是增加了線程切換的成本 Sentinel采用的是通過并發(fā)線程的數(shù)量和響應時間來對資源限制。Sentinel規(guī)則Sentinel默認定義如下規(guī)則:
流控規(guī)則
通過QPS或并發(fā)線程數(shù)來做限制,里面的針對來源可以對某個微服務做限制,默認是都限制。
流控模式: 直接:接口達到限流條件,開啟限流; 關聯(lián):當關聯(lián)的資源達到限流條件時,開啟限流(適合做應用讓步) 鏈路:當從某個接口過來的資源達到限流條件時,開啟限流(限制更細致)關于配置規(guī)則:可以直接使用url地址來配置,也可以通過自定義名稱來配置(需要在方法上添加@SentinelResource('order')注解才能達到效果,可以重復)
鏈路限流不生效的問題:由于sentinel基于filter開發(fā)的攔截使用的鏈路收斂的模式,因此需要設置關閉鏈路收斂使鏈路收斂能夠生效,
spring: cloud: sentinel: filter: # 關閉鏈路收斂使鏈路收斂能夠生效 enabled: false
降級規(guī)則
當滿足設置的條件,對服務進行降級。
根據(jù)平均響應時間:當資源的平均響應時間超過閥值(以ms為單位)之后,資源進入準降級狀態(tài)。如果接下來1秒持續(xù)進入的n個請求的RT都持續(xù)超過這個閥值,則在接下來的時間窗口(單位s)之內就會使這個方法進行服務降級。
注意Sentinel默認的最大時間為4900ms,超過這個時間將被默認設置為4900ms;可以通過啟動配置 -Dcsp.sentinel.statistic.max.rt=xxx來修改。
異常降級:通過設置異常數(shù)或者異常比例來進行服務降級。
熱點規(guī)則
必須使用@SentinelResource('order')注解來做標記,將限流做到參數(shù)級別上去,并且可以配置排除參數(shù)值等于某個值時不做限流。
授權規(guī)則
通過配置黑白名單來設置是否允許通過。
自定義來源獲取規(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自定義授權來源獲取規(guī)則 </p>*/@Componentpublic class RequestOriginParserDefinition implements RequestOriginParser { /** * 定義區(qū)分來源的規(guī)則:本質上是通過獲取request域中獲取來源標識,然后交給流控應用來進行匹配處理 * * @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ī)則是從應用級別的入口流量進行控制,從單臺機器的總體Load、RT、入口QPS、CPU使用率和線程數(shù)五個維度來監(jiān)控整個應用數(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 計算得出。設定參考值一般是 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 是設置訪問的端口號;sentinel-dashboard.jar 就是剛剛下載的jar包名稱;為方便使用可以創(chuàng)建一個bat啟動文件,在里面輸入上面的命令行,后面啟動直接點擊這個bat文件即可。
從 Sentinel 1.6.0 起,Sentinel 控制臺引入基本的登錄功能,默認用戶名和密碼都是 sentinel;啟動成功后瀏覽器輸入http://127.0.0.1:9080 即可訪問控制臺。注意這個控制臺不是必須接入的,同時只有你的接口方法被訪問過后控制臺里面才會顯示。
服務中使用添加如下依賴包
<!--由于我們使用的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的相關配置和依賴去掉,然后加入sentinel的依賴即可。
代碼中的使用示例1,如果我們只需對相關的http方法進行限流,直接引入依賴的包即可;下面是我們向對某個方法進行限流,因此使用使用@SentinelResource注解來配置。
@Servicepublic class SentinelDemoServiceImpl implements SentinelDemoService { /** * sentinel 熔斷限流示例1 */ @SentinelResource(value = 'SentinelDemoService#sentinelDemo1', defaultFallback = 'sentinelDemo1Fallback') @Override public String sentinelDemo1() {return 'sentinel 示例1'; } /** * 失敗的時候會調用此方法 */ public String sentinelDemo1Fallback(Throwable t) {if (BlockException.isBlockException(t)) { return 'Blocked by Sentinel: ' + t.getClass().getSimpleName();}return 'Oops, failed: ' + t.getClass().getCanonicalName(); }}
然后在控制臺配置相關的策略規(guī)則即可。
自定義Sentinel的異常返回通過實現(xiàn)BlockExceptionHandler接口來自定義異常返回;注意之前的UrlBlockHandler 視乎已經在新版中移除了。
@Componentpublic class SentinelExceptionHandler implements BlockExceptionHandler { /** * 異常處理 * * @param request 請求 * @param response 響應 * @param eBlockException異常接口,包含Sentinel的五個異常 * FlowException 限流異常 * DegradeException 降級異常 * ParamFlowException 參數(shù)限流異常 * AuthorityException 授權異常 * 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', '授權異常'); 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ī)則推送至客戶端并更新到內存中,接著注冊的寫數(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); // 授權規(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ī)則配置動態(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ù)見官網說明;下面是一個示例:
[ {'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接口,在實際操作中還是建議使用界面化的操作。
關于熔斷降級是如何實現(xiàn)自動調用我們配置的Fallback方法sentinel使用了spring的AOP切面編程功能攔截有@SentinelResource注解的類,具體查看com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect類,在執(zhí)行實際的方法時使用try-catch進行異常捕獲,如果異常是BlockException的時候會調用handleBlockException方法(注意我們也可以配置自己自定義的異常也走這個方法),通過反射執(zhí)行配置的Fallback方法。
到此這篇關于Spring Cloud Alibaba之Sentinel的文章就介紹到這了,更多相關Spring Cloud Alibaba內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
