一、背景介绍
Sentinel在默认情况下,URL触发限流后会直接返回 Blocked by Sentinel (flow limiting),提示不友好,所以需要自定义返回格式。
二、具体方案
方案一:在Spring Boot项目中,直接使用统一异常处理
/**
* 替换默认流控提示内容:Blocked by sentinel(flow limiting)
* 流控规则异常处理
* 如果 @SentinelResource中定义了 blockHandler 和 faidback 属性,则此处不会捕获,以为 blockHandler 和 failback的优先级最高。
*
*/
@ExceptionHandler({FlowException.class})
public R handleRRException(FlowException e){
logger.error(RespConstant.RESP_MSG_FLOW_LIMITING_EXCEPTION, e);
return RespUtils.commonHandle(RespConstant.RESP_CODE_FLOW_LIMITING_EXCEPTION, RespConstant.RESP_MSG_FLOW_LIMITING_EXCEPTION);
}
/**
* 服务降级异常处理
*/
@ExceptionHandler({DegradeException.class})
public R handleRRException(DegradeException e){
logger.error(RespConstant.RESP_MSG_DEGRADE_EXCEPTION, e);
return RespUtils.commonHandle(RespConstant.RESP_CODE_DEGRADE_EXCEPTION, RespConstant.RESP_MSG_DEGRADE_EXCEPTION);
}
/**
* 处理流量控制异常处理
*/
@ExceptionHandler({AuthorityException.class})
public R handleRRException(AuthorityException e){
logger.error(RespConstant.RESP_MSG_AUTHORITY_EXCEPTION, e);
return RespUtils.commonHandle(RespConstant.RESP_CODE_AUTHORITY_EXCEPTION, RespConstant.RESP_MSG_AUTHORITY_EXCEPTION);
}
/**
* 处理流量控制异常处理
*/
@ExceptionHandler({ParamFlowException.class})
public R handleRRException(ParamFlowException e){
logger.error(RespConstant.RESP_MSG_PARAM_FLOW_EXCEPTION, e);
return RespUtils.commonHandle(RespConstant.RESP_CODE_PARAM_FLOW_EXCEPTION, RespConstant.RESP_MSG_PARAM_FLOW_EXCEPTION);
}
/**
* 处理流量控制异常处理
*/
@ExceptionHandler({SystemBlockException.class})
public R handleRRException(SystemBlockException e){
logger.error(RespConstant.RESP_MSG_SYSTEM_BLOCK_EXCEPTION, e);
return RespUtils.commonHandle(RespConstant.RESP_CODE_SYSTEM_BLOCK_EXCEPTION, RespConstant.RESP_MSG_SYSTEM_BLOCK_EXCEPTION);
}
方案二:实现UrlBlockHandler接口
**package com.cyun.common.config.sentinel;
import cn.hutool.json.JSONUtil;
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 com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.cyun.core.result.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义sentinel异常返回信息
*
* @author panfu.he
*/
@Component
public class SentinelBlockHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
String msg = null;
if (ex instanceof FlowException) {
msg = "限流了";
} else if (ex instanceof DegradeException) {
msg = "降级了";
} else if (ex instanceof ParamFlowException) {
msg = "热点参数限流";
} else if (ex instanceof SystemBlockException) {
msg = "系统规则(负载/...不满足要求)";
} else if (ex instanceof AuthorityException) {
msg = "授权规则不通过";
}
response.setContentType("text/json;charset=utf-8");
response.getWriter().print(JSONUtil.toJsonStr(ResultVO.error(HttpStatus.TOO_MANY_REQUESTS.value(), msg)));
}
}
方案三:跳转降级界面测试(已过时)
spring:
application:
name: springboot-sentinel-sample
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:7777
servlet:
block-page: /goerror
@GetMapping("/goerror")
private String error(){
return "error";
}