当前位置:网站首页>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
指定的替代方案(因为限流是在方法执行之前进行的)
边栏推荐
- C# using ComboBox control
- 技能大赛训练题:域用户和组织单元的创建
- [Pytorch] F.softmax() method description
- ADS communicate with c #
- C# using NumericUpDown control
- Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
- C# Get network card information NetworkInterface IPInterfaceProperties
- 49.【拷贝构造函数与重载】
- 49. The copy constructor and overloaded 】
- VU 非父子组件通信
猜你喜欢
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Resolved (pymysqL connect to the database error) pymysqL. Err. ProgrammingError: (1146, "Table" test. Students' doesn 't exist ")
The 232-layer 3D flash memory chip is here: the single-chip capacity is 2TB, and the transmission speed is increased by 50%
对数字化时代的企业来说,数据治理难做,但应该去做
【蓝桥杯选拔赛真题46】Scratch磁铁游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
代码随想录笔记_哈希_454四数相加II
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
uniapp微信小程序引用标准版交易组件
ADS communicate with c #
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)
Install the latest pytorch gpu version
I summed up the bad MySQL interview questions
“听我说谢谢你”还能用古诗来说?清华搞了个“据意查句”神器,一键搜索你想要的名言警句...
线程池的使用二
【Pytorch】F.softmax()方法说明
A detailed explanation of the usage of Async and Await in C#
技能大赛训练题:登录安全加固
ML、DL、CV常见的问题整理
最近很火的国产接口神器Apipost体验
龟速乘【模板】
redhat/openssl generates a self-signed ca certificate and uses it
Nuget package and upload tutorial
The use of thread pool two
我把问烂了的MySQL面试题总结了一下
组合系列--有排列就有组合
MySQL [aggregate function]
拥塞控制,CDN,端到端
Introduction to the PartImageNet Semantic Part Segmentation dataset
MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!