seninel部署
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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())
  16. .build();
  17. ```
  18. ## Configuration
  19. - `SentinelOkHttpConfig` configuration:
  20. | name | description | type | default value |
  21. |------|------------|------|-------|
  22. | extractor | custom resource extractor | `OkHttpResourceExtractor` | `DefaultOkHttpResourceExtractor` |
  23. | fallback | handle request when it is blocked | `OkHttpFallback` | `DefaultOkHttpFallback` |
  24. ### extractor (resource extractor)
  25. We can define `OkHttpResourceExtractor` to custom resource extractor replace `DefaultOkHttpResourceExtractor`, for example: okhttp:GET:ip:port/okhttp/back/1 ==> /okhttp/back/{id}
  26. ```java
  27. OkHttpResourceExtractor extractor = (request, connection) -> {
  28. String resource = request.url().toString();
  29. String regex = "/okhttp/back/";
  30. if (resource.contains(regex)) {
  31. resource = resource.substring(0, resource.indexOf(regex) + regex.length()) + "{id}";
  32. }
  33. return resource;
  34. };
  35. SentinelOkHttpConfig.setExtractor(extractor);
  36. ```
  37. ### fallback (Block handling)
  38. We can define `OkHttpFallback` to handle request is blocked according to the actual scenario, for example:
  39. ```java
  40. public class DefaultOkHttpFallback implements OkHttpFallback {
  41. @Override
  42. public Response handle(Request request, Connection connection, BlockException e) {
  43. // Just wrap and throw the exception.
  44. throw new SentinelRpcException(e);
  45. }
  46. }
  47. ```