Skip to content

Commit

Permalink
Polish document and code of Sentinel annotation CDI extension
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Jun 15, 2020
1 parent fda21de commit ba3469b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
27 changes: 13 additions & 14 deletions sentinel-extension/sentinel-annotation-cdi-interceptor/README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# Sentinel Annotation cdi interceptor
# Sentinel Annotation CDI extension

This extension is an implementation using cdi interceptor for Sentinel annotations. [JSR 318: Enterprise JavaBeansTM 3.1/Interceptors 1.2](https://jcp.org/en/jsr/detail?id=318) define the javax interceptor and [CDI](http://www.cdi-spec.org/) related specifications extends the Java Interceptors specification and allows interceptor bindings to be applied to CDI stereotypes.
This extension is an implementation using CDI interceptor for Sentinel annotations. [JSR 318: Enterprise JavaBeansTM 3.1/Interceptors 1.2](https://jcp.org/en/jsr/detail?id=318) define the javax interceptor and [CDI](http://www.cdi-spec.org/) related specifications extends the Java Interceptors specification and allows interceptor bindings to be applied to CDI stereotypes.

[CDI](http://www.cdi-spec.org/) is an abbreviation for Contexts and Dependency Injection, the related JSRs are : [JSR 365: Contexts and Dependency Injection for JavaTM 2.0](https://jcp.org/en/jsr/detail?id=365), [JSR 346: Contexts and Dependency Injection for JavaTM EE 1.1](https://jcp.org/en/jsr/detail?id=346), [JSR 299: Contexts and Dependency Injection for the JavaTM EE platform](https://jcp.org/en/jsr/detail?id=299)

## Annotation

The `@SentinelResourceBinding` is modified from `@SentinelResource` by adding `@InterceptorBinding` for CDI specification, and in order to interceptor all kinds of `@SentinelResourceBinding` with different attributes in one interceptor, `@SentinelResourceBinding`'s all attribute is annotated by `@Nonbinding`
The `@SentinelResourceBinding` is modified from `@SentinelResource` by adding `@InterceptorBinding` for CDI specification,
and in order to intercept all kinds of `@SentinelResourceBinding` with different attributes in one interceptor,
all attributes of `@SentinelResourceBinding` are annotated with `@Nonbinding`.

The `@SentinelResource` annotation indicates a resource definition, including:
The `@SentinelResourceBinding` annotation indicates a resource definition, including:

- `value`: Resource name, required (cannot be empty)
- `entryType`: Resource entry type (inbound or outbound), `EntryType.OUT` by default
- `fallback` (refactored since 1.6.0): Fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). The fallback method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The method signature requirement:
- `entryType`: Traffic type (inbound or outbound), `EntryType.OUT` by default
- `fallback`: Fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). The fallback method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The method signature requirement:
- The return type should match the origin method;
- The parameter list should match the origin method, and an additional `Throwable` parameter can be provided to get the actual exception.
- `defaultFallback` (since 1.6.0): The default fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). Its intended to be a universal common fallback method. The method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The default fallback method signature requirement:
- `defaultFallback`: The default fallback method when exceptions caught (including `BlockException`, but except the exceptions defined in `exceptionsToIgnore`). Its intended to be a universal common fallback method. The method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `fallbackClass` with corresponding `Class` (Note the method in other classes must be *static*). The default fallback method signature requirement:
- The return type should match the origin method;
- parameter list should be empty, and an additional `Throwable` parameter can be provided to get the actual exception.
- `blockHandler`: Handler method that handles `BlockException` when blocked. The parameter list of the method should match original method, with the last additional parameter type `BlockException`. The return type should be same as the original method. The `blockHandler` method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `blockHandlerClass` with corresponding `Class` (Note the method in other classes must be *static*).
- `exceptionsToIgnore` (since 1.6.0): List of business exception classes that should not be traced and caught in fallback.
- `exceptionsToTrace` (since 1.5.1): List of business exception classes to trace and record. In most cases, using `exceptionsToIgnore` is better. If both `exceptionsToTrace` and `exceptionsToIgnore` are present, only `exceptionsToIgnore` will be activated.
- `exceptionsToIgnore`: List of business exception classes that should not be traced and caught in fallback.
- `exceptionsToTrace`: List of business exception classes to trace and record. In most cases, using `exceptionsToIgnore` is better. If both `exceptionsToTrace` and `exceptionsToIgnore` are present, only `exceptionsToIgnore` will be activated.

