Sentinel流量防控卫兵
生活随笔
收集整理的這篇文章主要介紹了
Sentinel流量防控卫兵
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Sentinel流量防控衛兵
- 啟動控制臺
- 利用@SentinelResource的低侵入方式
- 探討blockHandler和fallback的區別
也可以直接按照官方的操作https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
啟動控制臺
到官網下載jar包。
并在控制臺啟動,這里說明最好我們的控制臺和客戶端都在一個IP底下,不然會有很多其他繁雜的操作,而且我們的jdk需要時1.8的,不然控制臺啟動會報錯。
下載好后利用控制臺啟動jar包,用我們的官方提供的命令java -jar sentinel-dashboard-1.6.3.jar。
啟動成功
我們接入客戶端
pom文件
啟動類
package com.bfxy.test;import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import java.util.ArrayList; import java.util.List;@SpringBootApplication public class Application {public static void intDegradeRules() {List<DegradeRule> rules = new ArrayList<DegradeRule>();DegradeRule rule = new DegradeRule();// 注意: 我們的規則一定要綁定到對應的資源上,通過資源名稱進行綁定rule.setResource("com.bfxy.test.web.IndexController:degrade");rule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);rule.setCount(2);rules.add(rule);// 規則管理器DegradeRuleManager.loadRules(rules);}public static void intFlowRules() {List<FlowRule> rules = new ArrayList<FlowRule>();FlowRule rule = new FlowRule();// 注意: 我們的規則一定要綁定到對應的資源上,通過資源名稱進行綁定rule.setResource("helloworld");rule.setGrade(RuleConstant.FLOW_GRADE_QPS);rule.setCount(20);rules.add(rule);// 規則管理器FlowRuleManager.loadRules(rules);}public static void main(String[] args) {SpringApplication.run(Application.class, args);intFlowRules();intDegradeRules();System.err.println("規則加載完畢!");}}Controller
package com.bfxy.test.web;import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class IndexController {@RequestMapping("/flow")public String flow() throws InterruptedException {Entry entry = null;try {// 2.1 定義資源名稱entry = SphU.entry("helloworld");// 2.2 執行資源邏輯代碼System.err.println("helloworld: 訪問數據庫");System.err.println("helloworld: 訪問遠程redis");System.err.println("helloworld: 數據庫持久化操作");Thread.sleep(20);} catch (BlockException e) {System.err.println("要訪問的資源被流控了, 執行流控邏輯!");} finally {if(entry != null) {entry.exit();}} return "flow";}private int count = 0;@RequestMapping("/degrade")public String degrade() throws Exception {Entry entry = null;try {// resouceName:String resouceName = "com.bfxy.test.web.IndexController:degrade";// 2.1 定義資源名稱entry = SphU.entry(resouceName);// 2.2 執行資源邏輯代碼count ++;if(count % 2 == 0) {Thread.sleep(100);System.err.println("degrade--> 執行正常 100 ms ");//throw new Exception("degrade--> 拋出異常");} else {Thread.sleep(20);System.err.println("degrade--> 執行正常 20 ms ");}} catch (BlockException e) {System.err.println("要訪問的資源被降級了, 執行降級邏輯!");} finally {if(entry != null) {entry.exit();}} return "degrade";}}啟動時記得帶上jvm參數
在我們的控制臺可以添加各種各樣的規則,helloworld為我們的代碼中定義的資源名
利用@SentinelResource的低侵入方式
flow-service
package com.bfxy.test.service;import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.stereotype.Service;@Service public class FlowService {/*** blockHandler: 流控降級的時候進入的兜底函數* fallback: 拋出異常的時候進入的兜底函數* (1.6.0 之前的版本 fallback 函數只針對降級異常(DegradeException)進行處理,不能針對業務異常進行處理)* @return*/@SentinelResource(value = "com.bfxy.test.service.FlowService:flow",entryType = EntryType.OUT,blockHandler = "flowBlockHandler",fallback = "")public String flow() {System.err.println("----> 正常執行flow方法");return "flow";}public String flowBlockHandler(BlockException ex) {System.err.println("----> 觸發 流控策略:" + ex);return "執行 流控方法";}}Controller
package com.bfxy.test.web;import com.bfxy.test.service.DegradeService; import com.bfxy.test.service.FlowService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class SentinelAnnotationController {@Autowiredprivate FlowService flowService;@RequestMapping("/flow-test")public String flowTest() {return flowService.flow();} }SentinelResourceAspect
package com.bfxy.test;import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class SentinelAspectConfiguration {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}}啟動類不需要加載任何規則。
利用postman發送一個請求,記得如果不先發送一個請求,控制臺是無法獲取信息的。
探討blockHandler和fallback的區別
DegradeService
package com.bfxy.test.service;import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import org.springframework.stereotype.Service;import java.util.concurrent.atomic.AtomicInteger;@Service public class DegradeService {private AtomicInteger count = new AtomicInteger(0);/*** blockHandler: 流控降級的時候進入的兜底函數* fallback: 拋出異常的時候進入的兜底函數* (1.6.0 之前的版本 fallback 函數只針對降級異常(DegradeException)進行處理,不能針對業務異常進行處理)* @return*/@SentinelResource(value = "com.bfxy.test.service.DegradeService:degrade",entryType = EntryType.OUT,blockHandler = "degradeBlockHandler",fallback = "degradeFallback")public String degrade() {System.err.println("----> 正常執行degrade方法");if(count.incrementAndGet() % 3 == 0) {throw new RuntimeException("拋出業務異常");}return "degrade";}public String degradeBlockHandler(BlockException ex) {System.err.println("----> 觸發 降級流控策略:" + ex);return "執行 降級流控方法";}public String degradeFallback(Throwable t) {System.err.println("----> 觸發 異常時的降級策略:" + t);return "執行 異常降級方法";}}Controller
package com.bfxy.test.web;import com.bfxy.test.service.DegradeService; import com.bfxy.test.service.FlowService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class SentinelAnnotationController {@Autowiredprivate DegradeService degradeService;@RequestMapping("/degrade-test")public String degradeTest() {return degradeService.degrade();}}這里利用控制臺給這個資源添加一個流控規則:一秒以內不能超過一個請求。
開啟postman測試,測試結果當我們的執行方法中手動拋出的異常他會降級到fallback指定的方法,當觸發我們的流控規則時會降級到我們的blockHandler指定的方法。
總結
以上是生活随笔為你收集整理的Sentinel流量防控卫兵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode刷的一些杂题
- 下一篇: 阿里云6·18新玩法上线:邀好友送天猫超