流控组件Sentinel核心注解@SentinelResource中的参数fallback和blockHandler的使用方式
生活随笔
收集整理的這篇文章主要介紹了
流控组件Sentinel核心注解@SentinelResource中的参数fallback和blockHandler的使用方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
fallback顧名思義當Java程序運行發生錯誤時,由該參數定義的方法進行處理
@GetMapping(value = "/consumer/{id}")@SentinelResource(value = "consumer", fallback = "handlerFallback")public ResponseEntity<String> consumerData(@PathVariable("id")Integer id) {ResponseEntity<String> entity = remoteService.getData(id);if (id <= 0) {throw new IllegalArgumentException("非法參數異常!");} else if (entity.getData() == null) {throw new NullPointerException("無響應數據,空指針異常!");}return entity;}public ResponseEntity<String> handlerFallback(Integer id, Throwable e) {return new ResponseEntity<>(443, "程序處理異常,開始執行fallback數據-->" +e.getMessage());}blockHandler這個也很好明白,也就是當API違背控制臺設置的閾值時,執行該參數定義的方法進行處理;說白了就是限流之后的處理
@GetMapping(value = "/consumer/{id}")@SentinelResource(value = "consumer",blockHandler = "blockHandler")public ResponseEntity<String> consumerData(@PathVariable("id")Integer id) {ResponseEntity<String> entity = remoteService.getData(id);if (id <= 0) {throw new IllegalArgumentException("非法參數異常!");} else if (entity.getData() == null) {throw new NullPointerException("無響應數據,空指針異常!");}return entity;}public ResponseEntity<String> blockHandler(BlockException blockException) {return new ResponseEntity<>(444, "QPS過高,服務開始熔斷降級--->"+blockException.getMessage());}那么問題來了,如果同時發生Java運行錯誤和限流,那么會走哪個默認的方法呢?
@GetMapping(value = "/consumer/{id}")@SentinelResource(value = "consumer", fallback = "handlerFallback", blockHandler = "blockHandler")public ResponseEntity<String> consumerData(@PathVariable("id")Integer id) {ResponseEntity<String> entity = remoteService.getData(id);if (id <= 0) {throw new IllegalArgumentException("非法參數異常!");} else if (entity.getData() == null) {throw new NullPointerException("無響應數據,空指針異常!");}return entity;}public ResponseEntity<String> handlerFallback(Integer id, Throwable e) {return new ResponseEntity<>(443, "程序處理異常,開始執行fallback數據-->" +e.getMessage());}public ResponseEntity<String> blockHandler(Integer id, BlockException blockException) {return new ResponseEntity<>(444, "QPS過高,服務開始熔斷降級--->"+blockException.getMessage());}設置限流規則
當我持續刷新,它走的是blockHandler路線
記得在配置文件加入以下
總結
以上是生活随笔為你收集整理的流控组件Sentinel核心注解@SentinelResource中的参数fallback和blockHandler的使用方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手把手带你领略双十一背后的核心技术Sen
- 下一篇: Sentinel+Nacos实现Sent