For example:

Expand All @@ -42,7 +44,8 @@ public String defaultFallback(Throwable t) {

## Configuration

according to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#enabled_interceptors) to enable interceptor, it should be configured in resources/META-INF/beans.xml :
According to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#enabled_interceptors), to enable the interceptor,
we may add configuration in `resources/META-INF/beans.xml` like this:

```
<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -54,7 +57,3 @@ according to [9.4. Interceptor enablement and ordering](https://docs.jboss.org/c
</interceptors>
</beans>
```




Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
package com.alibaba.csp.sentinel.annotation.cdi.interceptor;

import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.MethodUtil;
import com.alibaba.csp.sentinel.util.StringUtil;

import javax.interceptor.InvocationContext;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

/**
* Some common functions for Sentinel annotation aspect.
* Some common functions for Sentinel annotation CDI extension.
*
* @author Eric Zhao
* @author seasidesky
*/
public abstract class AbstractSentinelInterceptorSupport {

Expand Down Expand Up @@ -184,7 +187,15 @@ private Method extractFallbackMethod(InvocationContext ctx, String fallbackName,
private Method extractDefaultFallbackMethod(InvocationContext ctx, String defaultFallback,
Class<?>[] locationClass) {
if (StringUtil.isBlank(defaultFallback)) {
return null;
SentinelResource annotationClass = ctx.getTarget().getClass().getAnnotation(SentinelResource.class);
if (annotationClass != null && StringUtil.isNotBlank(annotationClass.defaultFallback())) {
defaultFallback = annotationClass.defaultFallback();
if (locationClass == null || locationClass.length < 1) {
locationClass = annotationClass.fallbackClass();
}
} else {
return null;
}
}
boolean mustStatic = locationClass != null && locationClass.length >= 1;
Class<?> clazz = mustStatic ? locationClass[0] : ctx.getTarget().getClass();
Expand Down Expand Up @@ -302,7 +313,7 @@ protected Method resolveMethod(InvocationContext ctx) {
Class<?> targetClass = ctx.getTarget().getClass();

Method method = getDeclaredMethodFor(targetClass, ctx.getMethod().getName(),
ctx.getMethod().getParameterTypes());
ctx.getMethod().getParameterTypes());
if (method == null) {
throw new IllegalStateException("Cannot resolve target method: " + ctx.getMethod().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

import javax.enterprise.util.Nonbinding;
import javax.interceptor.InterceptorBinding;

import java.lang.annotation.*;

/**
* The annotation indicates a definition of Sentinel resource.
*
* @author Eric Zhao
* @author seasidesky
* @since 1.8.0
*/
@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface SentinelResourceBinding {
Expand All @@ -46,7 +49,6 @@

/**
* @return the classification (type) of the resource
* @since 1.7.0
*/
@Nonbinding
int resourceType() default 0;
Expand Down Expand Up @@ -80,7 +82,6 @@
* with the original method.
*
* @return name of the default fallback method, empty by default
* @since 1.6.0
*/
@Nonbinding
String defaultFallback() default "";
Expand All @@ -92,14 +93,12 @@
* must be static.
*
* @return the class where the fallback method is located (only single class)
* @since 1.6.0
*/
@Nonbinding
Class<?>[] fallbackClass() default {};

/**
* @return the list of exception classes to trace, {@link Throwable} by default
* @since 1.5.1
*/
@Nonbinding
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
Expand All @@ -110,7 +109,6 @@
* will be of higher precedence.
*
* @return the list of exception classes to ignore, empty by default
* @since 1.6.0
*/
@Nonbinding
Class<? extends Throwable>[] exceptionsToIgnore() default {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

/**
* @author sea
* @since 1.8.0
*/
@Interceptor
@SentinelResourceBinding
Expand Down

0 comments on commit ba3469b

Please sign in to comment.