当前位置:网站首页>[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
2022-07-02 22:35:00 【Bulst】
List of articles
Above, , We use sentinel Integrate feign, Let it be in feign The client handles exceptions uniformly , The principle is that when one service calls another service , If an exception is detected BlockExceptionHandler, Use the bottom covering method to deal with .
Source code
Let's have a look BlockExceptionHandler Source code :
public interface BlockExceptionHandler {
void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception;
}
Oh , It turned out to be an interface .
Realization
Let's take a look at the implementation classes , Just a default , ok .
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();
}
}
rewrite
This mistake is familiar , If we want to customize the current limiting return format and the information we need to return can distinguish various current limiting exceptions , We need to rewrite BlockExceptionHandler Interface , such as :
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"," Current limiting - It's abnormal ");
}else if (e instanceof DegradeException){
backMap.put("code",-2);
backMap.put("msg"," Downgrade - It's abnormal ");
}else if (e instanceof ParamFlowException){
backMap.put("code",-3);
backMap.put("msg"," hotspot - It's abnormal ");
}else if (e instanceof SystemBlockException){
backMap.put("code",-4);
backMap.put("msg"," System rules - It's abnormal ");
}else if (e instanceof AuthorityException){
backMap.put("code",-5);
backMap.put("msg"," authentication - It's abnormal ");
}
// Set back json data
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("content-Type","application/json;charset=UTF-8");
httpServletResponse.getWriter().write(JSON.toJSONString(backMap));
}
}
Problems and Solutions
But it's been tested , Find out , Hot spot current limiting is not easy to use , Then use the global exception to intercept , Ha ha ha , If you know the reason or have a better way , Remember to leave a message ~
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(" Trigger hot spot current limiting ");
out.flush();
out.close();
}
}
边栏推荐
- 一周生活
- Reading experience of just because
- ServiceMesh主要解决的三大痛点
- 【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
- 将 EMQX Cloud 数据通过公网桥接到 AWS IoT
- Oriental Aesthetics and software design
- Try to get property'num for PHP database data reading_ rows' of non-object?
- The failure rate is as high as 80%. What should we do about digital transformation?
- 任务和特权级保护
- Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
猜你喜欢
The failure rate is as high as 80%. What should we do about digital transformation?
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Kubernetes resource object introduction and common commands (4)
Oriental Aesthetics and software design
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
Sql service intercepts string
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Pointer and string
Daily book - low code you must understand in the era of digital transformation
[shutter] shutter application theme (themedata | dynamic modification theme)
随机推荐
App page sharing password rails implementation
The difference between include < > and include ""
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
Market Research - current situation and future development trend of carob chocolate market
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
UE4 UI自适应屏幕
Market Research - current situation and future development trend of preclinical medical device testing service market
SimpleITK使用——4. 奇怪的問題
Utilisation de simpletk - 4. Question étrange
Dynamic memory allocation (malloc calloc realloc free)
Market Research - current market situation and future development trend of intravenous injection (IV) bottles
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
Market Research - current market situation and future development trend of night vision goggles for pilots
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
服务器响应状态码
《乔布斯传》英文原著重点词汇笔记(九)【 chapter seven】
Simpleitk use - 4 Strange question
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
Lightgbm principle and its application in astronomical data
Daily book CSO advanced road first exposed