- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Spring Cloud Alibaba之Sentinel實(shí)現熔斷限流功能
微服務(wù)中為了防止某個(gè)服務(wù)出現問(wèn)題,導致影響整個(gè)服務(wù)集群無(wú)法提供服務(wù)的情況,我們在系統訪(fǎng)問(wèn)量和業(yè)務(wù)量高起來(lái)了后非常有必要對服務(wù)進(jìn)行熔斷限流處理。 其中熔斷即服務(wù)發(fā)生異常時(shí)能夠更好的處理;限流是限制每個(gè)服務(wù)的資源(比如說(shuō)訪(fǎng)問(wèn)量)。
spring-cloud中很多使用的是Hystrix組件來(lái)進(jìn)行限流的,現在我們這里使用阿里的sentinel來(lái)實(shí)現熔斷限流功能。
這個(gè)在阿里云有企業(yè)級的商用版本 ;現在有免費的入門(mén)級可以先體驗下,之后再決定是否使用付費的專(zhuān)業(yè)版或者是自己搭建。
Sentinel默認定義如下規則:
流控規則
通過(guò)QPS或并發(fā)線(xiàn)程數來(lái)做限制,里面的針對來(lái)源可以對某個(gè)微服務(wù)做限制,默認是都限制。
關(guān)于配置規則:可以直接使用url地址來(lái)配置,也可以通過(guò)自定義名稱(chēng)來(lái)配置(需要在方法上添加@SentinelResource("order")
注解才能達到效果,可以重復)
鏈路限流不生效的問(wèn)題:由于sentinel基于filter開(kāi)發(fā)的攔截使用的鏈路收斂的模式,因此需要設置關(guān)閉鏈路收斂使鏈路收斂能夠生效,
spring: cloud: sentinel: filter: # 關(guān)閉鏈路收斂使鏈路收斂能夠生效 enabled: false
降級規則
當滿(mǎn)足設置的條件,對服務(wù)進(jìn)行降級。
根據平均響應時(shí)間:當資源的平均響應時(shí)間超過(guò)閥值(以ms為單位)之后,資源進(jìn)入準降級狀態(tài)。
如果接下來(lái)1秒持續進(jìn)入的n個(gè)請求的RT都持續超過(guò)這個(gè)閥值,則在接下來(lái)的時(shí)間窗口(單位s)之內就會(huì )使這個(gè)方法進(jìn)行服務(wù)降級。
注意Sentinel默認的最大時(shí)間為4900ms,超過(guò)這個(gè)時(shí)間將被默認設置為4900ms;可以通過(guò)啟動(dòng)配置 -Dcsp.sentinel.statistic.max.rt=xxx來(lái)修改。
異常降級:通過(guò)設置異常數或者異常比例來(lái)進(jìn)行服務(wù)降級。
熱點(diǎn)規則
必須使用@SentinelResource("order")
注解來(lái)做標記,將限流做到參數級別上去,并且可以配置排除參數值等于某個(gè)值時(shí)不做限流。
授權規則
通過(guò)配置黑白名單來(lái)設置是否允許通過(guò)。
自定義來(lái)源獲取規則:
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自定義授權來(lái)源獲取規則 </p> */ @Component public class RequestOriginParserDefinition implements RequestOriginParser { /** * 定義區分來(lái)源的規則:本質(zhì)上是通過(guò)獲取request域中獲取來(lái)源標識,然后交給流控應用來(lái)進(jìn)行匹配處理 * * @param request request域 * @return 返回區分來(lái)源的值 */ @Override public String parseOrigin(HttpServletRequest request) { String client = request.getHeader("client"); if(StringUtils.isNotBlank(client)){ return "NONE"; } return client; } }
系統規則
系統保護規則是從應用級別的入口流量進(jìn)行控制,從單臺機器的總體Load、RT、入口QPS、CPU使用率和線(xiàn)程數五個(gè)維度來(lái)監控整個(gè)應用數據,讓系統跑到最大吞吐量的同時(shí)保證系統穩定性。
下面我們通過(guò)一些簡(jiǎn)單的示例來(lái)快速了解sentinel的使用。
在Sentinel的Github上下載安裝包;就是一個(gè)jar包直接使用命令啟動(dòng)即可。
java -Dserver.port=9080 -Dcsp.sentinel.dashboard.server=localhost:9080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
-Dserver.port
是設置訪(fǎng)問(wèn)的端口號;
sentinel-dashboard.jar 就是剛剛下載的jar包名稱(chēng);
為方便使用可以創(chuàng )建一個(gè)bat啟動(dòng)文件,在里面輸入上面的命令行,后面啟動(dòng)直接點(diǎn)擊這個(gè)bat文件即可。
從 Sentinel 1.6.0 起,Sentinel 控制臺引入基本的登錄功能,默認用戶(hù)名和密碼都是 sentinel;啟動(dòng)成功后瀏覽器輸入 即可訪(fǎng)問(wèn)控制臺。
注意這個(gè)控制臺不是必須接入的,同時(shí)只有你的接口方法被訪(fǎng)問(wèn)過(guò)后控制臺里面才會(huì )顯示。
添加如下依賴(lài)包
<!--由于我們使用的spring-cloud,因此這里因此 sentinel的集成包來(lái)簡(jiǎn)化我們的配置 --> <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>
注意如果沒(méi)有使用dubbo那么無(wú)需引入sentinel-apache-dubbo-adapter
; 比如之前使用的是feign和Hystrix搭配的,只需要將Hystrix的相關(guān)配置和依賴(lài)去掉,然后加入sentinel的依賴(lài)即可。
代碼中的使用示例1,如果我們只需對相關(guān)的http方法進(jìn)行限流,直接引入依賴(lài)的包即可;下面是我們向對某個(gè)方法進(jìn)行限流,因此使用使用@SentinelResource注解來(lái)配置。
@Service public class SentinelDemoServiceImpl implements SentinelDemoService { /** * sentinel 熔斷限流示例1 */ @SentinelResource(value = "SentinelDemoService#sentinelDemo1", defaultFallback = "sentinelDemo1Fallback") @Override public String sentinelDemo1() { return "sentinel 示例1"; } /** * 失敗的時(shí)候會(huì )調用此方法 */ 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ò)實(shí)現BlockExceptionHandler
接口來(lái)自定義異常返回;注意之前的UrlBlockHandler
視乎已經(jīng)在新版中移除了。
@Component public class SentinelExceptionHandler implements BlockExceptionHandler { /** * 異常處理 * * @param request 請求 * @param response 響應 * @param e BlockException異常接口,包含Sentinel的五個(gè)異常 * FlowException 限流異常 * DegradeException 降級異常 * ParamFlowException 參數限流異常 * AuthorityException 授權異常 * SystemBlockException 系統負載異常 * @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", "參數限流異常"); responseData.put("code", "C5003"); } else if (e instanceof AuthorityException) { responseData.put("message", "授權異常"); responseData.put("code", "C5004"); } else if (e instanceof SystemBlockException) { responseData.put("message", "系統負載異常"); responseData.put("code", "C5005"); } response.setContentType("application/json;charset=utf-8"); response.getWriter().write(responseData.toJSONString()); } }
Sentinel 控制臺通過(guò) API 將規則推送至客戶(hù)端并更新到內存中,接著(zhù)注冊的寫(xiě)數據源會(huì )將新的規則保存到本地的文件中。
編寫(xiě)一個(gè)實(shí)現InitFunc接口的類(lèi),在里面定義持久化的方式,這里使用文件
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); // 流控規則 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); // 降級規則 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); // 系統規則 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); // 授權規則 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)參數規則 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
;在文件中添加上面寫(xiě)的配置類(lèi)的全路徑top.vchar.order.config.FilePersistence
添加如下依賴(lài)
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
添加如下配置(具體可以參考SentinelProperties
配置類(lèi)):
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,類(lèi)型為text; 具體配置參數見(jiàn)官網(wǎng)說(shuō)明;下面是一個(gè)示例:
[ { "resource": "SentinelDemoService#sentinelDemo2", "count": 0, "grade": 1, "limitApp":"default", "strategy":0, "controlBehavior":0, "clusterMode":false } ]
實(shí)際使用不建議這樣做,還是建議使用控制臺的方式;因為使用官方提供的集成方式時(shí),nacos的時(shí)候會(huì )瘋狂的拉取數據,同時(shí)只支持一個(gè)規則的配置;因此要么自己去基于nacos實(shí)現,要么使用控制臺的方式;
且配置項很多,因此還是建議使用控制臺的方式來(lái)實(shí)現,或者是對接其rest api接口,在實(shí)際操作中還是建議使用界面化的操作。
sentinel使用了spring的AOP切面編程功能攔截有@SentinelResource
注解的類(lèi),具體查看com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect
類(lèi),在執行實(shí)際的方法時(shí)使用try-catch進(jìn)行異常捕獲,
如果異常是BlockException的時(shí)候會(huì )調用handleBlockException方法(注意我們也可以配置自己自定義的異常也走這個(gè)方法),通過(guò)反射執行配置的Fallback方法。
到此這篇關(guān)于Spring Cloud Alibaba之Sentinel的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2