您好,登錄后才能下訂單哦!
這篇文章主要講解了“Sentinel限流的使用方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Sentinel限流的使用方法”吧!
前言: Sentinel 是由alibaba出品的,針對于系統負載保護的組件,其有豐富的流量防護手段和多樣化的流量整型策略而被廣大使用。 以下是轉自Sentinel官方的介紹: 隨著微服務的流行,服務和服務之間的穩定性變得越來越重要。Sentinel 是面向分布式服務架構的輕量級流量控制組件,主要以流量為切入點,從限流、流量整形、熔斷降級、系統負載保護等多個維度來幫助您保障微服務的穩定性。 Sentinel-wiki Sentinel GitHub wiki 有興趣的童鞋可以去了解。
引入sentinel-core
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>1.6.3</version> </dependency>
加載規則信息
private static final String RESOURCE_NAME = "hello"; /** * 加載限流規則 */ public static void loadRules(){ List<FlowRule> rules = new ArrayList<FlowRule>(); FlowRule flowRule = new FlowRule(); flowRule.setResource(RESOURCE_NAME); //資源名 flowRule.setLimitApp("default");//default 代表對所有應用生效 flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); //限流閾值類型 flowRule.setCount(10); //閾值 rules.add(flowRule); FlowRuleManager.loadRules(rules); }
添加資源入口處理限流異常
/** * 執行方法 * @param hello */ public static void hello(String hello){ Entry entry = null; try { entry = SphU.entry(RESOURCE_NAME); log.info("args hello value is {}",hello); }catch (Exception e){ if(FlowException.isBlockException(e)){ log.error("block resourceName: {}",RESOURCE_NAME); } }finally { if(entry !=null){ entry.exit(); } } }
模擬調用測試
public static void main(String[] args) { loadRules(); for(int i=0;i<20;i++){ hello("hello"+i); } }
運行記錄
15:26:08.453 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello0 15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello1 15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello2 15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello3 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello4 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello5 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello6 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello7 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello8 15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello9 15:26:08.492 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.492 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello 15:26:08.494 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
感謝各位的閱讀,以上就是“Sentinel限流的使用方法”的內容了,經過本文的學習后,相信大家對Sentinel限流的使用方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。