Sentinel自定義異常的三種實(shí)現(xiàn)方式
Spring Cloud Alibaba Sentinel 是目前主流并開源的流量控制和系統(tǒng)保護(hù)組件,它提供了強(qiáng)大的限流、熔斷、熱點(diǎn)限流、授權(quán)限流和系統(tǒng)保護(hù)及監(jiān)控等功能。使用它可以輕松的保護(hù)我們微服務(wù),在高并發(fā)環(huán)境下的正常運(yùn)行。
那么,當(dāng)程序觸發(fā)了限流和熔斷規(guī)則時,如何自定義返回的異常信息呢?這是我們接下來要解決的問題。
0.概述
Spring Cloud Alibaba Sentinel 有以下 3 種自定義異常的實(shí)現(xiàn)方式:
- 自定義局部異常
- 自定義(Sentinel)全局異常
- 自定義系統(tǒng)異常
以上這 3 種實(shí)現(xiàn)方式,都可以重新定義 Sentinel 的異常返回信息,它們的具體實(shí)現(xiàn)如下。
1.自定義局部異常
自定義局部異常是在使用 @SentinelResource 注解時,直接定義的 blockHandler 異常方法,如下代碼所示:
@SentinelResource(value = "/user/getuser",
blockHandler = "myBlockHandler")
@RequestMapping("getuser")
public String getUser(Integer uid) {
return "User:" + uid;
}
/**
* 定義限流/熔斷等異常
*/
public String myBlockHandler(Integer uid, BlockException e) {
String msg = "未知異常";
if (e instanceof FlowException) {
msg = "請求被限流了";
} else if (e instanceof ParamFlowException) {
msg = "請求被熱點(diǎn)參數(shù)限流";
} else if (e instanceof DegradeException) {
msg = "請求被降級了";
} else if (e instanceof AuthorityException) {
msg = "沒有權(quán)限訪問";
}
return msg;
}
注意事項
在定義 blockHandler 方法時,需要注意以下 3 個問題:
- 自定義的 blockHandler 方法的返回值,必須要和原方法(使用 @SentinelResource 注解修飾的方法)的返回值保持一致。
- 自定義的 blockHandler 方法的參數(shù)必須和原方法參數(shù)保持一致。
- 自定義的 blockHandler 方法的方法參數(shù)中必須包含 BlockException 參數(shù)。
如果不滿足以上事項中的任何一項,那么就不能正常匹配到自定義的 blockHandler 方法,并且程序也會報錯。
2.自定義全局異常
自定義 Sentinel 全局異常需要實(shí)現(xiàn) BlockExceptionHandler 類,并重寫 handle 方法,如下代碼所示:
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class SentinelExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
String msg = "未知異常";
int status = HttpStatus.TOO_MANY_REQUESTS.value();
if (e instanceof FlowException) {
msg = "請求被限流了";
} else if (e instanceof ParamFlowException) {
msg = "請求被熱點(diǎn)參數(shù)限流";
} else if (e instanceof DegradeException) {
msg = "請求被降級了";
} else if (e instanceof AuthorityException) {
msg = "沒有權(quán)限訪問";
status = HttpStatus.UNAUTHORIZED.value();
}
response.setContentType("application/json;charset=utf-8");
response.setStatus(status);
response.getWriter().println("{\"msg\": " + msg + ", \"code\": " + status + "}");
}
}
自定義 Sentinel 全局異常是在執(zhí)行 Sentinel 控制臺設(shè)置的限流和熔斷異常時,執(zhí)行的全局自定義異常方法。
但是,如果是程序中出現(xiàn)的 Sentinel 報錯信息,例如使用熱點(diǎn)限流時,因為要配合使用 @SentinelResource 注解時,此時只自定義了 value 屬性,未定義局部 blockHandler 方法,此時系統(tǒng)就會報錯,但這個時候并不會執(zhí)行 Sentinel 全局自定義異常,而是程序報錯,此時就需要使用系統(tǒng)自定義異常來重新定義異常信息了。
3.自定義系統(tǒng)異常
自定義系統(tǒng)異常需要新建一個異常類,并且使用 @RestControllerAdvice 注解修飾此類,并配合 @ExceptionHandler 注解來完成全局系統(tǒng)異常的獲取和定義,具體實(shí)現(xiàn)代碼如下:
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class CustomExceptionHandler {
/**
* 限流全局異常
*/
@ExceptionHandler(FlowException.class)
public Map handlerFlowException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "被限流");
}};
}
/**
* 熔斷全局異常
*/
@ExceptionHandler(DegradeException.class)
public Map handlerDegradeException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "被熔斷");
}};
}
/**
* 熱點(diǎn)限流異常
*/
@ExceptionHandler(ParamFlowException.class)
public Map handlerparamFlowException(){
return new HashMap(){{
put("code", HttpStatus.TOO_MANY_REQUESTS.value());
put("msg", "熱點(diǎn)限流");
}};
}
/**
* Sentinel 權(quán)限攔截全局異常
*/
@ExceptionHandler(AuthorityException.class)
@ResponseBody
public Map handlerAuthorityException(){
return new HashMap(){{
put("code", HttpStatus.UNAUTHORIZED.value());
put("msg", "暫無權(quán)限");
}};
}
}
此時,只要是系統(tǒng)中出現(xiàn)的 Sentinel 報錯信息,都會被此方法所捕獲,并通過自定義的代碼完成自定義異常信息的返回。
小結(jié)
Sentinel 有 3 種自定義異常的實(shí)現(xiàn):自定義局部異常、自定義(Sentinel)全局異常、自定義系統(tǒng)異常。自定義局部異常作用范圍比較小,需要給每個資源單獨(dú)設(shè)置才行;而自定義全局異常作用范圍比較大,但如果是程序報錯,也不會執(zhí)行其方法,所以需要配合系統(tǒng)異常同時來完成自定義異常的返回。
PS:如果這 3 種自定義異常同時存在,那么它的執(zhí)行優(yōu)先級是:自定義局部異常 > 自定義全局異常 > 自定義系統(tǒng)異常。
以上就是Sentinel自定義異常的三種實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于Sentinel自定義異常的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中的@AutoWired與@Resource及@Qualifier注解詳解
這篇文章主要介紹了Spring中的@AutoWired與@Resource及@Qualifier注解詳解,spring不但支持自己定義的@Autowired注解,所以Autowired與Spring是強(qiáng)相關(guān)性,只能在spring框架中使用,而后幾個注解則不然,需要的朋友可以參考下2023-11-11
深入解析Java多態(tài)進(jìn)階學(xué)習(xí)
java的動態(tài)綁定機(jī)制非常重要。這篇文章將帶大家更深入的學(xué)習(xí)一下Java的多態(tài),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-07-07
Java調(diào)用ChatGPT API并實(shí)現(xiàn)流式接收方式(Server-Sent Events,SSE)
文章介紹如何在Java中通過OkHttp和SSE技術(shù)實(shí)現(xiàn)流式獲取ChatGPT響應(yīng),解決傳統(tǒng)HTTP阻塞問題,提升用戶體驗,需配置stream參數(shù),利用SseEmitter封裝后端推送,前端使用EventSourcePolyfill插件處理Token,同時注意資源管理和避免換行符干擾2025-08-08
利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤
這篇文章主要介紹了利用Spring?boot+LogBack+MDC實(shí)現(xiàn)鏈路追蹤,MDC?可以看成是一個與當(dāng)前線程綁定的哈希表,可以往其中添加鍵值對,下文詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04
Java?LinkedHashMap與TreeMap示例詳解
本文對比分析了Java中LinkedHashMap和TreeMap兩種Map實(shí)現(xiàn)類的特性與使用場景,本文結(jié)合實(shí)例代碼給大家介紹給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2026-05-05

