当前位置:网站首页>【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
2022-07-02 22:01:00 【步尔斯特】
上文,我们使用sentinel整合feign,让其在feign客户端统一对异常进行处理,其原理是在一个服务调用另一个服务的时候,如果检测到异常为BlockExceptionHandler,就使用兜底方法进行处理。
源码
让我们看一看BlockExceptionHandler的源码:
public interface BlockExceptionHandler {
void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception;
}
哦,原来是一个接口。
实现
那我们来看看实现类都有哪些,就一个默认的,好吧。
public class DefaultBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
// Return 429 (Too Many Requests) by default.
response.setStatus(429);
PrintWriter out = response.getWriter();
out.print("Blocked by Sentinel (flow limiting)");
out.flush();
out.close();
}
}
重写
这个错误熟悉吧,如果我们想自定义限流返回格式并且需要返回的信息可以区分开各种限流异常,就需要我们重写BlockExceptionHandler接口,比如:
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.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/** * @author issavior */
@Component
public class MyUrlBlockHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
Map<String,Object> backMap=new HashMap<>(16);
if (e instanceof FlowException){
backMap.put("code",-1);
backMap.put("msg","限流-异常啦");
}else if (e instanceof DegradeException){
backMap.put("code",-2);
backMap.put("msg","降级-异常啦");
}else if (e instanceof ParamFlowException){
backMap.put("code",-3);
backMap.put("msg","热点-异常啦");
}else if (e instanceof SystemBlockException){
backMap.put("code",-4);
backMap.put("msg","系统规则-异常啦");
}else if (e instanceof AuthorityException){
backMap.put("code",-5);
backMap.put("msg","认证-异常啦");
}
// 设置返回json数据
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("content-Type","application/json;charset=UTF-8");
httpServletResponse.getWriter().write(JSON.toJSONString(backMap));
}
}
问题及解决
但是经过测试,发现,热点限流不太好用,那就用全局异常来拦截一下吧,哈哈哈,如果有知道原因或者有更好的办法,记得留言哦~
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/** * @author issavior */
@RestControllerAdvice
public class MyParamFlowException {
@ExceptionHandler(ParamFlowException.class)
public void test(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setStatus(400);
PrintWriter out = response.getWriter();
out.print("触发热点限流限流");
out.flush();
out.close();
}
}
边栏推荐
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- Market Research - current situation and future development trend of herringbone gear Market
- Ransack combined condition search implementation
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- [sword finger offer] 56 - I. the number of numbers in the array
- LightGBM原理及天文数据中的应用
- About test cases
- How do I access the kubernetes API?
- Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
- Market Research - current market situation and future development trend of genome editing mutation detection kit
猜你喜欢
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Daily book - low code you must understand in the era of digital transformation
Pointer and string
Image segmentation using pixellib
Basic concepts of image and deep understanding of yuv/rgb
Introduction to the principle of geographical detector
[shutter] shutter gesture interaction (small ball following the movement of fingers)
[zero foundation I] Navicat download link
About test cases
CVPR论文解读 | 弱监督的高保真服饰模特生成
随机推荐
Official announcement! The golden decade of new programmers and developers was officially released
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
Market Research - current market situation and future development trend of third-party data platform
ServiceMesh主要解决的三大痛點
[sword finger offer] 56 - I. the number of numbers in the array
C language, to achieve three chess games
[zero foundation I] Navicat download link
[shutter] shutter gesture interaction (small ball following the movement of fingers)
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
About test cases
B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
Market Research - current market situation and future development trend of aircraft front wheel steering system
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
ServiceMesh主要解决的三大痛点
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
20220702 how do programmers build knowledge systems?
Market Research - current market situation and future development trend of high tibial osteotomy plate
服务可见可观测性
Technological Entrepreneurship: failure is not success, but reflection is