当前位置:网站首页>[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;
}
}
边栏推荐
- Using emqx cloud to realize one machine one secret verification of IOT devices
- Explain the volatile keyword
- ESP自动下载电路设计
- 【嵌入式系统课设】单个按键控制LED灯
- Convergence and disposal suggestions of some Internet exposure surfaces
- Turn -- bring it and use it: share a gadget for checking memory leaks
- Jielizhi, production line assembly link [chapter]
- The online beggar function of Japanese shopping websites
- 想请教股票开户要认识谁?在线开户是安全么?
- Map container
猜你喜欢
【微服务|Sentinel】sentinel整合openfeign
Tcpdump command usage details
有些能力,是工作中学不来的,看看这篇超过90%同行
rviz打开后如何显示实时2D地图
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration
Use 3DMAX to make a chess piece
工作中非常重要的测试策略,你大概没注意过吧
Jielizhi Bluetooth headset quality control and production skills [chapter]
赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链
随机推荐
leetcode - 287. Find duplicates
"Trust machine" empowers development
Cloud Vulnerability Global Database
Compare the version number [double pointer to intercept the string you want]
Turn -- the underlying debugging principle of GDB is so simple
Jielizhi Bluetooth headset quality control and production skills [chapter]
ESP自动下载电路设计
Detailed explanation of twenty common software testing methods (the most complete in History)
【嵌入式系统课设】单个按键控制LED灯
Use and function of spark analyze command map join broadcast join
plain framework的实际应用和扩展
Aaai22 | structural tagging and interaction modeling: a "slim" network for graph classification
[机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
Happy number [fast and slow pointer of ring PROBLEMS]
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
Contents of other parts of the map
js——arguments的使用
“35岁,公司老总,月薪2万送外卖“:时代抛弃你,连声再见都没有
马赛克后挡板是什么?
OpenVINO 模型性能评估工具—DL Workbench