当前位置:网站首页>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指定的替代方案(因为限流是在方法执行之前进行的)
边栏推荐
- STM32的CAN过滤器
- Network layer key protocol - IP protocol
- MySQL 23道经典面试吊打面试官
- leetcode: 485. Maximum number of consecutive 1s
- Nuget package and upload tutorial
- leetcode:2032. 至少在两个数组中出现的值
- 自制的数据库安全攻防题,相关靶机自己制作
- The magic of SQL MERGE statement (detailed instructions)
- Small test knife: Go reflection helped me convert Excel to Struct
- Shell script classic case: backup of files
猜你喜欢

LeetCode·304竞赛·6132·使数组中所有元素都等于零·模拟·哈希

csdn发文助手问题

STM32的CAN过滤器

一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?

Why do we need to sub-library and sub-table?

以后面试官问你 为啥不建议使用Select *,请你大声回答他!

使用CompletableFuture进行异步处理业务

Redis 】 【 publish and subscribe message

Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)

All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
随机推荐
[Pytorch] F.softmax() method description
LeetCode旋转数组
VU 非父子组件通信
线程池的使用二
232层3D闪存芯片来了:单片容量2TB,传输速度提高50%
[Blue Bridge Cup Trial Question 46] Scratch Magnet Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Shell脚本经典案例:探测批量主机是否存活
Samba 远程命令执行漏洞(CVE-2017-7494)
Resnet&API
一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?
C#高级--委托
Combination series - there are combinations when there are arrangements
Shell script classic case: backup of files
Comparison of Optical Motion Capture and UWB Positioning Technology in Multi-agent Cooperative Control Research
LeetCode·每日一题·1161.最大层内元素和·层次遍历
An article makes it clear!What is the difference and connection between database and data warehouse?
go使用makefile脚本编译应用
Node version switching management using NVM
推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】