In a previous blog post, I gave you an example of how to use @SentinelResource to define resources to achieve traffic limiting. Here’s a look at how to do this using the source code.@SentinelResource is the breakthrough of Sentinel learning. If you understand the application of this annotation, you can basically understand most of the application scenarios of Sentinel.

@sentinelResource parsing

Sentinel provides the @SentinelResource annotation for defining resources and an extension to AspectJ for automatically defining resources, handling blockexceptions, and so on.

1, SentinelResource source code

View the Sentinel source code, can see SentinelResource defines value, entryType, resourceType, blockHandler, fallback, defaultFallback properties, etc.

@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 {}; }Copy the code

2. Attribute description

Refer to the source code comments to explain each of these attributes.

value

Resource name, required, because the rule needs to be found by resource name, this must be configured.

entryType

The entry type is optional. There are two options: IN and OUT. The default value is entryType.out.

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

blockHandler

BlockHandler Specifies the name of the function that processes BlockException. This parameter is optional. The blockHandler function must have a public access scope, a return type that matches the original method, a parameter type that matches the original method and an extra parameter of type BlockException at the end.

blockHandlerClass

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

fallback

The optional fallback function name that provides fallback handling logic when an exception is thrown. The fallback function handles all types of exceptions except those excluded from exceptionsToIgnore.

fallbackClass

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

DefaultFallback (since 1.6.0)

If the defaultFallback method is not configured, this is the default. The default, optional fallback function name, usually used for generic fallback logic. The default fallback function handles all types of exceptions except those excluded from exceptionsToIgnore. If both fallback and defaultFallback are configured, only fallback takes effect.

ExceptionsToIgnore (since 1.6.0)

Used to specify which exceptions are excluded and will not be counted in the exception count or in fallback logic, but will be thrown as is.

2. Sentinel section configuration

To use the @SentinelResource annotation, you must open the corresponding section to bring in SentinelResource Ect.

1. AspectJ

Sentinel supports AspectJ extensions to automatically define resources, handle blockexceptions, etc. If AspectJ is enabled in your application, you need to introduce the corresponding Aspect in the AOP.xml file:

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

2. Spring AOP approach

If Spring AOP is used in your application, you need to add a SentinelResourceAspect Bean to your code, and register the SentinelResourceAspect as a Spring Bean by configuration:

@Configuration
public class SentinelAspectConfiguration {

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

Third, summary

In this section, you learned about the properties of @SentinelResource and how to enable a SentinelResource in a project through a section. Why must a section be enabled to use @SentinelResource? In the next section, we will learn the principle of Sentinel Resources ect section in Sentinel.

Follow the public account: Architecture evolution, get first hand technical information and original articles