当前位置:网站首页>[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();
}
}
边栏推荐
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- Market Research - current situation and future development trend of preclinical medical device testing service market
- Regular expression (2)
- It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
- Oracle-游标
- Socket socket c/s end process
- Market Research - current market situation and future development trend of aircraft audio control panel system
- php实现根据输入的年龄查询出生日期符合的数据
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- 100 important knowledge points that SQL must master: management transaction processing
猜你喜欢
Daily book - low code you must understand in the era of digital transformation
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
Build your own website (22)
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
#include errors detected. Please update your includePath.
Oracle-游标
An overview of the development of affective computing and understanding research
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
Pointer and string
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
随机推荐
Pointer array parameter passing, pointer parameter passing
Unity3D学习笔记4——创建Mesh高级接口
A week's life
New feature of go1.18: introduce new netip Network Library
[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)
《乔布斯传》英文原著重点词汇笔记(十)【 chapter eight】
Using emqx cloud to realize one machine one secret verification of IOT devices
对象与对象变量
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
[C question set] of V
[QT] QT multithreading development - reentrancy and thread safety
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
Market Research - current market situation and future development trend of aircraft front wheel steering system
An overview of the development of affective computing and understanding research
SimpleITK使用——3. 常见操作
Lightgbm principle and its application in astronomical data
Evolution of messaging and streaming systems under the native tide of open source cloud
Ransack combined condition search implementation