Sentinel自定义异常
Posted IT-熊猫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sentinel自定义异常相关的知识,希望对你有一定的参考价值。
文章目录
1.引言
通过sentinel做限流设置后,发现其返回的错误是:"Blocked by Sentinel (flow limiting)"字符串,这严重不符合系统开发规范,因此萌生了对异常的改造念头。
2.整体思路如下
1.通过在搜索关键字“Blocked by Sentinel
”发现存在于DefaultBlockExceptionHandler
类中,而此类实现了BlockExceptionHandler
接口,DefaultBlockExceptionHandler
实现如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.alibaba.csp.sentinel.adapter.spring.webmvc.callback;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.StringUtil;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DefaultBlockExceptionHandler implements BlockExceptionHandler
public DefaultBlockExceptionHandler()
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception
response.setStatus(429);
StringBuffer url = request.getRequestURL();
if ("GET".equals(request.getMethod()) && StringUtil.isNotBlank(request.getQueryString()))
url.append("?").append(request.getQueryString());
PrintWriter out = response.getWriter();
out.print("Blocked by Sentinel (flow limiting)");
out.flush();
out.close();
2.自定义异常类MyBlockExceptionHandler
,也实现BlockExceptionHandler
接口
3.自定义异常类MyBlockExceptionHandler
实现BlockExceptionHandler
接口的handle方法, 并根据不同的异常规程返回不同的提示
4.让springboot
启动的时候自定加载自定义异常类
3.自定义异常类具体实现
- 定义
MyBlockExceptionHandler
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;
@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);
4.自定义前后返回对比
-
sentinel dashbord 限流规则如图:
-
改造前返回结果
-
改造后返回结果
以上是关于Sentinel自定义异常的主要内容,如果未能解决你的问题,请参考以下文章