经过长时间的发展,现在系统前后端分离已成为一种潜移默化的行为,当时这是一件好事,在有限的精力里面让更专业的人做专业的事。
通过sentinel做限流设置后,发现其返回的错误是:"Blocked by Sentinel (flow limiting)"字符串,这严重不符合系统开发规范,因此萌生了对异常的改造念头。
一、整体思路如下:
1.通过在搜索关键字“Blocked by Sentinel”发现存在于DefaultBlockExceptionHandler类中,而此类实现了BlockExceptionHandler接口,DefaultBlockExceptionHandler实现如下:
2.自定义异常类MyBlockExceptionHandler,也实现BlockExceptionHandler接口
3.自定义异常类MyBlockExceptionHandler实现BlockExceptionHandler接口的handle方法, 并根据不同的异常规程返回不同的提示
4.让springboot启动的时候自定加载自定义异常类
二、自定义异常类具体实现
1. 定义返回对象,MyBlockExceptionHandler会用到此类
package com.charlie.cloudconsumer.common.utils;
import lombok.Builder;
import lombok.Data;
/**
* @Author: charlie
* @CreateTime: 2022/2/19
* Description: 异步结果类
*/
@Data
@Builder //生产构建器模式
public class AjaxResult {
private String msg;
private Integer status;
private T data;
}
2. 定义MyBlockExceptionHandler
package com.charlie.cloudconsumer.common.exception.sentinel;
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.charlie.cloudconsumer.common.utils.AjaxResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @Author: charlie
* @CreateTime: 2022/2/19
* Description: 自定义sentinel限流异常,sentinel的异常类默认是加载DefaultBlockExceptionHandler
*/
@Slf4j
@Configuration
public class MyBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,BlockException e)throws Exception {
log.info("urlBlock.......................");
AjaxResult ajaxResult =null;
//不同的异常返回不同的提示语
if(einstanceof FlowException) {//限流异常
ajaxResult =AjaxResult.builder().status(100).msg("您的访问过于频繁,请稍后重试").build();
}else if (einstanceof DegradeException) {//降级异常
ajaxResult =AjaxResult.builder().status(101).msg("调用服务响应异常,已进行降级").build();
}else if (einstanceof ParamFlowException) {//热点参数异常
ajaxResult =AjaxResult.builder().status(102).msg("您对热点参数访问过于频繁,请稍后重试").build();
}else if (einstanceof SystemBlockException) {//系统保护规则
ajaxResult =AjaxResult.builder().status(103).msg("已触碰系统的红线规则,请检查访问参数").build();
}else if (einstanceof AuthorityException) {//授权规则
ajaxResult =AjaxResult.builder().status(104).msg("授权规则检测不同,请检查访问参数").build();
}else {//其他规则
ajaxResult =AjaxResult.builder().status(105).msg("非法访问,请稍后重试").build();
}
httpServletResponse.setStatus(500);
httpServletResponse.setCharacterEncoding("utf-8");
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
//springmvc的一个json转换类(jockson)
new ObjectMapper().writeValue(httpServletResponse.getWriter(),ajaxResult);
}
}
三、自定义前后返回对比
1. sentinel dashbord 限流规则如图:
2. 改造前返回结果
3.改造后返回结果
以上的代码是通过本人运行,希望对看到的您有所帮助!