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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Sentinel OkHttp Adapter
  2. ## Introduction
  3. Sentinel provides integration for OkHttp client to enable flow control for web requests.
  4. Add the following dependency in `pom.xml` (if you are using Maven):
  5. ```xml
  6. <dependency>
  7. <groupId>com.alibaba.csp</groupId>
  8. <artifactId>sentinel-okhttp-adapter</artifactId>
  9. <version>x.y.z</version>
  10. </dependency>
  11. ```
  12. We can add the `SentinelOkHttpInterceptor` interceptor when `OkHttpClient` at initialization, for example:
  13. ```java
  14. OkHttpClient client = new OkHttpClient.Builder()
  15. .addInterceptor(new SentinelOkHttpInterceptor(new SentinelOkHttpConfig()))
  16. .build();
  17. ```
  18. ## Configuration
  19. `SentinelOkHttpConfig` configuration:
  20. | name | description | type | default value |
  21. |------|------------|------|-------|
  22. | resourcePrefix | customized resource name prefix | `String` | `okhttp:` |
  23. | resourceExtractor | customized resource extractor | `OkHttpResourceExtractor` | `DefaultOkHttpResourceExtractor` |
  24. | fallback | handle request when it is blocked | `OkHttpFallback` | `DefaultOkHttpFallback` |
  25. ### Resource Extractor
  26. We can define `OkHttpResourceExtractor` to customize the logic of extracting resource name from the HTTP request.
  27. For example: `okhttp:GET:ip:port/okhttp/back/1 ==> /okhttp/back/{id}`
  28. ```java
  29. OkHttpResourceExtractor extractor = (request, connection) -> {
  30. String resource = request.url().toString();
  31. String regex = "/okhttp/back/";
  32. if (resource.contains(regex)) {
  33. resource = resource.substring(0, resource.indexOf(regex) + regex.length()) + "{id}";
  34. }
  35. return resource;
  36. };
  37. ```
  38. The pattern of default resource name extractor is `${HTTP_METHOD}:${URL}` (e.g. `GET:/foo`).
  39. ### Fallback (Block handling)
  40. We can define `OkHttpFallback` to handle blocked request. For example:
  41. ```java
  42. public class DefaultOkHttpFallback implements OkHttpFallback {
  43. @Override
  44. public Response handle(Request request, Connection connection, BlockException e) {
  45. return new Response(myErrorBuilder);
  46. }
  47. }
  48. ```