seninel部署
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Sentinel Annotation CDI extension
  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,
  6. and in order to intercept all kinds of `@SentinelResourceBinding` with different attributes in one interceptor,
  7. all attributes of `@SentinelResourceBinding` are annotated with `@Nonbinding`.
  8. The `@SentinelResourceBinding` annotation indicates a resource definition, including:
  9. - `value`: Resource name, required (cannot be empty)
  10. - `entryType`: Traffic type (inbound or outbound), `EntryType.OUT` by default
  11. - `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:
  12. - The return type should match the origin method;
  13. - The parameter list should match the origin method, and an additional `Throwable` parameter can be provided to get the actual exception.
  14. - `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:
  15. - The return type should match the origin method;
  16. - parameter list should be empty, and an additional `Throwable` parameter can be provided to get the actual exception.
  17. - `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*).
  18. - `exceptionsToIgnore`: List of business exception classes that should not be traced and caught in fallback.
  19. - `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.
  20. For example:
  21. ```java
  22. @SentinelResourceBinding(value = "abc", fallback = "doFallback")
  23. public String doSomething(long i) {
  24. return "Hello " + i;
  25. }
  26. public String doFallback(long i, Throwable t) {
  27. // Return fallback value.
  28. return "fallback";
  29. }
  30. public String defaultFallback(Throwable t) {
  31. return "default_fallback";
  32. }
  33. ```
  34. ## Configuration
  35. 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,
  36. we may add configuration in `resources/META-INF/beans.xml` like this:
  37. ```
  38. <?xml version="1.0" encoding="UTF-8"?>
  39. <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  40. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  41. 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">
  42. <interceptors>
  43. <class>com.alibaba.csp.sentinel.annotation.cdi.interceptor.SentinelResourceInterceptor</class>
  44. </interceptors>
  45. </beans>
  46. ```