Note 1:
@SentinelResource:
@SentinelResource is used to define resources and provide optional exception handling and fallback configuration items.
Attributes
|
effect |
value
|
Resource Name
|
entryType
|
Entry
type, marking the direction of traffic, the value is
IN/OUT
, the default is
OUT
|
blockHandler
|
The name of the function that handles
BlockException
, the function requirements:
1.
Must be
public
2. The
return type parameters are consistent with the original method
3. The default method needs to be in the same class as the original method. If you want to use functions of other classes, you can configure blockHandlerClass
and specify
the methods in
blockHandlerClass
.
|
blockHandlerClass
|
Storing
blockHandler
class
,
the corresponding handler must
static
modification.
|
fallback
|
Used to provide
fallback
processing logic
when an exception is thrown .
The fallback function can
handle all types of exceptions (except
the exception types excluded in exceptionsToIgnore). Function requirements:
1. The
return type is consistent with the original method
2. The
parameter type needs to match the original method
3. The default method needs to be in the same class as the original method. If you want to use functions of other classes, you can configure fallbackClass
and specify
the methods in
fallbackClass
.
|
fallbackClass
|
The class that stores the
fallback
. The corresponding processing function must be
statically
modified.
|
defaultFallback
|
Used for general
fallback
logic. The default
fallback function can handle all types of exceptions. If both
fallback
and
defaultFallback
are configured
, the
fallback shall prevail. Function requirements:
1. The
return type is consistent with the original method
2. The
method parameter list is empty, or there is a
Throwable
type parameter.
3. The default method needs to be in the same class as the original method. If you want to use functions of other classes, you can configure fallbackClass
and specify
the methods in
fallbackClass
.
|
exceptionsToIgnore
|
Specify which exceptions to exclude. The excluded exception will not be counted in the exception statistics, nor will it enter the fallback
logic, but thrown out as it is.
|
exceptionsToTrace
|
Exceptions that require
trace
|
Define the processing classes and methods after current limiting and downgrading:
Note: Although you can write methods directly in the current class, the reusability is low (not recommended)!
Defining the processing class can improve reusability, but the method must be static (very important)!
Use @SentinelResource as a resource on the interface or method , and configure the value, blockHandlerClass, blockHandler, fallbackClass, fallback attributes.
Create a new OrderServiceImpl3BlockHandlerClass class:
// BlockException 异常接口,其子类为Sentinel五种规则异常的实现类:
// AuthorityException 授权异常
// DegradeException 降级异常
// FlowException 限流异常
// ParamFlowException 参数限流异常
// SystemBlockException 系统负载异常
@Slf4j
public class OrderServiceImpl3BlockHandlerClass {//定义BlockException处理类
//注意:必须加上static修饰
public static String blockHandler(BlockException ex) {
log.error("{}", ex);
return "接口被限流或者降级了...";
}
}
Create a new OrderServiceImpl3FallbackClass class:
@Slf4j
public class OrderServiceImpl3FallbackClass {//定义Throwable处理类
//Throwable时进入的方法
public static String fallback(Throwable throwable) {
log.error("{}", throwable);
return "接口发生异常了...";
}
}
OrderServiceImpl3类:
@Service
@Slf4j
public class OrderServiceImpl3 {
int i = 0;
/*
@SentinelResource("资源名")注解: 用于指定本地资源(接口)并配置相应的流控规则.
*/
@SentinelResource(value = "/message5",//资源名
blockHandlerClass = OrderServiceImpl3BlockHandlerClass.class,//可以指定外部类
blockHandler = "blockHandler",//发生BlockException时调用,本类没有则须指定blockHandlerClass
fallbackClass = OrderServiceImpl3FallbackClass.class,//可以指定外部类
fallback = "fallback")//发生Throwable时调用,本类没有则须指定fallbackClass
public String message5() {
i++; //每次调用接口i就+1;所以i从1,2,3,4递增,每次递增1
if ( i % 3 == 0 ) {
throw new RuntimeException();
}//每过3次就触发一次异常,接口就会调用失败
return "message5";
}
// //直接在本类定义发生BlockException时进入的方法(复用性低,不建议)
// public String blockHandler(BlockException ex) {
// log.error("{}", ex);
// return "接口被限流或者降级了...";
// }
// //直接在本类定义发生Throwable时进入的方法(复用性低,不建议)
// public String fallback(Throwable throwable) {
// log.error("{}", throwable);
// return "接口发生异常了...";
// }
}