springcloud -- sentinel
阿新 • • 發佈:2021-06-22
寫這篇主要是看網上資料不怎麼全面,防止自己遺忘。。。。
1、官網 (下載地址)
https://github.com/alibaba/Sentinel/releases
2、能幹嘛
解決服務中各種問題:
服務雪崩
服務降級
服務熔斷
服務限流
3、安裝啟動sentinel
進入下載完成的sentinel 路徑
4、springcloud 中 yml配置sentinel
server: port: 8301 spring: application: name: mmren-edu-micro-sentinel-1 cloud: nacos: server-addr: alex.com:8848 sentinel: transport: # 預設為8719,如果發現該埠被佔用,每次嘗試預設埠+1,直到找到未被佔用的埠為止 port: 8719 # sentinel監控地址必須配置 dashboard: localhost:8888
5、測試sentinel ,測試類
@RestController @Slf4j public class RateLimitController { // 測試QPS // 執行緒數的測試 @GetMapping("testA") public String testA() { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }return "測試TestA"; } @GetMapping("testB") public String testB() { log.info("1秒處理一個請求,排隊處理......"); //log.info("TestB執行中....."); return "測試TestB"; } @GetMapping("testD") public String testD() { try { TimeUnit.SECONDS.sleep(1); }catch (InterruptedException e) { e.printStackTrace(); } //log.info("TestB執行中....."); return "測TestD中RT配置"; } @GetMapping("testC") public String testD1() { int i = 10 / 0; //log.info("TestB執行中....."); return "測TestC中異常比例配置"; } @GetMapping("testE") public String testE() { int i = 10 / 0; log.info("測TestE中異常輸配置"); return "測TestE中異常輸配置"; } @GetMapping("testHotkey") @SentinelResource(value = "testHotkey", /*defaultFallback = "hotkeyHandler"*/blockHandler = "handler") public String testHotkey( @RequestParam(value = "p1",required = false) String p1, @RequestParam(value = "p2",required = false) String p2) { return "測試熱點key限流"; } public String handler(String p1, String p2, BlockException e) { return "fdsfdsfdsf " + e.getMessage(); } public String hotkeyHandler() { return "觸發降級方法!"; } }
順便說一下sentinel 是懶載入,啟動完成後需要再訪問才會在介面上顯示