当前位置:网站首页>[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 page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- Market Research - current market situation and future development trend of aircraft audio control panel system
- Leetcode theme [array] -169- most elements
- Try to get property'num for PHP database data reading_ rows' of non-object?
- Learn computer knowledge from scratch
- Simpleitk use - 4 Strange question
- Lightgbm principle and its application in astronomical data
- Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
- kubernetes资源对象介绍及常用命令(四)
- Ransack combined condition search implementation
猜你喜欢

Micro service gateway selection, please accept my knees!

SimpleITK使用——4. 奇怪的問題

Official announcement! The golden decade of new programmers and developers was officially released

20220702 how do programmers build knowledge systems?

The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"

Ransack combined condition search implementation

SimpleITK使用——3. 常见操作
![[shutter] shutter gesture interaction (small ball following the movement of fingers)](/img/5a/a8dad8a0943645c980cc4fe7cb55d4.gif)
[shutter] shutter gesture interaction (small ball following the movement of fingers)

Socket socket c/s end process

Hanoi Tower problem
随机推荐
【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
Service visibility and observability
Share how to make professional hand drawn electronic maps
[QT] QT multithreading development - four methods to realize multithreading design
php实现根据输入的年龄查询出生日期符合的数据
Reading experience of just because
The failure rate is as high as 80%. What should we do about digital transformation?
[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)
Simpleitk use - 4 Strange question
Web side defense Guide
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
【C 题集】of Ⅴ
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
kubernetes资源对象介绍及常用命令(四)
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
Web侧防御指南
【leetcode】1380. Lucky number in matrix
phpcms实现订单直接支付宝支付功能
Attack and defense world PWN question: Echo
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation