当前位置:网站首页>[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();
}
}
边栏推荐
- "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!
- U++ 原始内存 学习笔记
- Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
- [QT] QT multithreading development - reentrancy and thread safety
- Une semaine de vie
- Commodity information management system (C language document version)
- 建立自己的网站(22)
- Evolution of messaging and streaming systems under the native tide of open source cloud
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
- Share how to make professional hand drawn electronic maps
猜你喜欢

20220702-程序员如何构建知识体系?

【外刊】睡眠与减肥

Phpcms realizes the direct Alipay payment function of orders

From personal heroes to versatile developers, the era of programmer 3.0 is coming

Micro service gateway selection, please accept my knees!

图像基础概念与YUV/RGB深入理解
![[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)](/img/27/8594ba0b49d5008b7469967babed17.jpg)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
![[shutter] shutter resource file use (import resource pictures | use image resources)](/img/e9/94ae2e3ee315f490eb3cf14bcf2e49.jpg)
[shutter] shutter resource file use (import resource pictures | use image resources)

Source code analysis - lightweight asynchronous crawler framework Ruia

SimpleITK使用——3. 常见操作
随机推荐
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Necessary browser plug-ins for network security engineers
New feature of go1.18: trylock, which has been tossed n times
[C question set] of V
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Leetcode theme [array] -169- most elements
Ransack组合条件搜索实现
U++ 学习笔记 ----松弛
Une semaine de vie
【外刊】睡眠与减肥
Market Research - current market situation and future development trend of genome editing mutation detection kit
100 important knowledge points that SQL must master: management transaction processing
Destroy in beforedestroy invalid value in localstorage
Evolution of messaging and streaming systems under the native tide of open source cloud
Basic concepts of image and deep understanding of yuv/rgb
How to write a good program when a big book speaks every day?
Market Research - current market situation and future development trend of aircraft front wheel steering system
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file