当前位置:网站首页>Sentinel限流和异常处理
Sentinel限流和异常处理
2022-07-31 14:05:00 【Leon_Jinhai_Sun】
现在我们已经了解了如何进行限流操作,那么限流状态下的返回结果该怎么修改呢,我们看到被限流之后返回的是Sentinel默认的数据,现在我们希望自定义改如何操作?
这里我们先创建好被限流状态下需要返回的内容,定义一个请求映射:
@RequestMapping("/blocked")
JSONObject blocked(){
JSONObject object = new JSONObject();
object.put("code", 403);
object.put("success", false);
object.put("massage", "您的请求频率过快,请稍后再试!");
return object;
}接着我们在配置文件中将此页面设定为限流页面:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8858
# 将刚刚编写的请求映射设定为限流页面
block-page: /blocked这样,当被限流时,就会被重定向到指定页面:

那么,对于方法级别的限流呢?经过前面的学习我们知道,当某个方法被限流时,会直接在后台抛出异常,那么这种情况我们该怎么处理呢,比如我们之前在Hystrix中可以直接添加一个替代方案,这样当出现异常时会直接执行我们的替代方法并返回,Sentinel也可以。
比如我们还是在getUserBorrowDetailByUid方法上进行配置:
@Override
@SentinelResource(value = "getBorrow", blockHandler = "blocked") //指定blockHandler,也就是被限流之后的替代解决方案,这样就不会使用默认的抛出异常的形式了
public UserBorrowDetail getUserBorrowDetailByUid(int uid) {
List<Borrow> borrow = mapper.getBorrowsByUid(uid);
User user = userClient.getUserById(uid);
List<Book> bookList = borrow
.stream()
.map(b -> bookClient.getBookById(b.getBid()))
.collect(Collectors.toList());
return new UserBorrowDetail(user, bookList);
}
//替代方案,注意参数和返回值需要保持一致,并且参数最后还需要额外添加一个BlockException
public UserBorrowDetail blocked(int uid, BlockException e) {
return new UserBorrowDetail(null, Collections.emptyList());
}可以看到,一旦被限流将执行替代方案,最后返回的结果就是:

注意blockHandler只能处理限流情况下抛出的异常,包括下面即将要介绍的热点参数限流也是同理,如果是方法本身抛出的其他类型异常,不在管控范围内,但是可以通过其他参数进行处理:
@RequestMapping("/test")
@SentinelResource(value = "test",
fallback = "except", //fallback指定出现异常时的替代方案
exceptionsToIgnore = IOException.class) //忽略那些异常,也就是说这些异常出现时不使用替代方案
String test(){
throw new RuntimeException("HelloWorld!");
}
//替代方法必须和原方法返回值和参数一致,最后可以添加一个Throwable作为参数接受异常
String except(Throwable t){
return t.getMessage();
}这样,其他的异常也可以有替代方案了:

特别注意这种方式会在没有配置blockHandler的情况下,将Sentinel机制内(也就是限流的异常)的异常也一并处理了,如果配置了blockHandler,那么在出现限流时,依然只会执行blockHandler指定的替代方案(因为限流是在方法执行之前进行的)
边栏推荐
- csdn发文助手问题
- C#高级--委托
- 我把问烂了的MySQL面试题总结了一下
- C# control StatusStrip use
- Resnet&API
- All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- A detailed explanation of the usage of Async and Await in C#
- MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!
- For enterprises in the digital age, data governance is difficult, but it should be done
- MySQL 23道经典面试吊打面试官
猜你喜欢

已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)

STM32的CAN过滤器

Analysis of the startup source code of hyperf (2) - how the request reaches the controller

LeetCode·每日一题·1161.最大层内元素和·层次遍历

Introduction to the PartImageNet Semantic Part Segmentation dataset

endnote引用

Asynchronous processing business using CompletableFuture
![Miller_Rabin Miller Rabin probability sieve [template]](/img/51/8dcc9f78478debf7d3dcfa6d1a23e3.png)
Miller_Rabin Miller Rabin probability sieve [template]

Uniapp WeChat small application reference standard components

AWS implements scheduled tasks - Lambda+EventBridge
随机推荐
C#高级--委托
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Solution for browser hijacking by hao360
CLion用于STM32开发
技能大赛训练题: 子网掩码划分案例
How to quickly split and merge cell data in Excel
49. The copy constructor and overloaded 】
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
MySQL【子查询】
五个维度着手MySQL的优化
AWS实现定时任务-Lambda+EventBridge
已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)
LeetCode·每日一题·1161.最大层内元素和·层次遍历
技能大赛训练题:登录安全加固
最近很火的国产接口神器Apipost体验
Uniapp WeChat small application reference standard components
LeetCode只出现一次的数字
纸质说明书秒变3D动画,斯坦福大学吴佳俊最新研究,入选ECCV 2022
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
SetoolKit User Guide