当前位置:网站首页>【微服务|Sentinel】@SentinelResource详解
【微服务|Sentinel】@SentinelResource详解
2022-07-01 22:29:00 【步尔斯特】
之前我们写过的文章中提到,在热点参数限流处,我们只能使用@SentinelResource注解来定义资源,才能生效,那么这个注解到底还有何用处呢?
Sentinel 提供了 @SentinelResource 注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 BlockException 等。
介绍
@SentinelResource 注解包含以下属性:
- value:资源名称,必需项(不能为空)
- entryType:entry 类型,可选项(默认为 EntryType.OUT)
- blockHandler / blockHandlerClass: blockHandler 对应处理 BlockException 的函数名称,可选项。blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- fallback:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。fallback 函数签名和位置要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所以类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。
特别地,若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。
案例
public class TestService {
// 对应的 `handleException` 函数需要位于 `ExceptionUtil` 类中,并且必须为 static 函数.
@SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {
ExceptionUtil.class})
public void test() {
System.out.println("Test");
}
// 原函数
@SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
public String hello(long s) {
return String.format("Hello at %d", s);
}
// Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
public String helloFallback(long s) {
return String.format("Halooooo %d", s);
}
// Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
public String exceptionHandler(long s, BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "Oops, error occurred at " + s;
}
}
边栏推荐
- El input text field word limit, beyond which the display turns red and input is prohibited
- SAP intelligent robot process automation (IRPA) solution sharing
- Flynk SQL client uses comparison and is familiar with official documents
- Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
- CKS CKA CKAD 将终端更改为远程桌面
- Electron学习(三)之简单交互操作
- Use 3DMAX to make a chess piece
- Unable to climb hill sort, directly insert sort
- 转--拿来即用:分享一个检查内存泄漏的小工具
- 14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
猜你喜欢

Cutefishos system~

思科考试--冗余网络

Advanced skills of testers: a guide to the application of unit test reports
![[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party](/img/ad/dcb993c3e1e79d2c3663f031dfd1b3.png)
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party
![[target tracking] | single target tracking indicator](/img/07/033d5c07b07e3443330840e98940b3.png)
[target tracking] | single target tracking indicator

I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction

shell 流程控制
![Jerry's burning of upper version materials requires [chapter]](/img/65/fcd804e00dc08a2bd056e8e6493829.png)
Jerry's burning of upper version materials requires [chapter]

Copy ‘XXXX‘ to effectively final temp variable

"Trust machine" empowers development
随机推荐
正则系列之量词(Quantifiers)
Stimulate new kinetic energy and promote digital economy in multiple places
Origin2018 installation tutorial "recommended collection"
[daily training] 66 add one-tenth
思科考试--冗余网络
Design of ESP automatic download circuit
Treatment of insufficient space in the root partition of armbain system
Explain ThreadLocal in detail
思科--高可用和高可靠网络考试
ECMAScript 2022 was officially released. Have you heard about it?
Cutefishos system~
shell 流程控制
El input text field word limit, beyond which the display turns red and input is prohibited
Jielizhi Bluetooth headset quality control and production skills [chapter]
Metauniverse may become a new direction of Internet development
MySQL -- deduction of index storage model
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
Single step debugging analysis of rxjs observable of operator
YOGA27多维一体电脑,兼具出色外观与高端配置