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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Sentinel Apache Httpclient 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-apache-httpclient-adapter</artifactId>
  9. <version>x.y.z</version>
  10. </dependency>
  11. ```
  12. We can use the `SentinelApacheHttpClientBuilder` when `CloseableHttpClient` at initialization, for example:
  13. ```java
  14. CloseableHttpClient httpclient = new SentinelApacheHttpClientBuilder().build();
  15. ```
  16. If we want to add some additional configurations, we can refer to the following code
  17. ```java
  18. HttpClientBuilder builder = new SentinelApacheHttpClientBuilder();
  19. //builder Other Definitions
  20. CloseableHttpClient httpclient = builder.build();
  21. ```
  22. ## Configuration
  23. - `SentinelApacheHttpClientConfig` configuration:
  24. | name | description | type | default value |
  25. |------|------------|------|-------|
  26. | prefix | customize resource prefix | `String` | `httpclient:` |
  27. | extractor | customize resource extractor | `ApacheHttpClientResourceExtractor` | `DefaultApacheHttpClientResourceExtractor` |
  28. | fallback | handle request when it is blocked | `ApacheHttpClientFallback` | `DefaultApacheHttpClientFallback` |
  29. ### extractor (resource extractor)
  30. We can define `ApacheHttpClientResourceExtractor` to customize resource extractor replace `DefaultApacheHttpClientResourceExtractor` at `SentinelApacheHttpClientBuilder` default config, for example: httpclient:GET:/httpclient/back/1 ==> httpclient:GET:/httpclient/back/{id}
  31. ```java
  32. SentinelApacheHttpClientConfig config = new SentinelApacheHttpClientConfig();
  33. config.setExtractor(new ApacheHttpClientResourceExtractor() {
  34. @Override
  35. public String extractor(HttpRequestWrapper request) {
  36. String contains = "/httpclient/back/";
  37. String uri = request.getRequestLine().getUri();
  38. if (uri.startsWith(contains)) {
  39. uri = uri.substring(0, uri.indexOf(contains) + contains.length()) + "{id}";
  40. }
  41. return request.getMethod() + ":" + uri;
  42. }
  43. });
  44. CloseableHttpClient httpclient = new SentinelApacheHttpClientBuilder(config).build();
  45. ```
  46. ### fallback (Block handling)
  47. We can define `ApacheHttpClientFallback` at `SentinelApacheHttpClientBuilder` default config, to handle request is blocked according to the actual scenario, for example:
  48. ```java
  49. public class DefaultApacheHttpClientFallback implements ApacheHttpClientFallback {
  50. @Override
  51. public CloseableHttpResponse handle(HttpRequestWrapper request, BlockException e) {
  52. // Just wrap and throw the exception.
  53. throw new SentinelRpcException(e);
  54. }
  55. }
  56. ```