当前位置:网站首页>[micro service sentinel] @sentinelresource details
[micro service sentinel] @sentinelresource details
2022-07-01 23:12:00 【Bulst】
List of articles
We mentioned in the article we wrote before , At the current limit of hot spot parameters , We can only use @SentinelResource Annotation to define resources , To take effect , So what is the use of this annotation ?
Sentinel Provides @SentinelResource Annotations are used to define resources , And provides AspectJ The extension of is used to automatically define resources 、 Handle BlockException etc. .
Introduce
@SentinelResource The annotation contains the following properties :
- value: Resource name , Required ( Can't be empty )
- entryType:entry type , optional ( The default is EntryType.OUT)
- blockHandler / blockHandlerClass: blockHandler Deal with BlockException The function name of , optional .blockHandler The function access range needs to be public, Return type needs to match the original method , The parameter type needs to match the original method and add an extra parameter , The type is BlockException.blockHandler Function default needs to be in the same class as the original method . If you want to use functions of other classes , You can specify blockHandlerClass For the corresponding class Class object , Note that the corresponding function must be static function , Otherwise, it cannot be parsed .
- fallback:fallback The name of the function , optional , Used to provide... When an exception is thrown fallback Processing logic .fallback Functions can be used for all types of exceptions ( except exceptionsToIgnore The types of exceptions excluded ) To deal with .fallback Function signature and location requirements :
- The return value type must be the same as the return value type of the original function ;
- Method parameter list should be consistent with the original function , Or maybe one more Throwable The parameter of type is used to receive the corresponding exception .
- fallback Function default needs to be in the same class as the original method . If you want to use functions of other classes , You can specify fallbackClass For the corresponding class Class object , Note that the corresponding function must be static function , Otherwise, it cannot be parsed .
- defaultFallback(since 1.6.0): default fallback The name of the function , optional , Usually used for general purpose fallback Logic ( It can be used in many services or methods ). Default fallback Functions can be used for all types of exceptions ( except exceptionsToIgnore The types of exceptions excluded ) To deal with . If you configure fallback and defaultFallback, only fallback Will take effect .defaultFallback Function signature requirements :
- The return value type must be the same as the return value type of the original function ;
- Method parameter list needs to be empty , Or maybe one more Throwable The parameter of type is used to receive the corresponding exception .
- defaultFallback Function default needs to be in the same class as the original method . If you want to use functions of other classes , You can specify fallbackClass For the corresponding class Class object , Note that the corresponding function must be static function , Otherwise, it cannot be parsed .
- exceptionsToIgnore(since 1.6.0): Used to specify which exceptions are excluded , It will not be included in the abnormal statistics , And will not enter fallback In the logic , But will throw out as is .
Specially , if blockHandler and fallback It's all configured , It is degraded by current limiting and thrown BlockException It will only enter blockHandler Processing logic . If not configured blockHandler、fallback and defaultFallback, When it is degraded by current limiting, it will BlockException Direct selling .
Case study
public class TestService {
// Corresponding `handleException` The function needs to be in `ExceptionUtil` Class , And must be static function .
@SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {
ExceptionUtil.class})
public void test() {
System.out.println("Test");
}
// Primitive function
@SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
public String hello(long s) {
return String.format("Hello at %d", s);
}
// Fallback function , The signature of the function is the same as the original function or add one Throwable Parameters of type .
public String helloFallback(long s) {
return String.format("Halooooo %d", s);
}
// Block Exception handling functions , One more parameter at the end BlockException, The rest is consistent with the original function .
public String exceptionHandler(long s, BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "Oops, error occurred at " + s;
}
}
边栏推荐
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- 日本购物网站的网络乞丐功能
- Demo program implementation of QT version Huarui camera
- OpenVINO 模型性能评估工具—DL Workbench
- [MySQL] basic use of explain and the function of each column
- 半监督学习之数据增强
- Programming English vocabulary notebook
- Microservice stability management
- AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
- Yoga27 multidimensional all-in-one computer with excellent appearance and high-end configuration
猜你喜欢

YOGA27多维一体电脑,兼具出色外观与高端配置

2022年起重机司机(限桥式起重机)考试试题及模拟考试

数字峰会人气火爆,城链科技引发新一轮商业变革

Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load

Turn -- bring it and use it: share a gadget for checking memory leaks

The digital summit is popular, and city chain technology has triggered a new round of business transformation

Istio, ebpf and rsocket Broker: in depth study of service grid

Copy ‘XXXX‘ to effectively final temp variable

数字化转型道阻且长,如何迈好关键的第一步

MySQL -- deduction of index storage model
随机推荐
思科考试--冗余网络
Explain the volatile keyword
数字峰会人气火爆,城链科技引发新一轮商业变革
You probably haven't noticed the very important testing strategy in your work
测试人进阶技能:单元测试报告应用指南
ECMAScript 2022 was officially released. Have you heard about it?
CKS CKA ckad change terminal to remote desktop
思科--高可用和高可靠网络考试
js——arguments的使用
【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
【嵌入式系统课设】单个按键控制LED灯
Jerry's burning of upper version materials requires [chapter]
2022年危险化学品经营单位安全管理人员考试题及在线模拟考试
Happy number [fast and slow pointer of ring PROBLEMS]
马赛克后挡板是什么?
AirServer最新Win64位个人版投屏软件
Flink SQL command line connection yarn
rviz打开后如何显示实时2D地图
第三方验收测试有什么好处?专业第三方软件测试机构推荐
[MySQL] index classification