当前位置:网站首页>[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();
}
}
边栏推荐
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Service visibility and observability
- ServiceMesh主要解决的三大痛點
- Market Research - current situation and future development trend of herringbone gear Market
- Pointer array parameter passing, pointer parameter passing
- [QT] QT multithreading development - four methods to realize multithreading design
- [C question set] of V
- U++ 原始内存 学习笔记
- U++ 学习笔记 ----松弛
猜你喜欢
![[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)](/img/2b/f31b81cedf37ca187bcaa20dfe0b83.png)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)

An overview of the development of affective computing and understanding research

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!

Source code analysis - lightweight asynchronous crawler framework Ruia

Task and privilege level protection

《Just because》阅读感受

New feature of go1.18: trylock, which has been tossed n times

Basic concepts of image and deep understanding of yuv/rgb

C language, to achieve three chess games

Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
随机推荐
【AUTOSAR-DCM】-4.3-UDS $22和$2E服务如何读取和写入NVM数据
Infrastructure is code: a change is coming
使用 EMQX Cloud 实现物联网设备一机一密验证
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
Lightgbm principle and its application in astronomical data
Learn computer knowledge from scratch
U++ 学习笔记 堆
20220702-程序员如何构建知识体系?
New feature of go1.18: trylock, which has been tossed n times
Unity3d learning notes 4 - create mesh advanced interface
Web侧防御指南
Ransack组合条件搜索实现
SimpleITK使用——4. 奇怪的問題
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
php优化foreach中的sql查询
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
Regular expression (2)
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Market Research - current market situation and future development trend of night vision goggles for pilots
Service visibility and observability