seninel部署
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Sentinel Annotation cdi interceptor
  2. 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.
  3. [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)
  4. ## Annotation
  5. 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`
  6. The `@SentinelResource` annotation indicates a resource definition, including:
  7. - `value`: Resource name, required (cannot be empty)
  8. - `entryType`: Resource entry type (inbound or outbound), `EntryType.OUT` by default
  9. - `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:
  10. - The return type should match the origin method;
  11. - The parameter list should match the origin method, and an additional `Throwable` parameter can be provided to get the actual exception.
  12. - `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:
  13. - The return type should match the origin method;
  14. - parameter list should be empty, and an additional `Throwable` parameter can be provided to get the actual exception.
  15. - `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*).
  16. - `exceptionsToIgnore` (since 1.6.0): List of business exception classes that should not be traced and caught in fallback.
  17. - `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.
  18. For example:
  19. ```java
  20. @SentinelResourceBinding(value = "abc", fallback = "doFallback")
  21. public String doSomething(long i) {
  22. return "Hello " + i;
  23. }
  24. public String doFallback(long i, Throwable t) {
  25. // Return fallback value.
  26. return "fallback";
  27. }
  28. public String defaultFallback(Throwable t) {
  29. return "default_fallback";
  30. }
  31. ```
  32. ## Configuration
  33. 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 :
  34. ```
  35. <?xml version="1.0" encoding="UTF-8"?>
  36. <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  37. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  38. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" bean-discovery-mode="all" version="2.0">
  39. <interceptors>
  40. <class>com.alibaba.csp.sentinel.annotation.cdi.interceptor.SentinelResourceInterceptor</class>
  41. </interceptors>
  42. </beans>
  43. ```