A note to understand Sentinel @ SentinelResource summary

Keywords: Spring

@SentinelResource can be said to be a breakthrough in Sentinel learning. Understanding the application of this annotation basically clarifies most of Sentinel application scenarios.

1, @ SentinelResource resolution

Sentinel provides @ SentinelResource annotation to define resources, and provides AspectJ extension to automatically define resources and handle blockexceptions.

1. SentinelResource source code

Viewing Sentinel's source code, you can see that sentinel resource defines attributes such as value, entrytype, resourcetype, blockhandler, fallback and defaultfallback.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface SentinelResource {

    /**
     * @return 
     */
    String value() default "";

    /**
     * @return the entry type (inbound or outbound), outbound by default
     */
    EntryType entryType() default EntryType.OUT;

    /**
     * @return the classification (type) of the resource
     * @since 1.7.0
     */
    int resourceType() default 0;

    /**
     * @return name of the block exception function, empty by default
     */
    String blockHandler() default "";

    /**
     * The {@code blockHandler} is located in the same class with the original method by default.
     * However, if some methods share the same signature and intend to set the same block handler,
     * then users can set the class where the block handler exists. Note that the block handler method
     * must be static.
     * @return the class where the block handler exists, should not provide more than one classes
     */
    Class<?>[] blockHandlerClass() default {};

    /**
     * @return name of the fallback function, empty by default
     */
    String fallback() default "";

    /**
     * The {@code defaultFallback} is used as the default universal fallback method.
     * It should not accept any parameters, and the return type should be compatible
     * @return name of the default fallback method, empty by default
     * @since 1.6.0
     */
    String defaultFallback() default "";

    /**
     * The {@code fallback} is located in the same class with the original method by default.
     * However, if some methods share the same signature and intend to set the same fallback,
     * then users can set the class where the fallback function exists. Note that the shared fallback method
     * @return the class where the fallback method is located (only single class)
     * @since 1.6.0
     */
    Class<?>[] fallbackClass() default {};

    /**
     * @return the list of exception classes to trace, {@link Throwable} by default
     * @since 1.5.1
     */
    Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
    
    /**
     * Indicates the exceptions to be ignored. Note that {@code exceptionsToTrace} should
     * not appear with {@code exceptionsToIgnore} at the same time, or {@code exceptionsToIgnore}
     * will be of higher precedence.
     *
     * @return the list of exception classes to ignore, empty by default
     * @since 1.6.0
     */
    Class<? extends Throwable>[] exceptionsToIgnore() default {};
}

2. Attribute description

Refer to the notes of the source code to explain the role of these attributes one by one.

value

Resource name, required item, because the corresponding rule needs to be found through resource name, which must be configured.

entryType

entry type: optional. There are two options: IN and OUT. The default is EntryType.OUT.

public enum EntryType {
    IN("IN"),
    OUT("OUT");
}

blockHandler

blockHandler corresponds to the name of the function that handles BlockException. Optional.
The access scope of the blockHandler function needs to be public, the return type needs to match the original method, the parameter type needs to match the original method, and finally add an additional parameter with the type of BlockException.

blockHandlerClass

The blockHandler function needs to be in the same Class as the original method by default. If you want to use functions of other classes, you need to specify blockHandlerClass as the Class object of the corresponding Class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

fallback

Fallback function name, optional, used to provide fallback processing logic when throwing exceptions. The fallback function can handle all types of exceptions (except those excluded in exceptionsToIgnore).

fallbackClass

The application of fallbackClass is similar to blockHandlerClass. The fallback function needs to be in the same class as the original method by default.
If you want to use functions of other classes, you can specify fallbackClass as the Class object of the corresponding Class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

defaultFallback(since 1.6.0)

If the defaultFallback method is not configured, it will come here by default.
The default fallback function name is optional. It is usually used for general fallback logic.
The default fallback function can handle all types of exceptions (except those excluded in exceptionsToIgnore). If both fallback and defaultFallback are configured, only fallback will take effect.

exceptionsToIgnore(since 1.6.0)

It is used to specify which exceptions are excluded, which will not be included in the exception statistics and will not enter the fallback logic, but will be thrown as is.

2, Sentinel section configuration

When applying @ SentinelResource annotation, you must open the corresponding section and introduce SentinelResourceAspect.

1. AspectJ mode

Sentinel supports the extension of AspectJ, which is used to automatically define resources and handle blockexceptions. If AspectJ is enabled in the application, the corresponding Aspect needs to be introduced into the aop.xml file:

<aspects>
    <aspect name="com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect"/>
</aspects>

2. Spring AOP mode

If Spring AOP is used in the application, the Bean of SentinelResourceAspect needs to be added to the code and registered as a Spring Bean through configuration:

@Configuration
public class SentinelAspectConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

3, Summary

In this section, we learned about the related properties of @ SentinelResource and how to open SentinelResource through aspect in the project, but why does @ SentinelResource have to open aspect?



Author: Bing Yue
Link: https://www.jianshu.com/p/b61727df6ea5
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Posted by sublevel4 on Wed, 24 Nov 2021 06:41:53 -0800