seninel部署
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Sentinel adapter for JAX-RS
  2. Sentinel provides integration to enable fault-tolerance and flow control for JAX-RS web requests.
  3. Add the following dependency in `pom.xml` (if you are using Maven):
  4. ```xml
  5. <dependency>
  6. <groupId>com.alibaba.csp</groupId>
  7. <artifactId>sentinel-jax-rs-adapter</artifactId>
  8. <version>x.y.z</version>
  9. </dependency>
  10. ```
  11. ## SentinelJaxRsProviderFilter
  12. The `SentinelJaxRsProviderFilter` is auto activated in pure JAX-RS application.
  13. For Spring web applications you can configure with Spring bean:
  14. ```java
  15. @Configuration
  16. public class FilterConfig {
  17. @Bean
  18. public SentinelJaxRsProviderFilter sentinelJaxRsProviderFilter() {
  19. return new SentinelJaxRsProviderFilter();
  20. }
  21. }
  22. ```
  23. ## DefaultExceptionMapper
  24. Sentinel provides DefaultExceptionMapper to map Throwable to Response (with Status.INTERNAL_SERVER_ERROR),
  25. in order to let SentinelJaxRsProviderFilter to be called and exit the Sentinel entry.
  26. According to `3.3.4 Exceptions` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
  27. > Checked exceptions and throwables that have not been mapped and cannot be thrown directly MUST be wrapped in a container-specific exception that is then thrown and allowed to propagate to the underlying container.
  28. If WebApplicationException or its subclasses are thrown, they'll be automatically converted to `Response` and can enter response filter.
  29. If other kind of exceptions were thrown, and not matched by custom exception mapper, then the response filter cannot be called.
  30. For this case, a default exception mapper maybe introduced.
  31. According to `4.4 Exception Mapping Providers` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
  32. > When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception. If two or more exception providers are applicable, the one with the highest priority MUST be chosen as described in Section 4.1.3.
  33. If user also provides customized exception mapper of `Throwable`, then user has the responsibility to convert it to response and then the response filter can be called.
  34. As describe in `6.7.1 exceptions` of [jaxrs-2_1-final-spec](https://download.oracle.com/otn-pub/jcp/jaxrs-2_1-final-eval-spec/jaxrs-2_1-final-spec.pdf):
  35. > A response mapped from an exception MUST be processed using the ContainerResponse filter chain and the WriteTo interceptor chain (if an entity is present in the mapped response).
  36. ## SentinelJaxRsClientTemplate
  37. For jax-rs client, we provide `SentinelJaxRsClientTemplate` you can use it like this:
  38. ```
  39. Response response = SentinelJaxRsClientTemplate.execute(resourceName, new Supplier<Response>() {
  40. @Override
  41. public Response get() {
  42. return client.target(host).path(url).request()
  43. .get();
  44. }
  45. });
  46. ```
  47. or executeAsync like this:
  48. ```
  49. Future<Response> future = SentinelJaxRsClientTemplate.executeAsync(resourceName, new Supplier<Future<Response>>() {
  50. @Override
  51. public Future<Response> get() {
  52. return client.target(host).path(url).request()
  53. .async()
  54. .get();
  55. }
  56. });
  57. ```
  58. When a request is blocked, Sentinel JAX-RS filter will return Response with status of `TOO_MANY_REQUESTS` indicating the request is rejected.
  59. You can customize it by implement your own `SentinelJaxRsFallback` and register to `SentinelJaxRsConfig`.