当前位置:网站首页>【微服务|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();
}
}
边栏推荐
- Pyqt picture decodes and encodes and loads pictures
- New feature of go1.18: trylock, which has been tossed n times
- 540. Single element in ordered array
- scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
- Lightgbm principle and its application in astronomical data
- 100 important knowledge points that SQL must master: using cursors
- Market Research - current market situation and future development trend of aircraft audio control panel system
- [shutter] shutter resource file use (import resource pictures | use image resources)
- Promise optimized callback hell
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
猜你喜欢
LandingSite eBand B1冒烟测试用例
[001] [arm-cortex-m3/4] internal register
Etcd raft protocol
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
20220702 how do programmers build knowledge systems?
TinyMCE visual editor adds Baidu map plug-in
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
20220702-程序员如何构建知识体系?
PIP audit: a powerful security vulnerability scanning tool
Etcd Raft 协议
随机推荐
Web side defense Guide
The failure rate is as high as 80%. What should we do about digital transformation?
:last-child 不生效解决
Oriental Aesthetics and software design
The difference between include < > and include ""
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Unity3d learning notes 4 - create mesh advanced interface
Destroy in beforedestroy invalid value in localstorage
Service visibility and observability
记录一下微信、QQ、微博分享web网页功能
Market Research - current situation and future development trend of cell-based seafood market
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Landingsite eband B1 smoke test case
[Jianzhi offer] 56 - ii Number of occurrences of numbers in the array II
Unity3D学习笔记4——创建Mesh高级接口
Pointer and string
How to write a good program when a big book speaks every day?
Promise optimized callback hell
20220702-程序员如何构建知识体系?