seninel部署
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Sentinel Spring MVC Adapter
  2. ## Introduction
  3. Sentinel provides integration for Spring Web 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-spring-webmvc-adapter</artifactId>
  9. <version>x.y.z</version>
  10. </dependency>
  11. ```
  12. Then we could add a configuration bean to configure the interceptor:
  13. ```java
  14. @Configuration
  15. public class InterceptorConfig implements WebMvcConfigurer {
  16. @Override
  17. public void addInterceptors(InterceptorRegistry registry) {
  18. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  19. // Enable the HTTP method prefix.
  20. config.setHttpMethodSpecify(true);
  21. // Add to the interceptor list.
  22. registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**");
  23. }
  24. }
  25. ```
  26. Then Sentinel will extract URL patterns defined in Web Controller as the web resource (e.g. `/foo/{id}`).
  27. ## Configuration
  28. ### Block handling
  29. Sentinel Spring Web adapter provides a `BlockExceptionHandler` interface to handle the blocked requests.
  30. We could set the handler via `SentinelWebMvcTotalConfig#setBlockExceptionHandler()` method.
  31. By default the interceptor will throw out the `BlockException`.
  32. We need to set a global exception handler function in Spring to handle it. An example:
  33. ```java
  34. @ControllerAdvice
  35. @Order(0)
  36. public class SentinelBlockExceptionHandlerConfig {
  37. private Logger logger = LoggerFactory.getLogger(this.getClass());
  38. @ExceptionHandler(BlockException.class)
  39. @ResponseBody
  40. public String sentinelBlockHandler(BlockException e) {
  41. AbstractRule rule = e.getRule();
  42. logger.info("Blocked by Sentinel: {}", rule.toString());
  43. return "Blocked by Sentinel";
  44. }
  45. }
  46. ```
  47. We've provided a `DefaultBlockExceptionHandler`. When a request is blocked, the handler will return a default page
  48. indicating the request is rejected (`Blocked by Sentinel (flow limiting)`).
  49. The HTTP status code of the default block page is **429 (Too Many Requests)**.
  50. We could also implement our implementation of the `BlockExceptionHandler` interface and
  51. set to the config object. An example:
  52. ```java
  53. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  54. config.setBlockExceptionHandler((request, response, e) -> {
  55. String resourceName = e.getRule().getResource();
  56. // Depending on your situation, you can choose to process or throw
  57. if ("/hello".equals(resourceName)) {
  58. // Do something ......
  59. response.getWriter().write("Blocked by Sentinel");
  60. } else {
  61. // Handle it in global exception handling
  62. throw e;
  63. }
  64. });
  65. ```
  66. ### Customized configuration
  67. - Common configuration in `SentinelWebMvcConfig` and `SentinelWebMvcTotalConfig`:
  68. | name | description | type | default value |
  69. |------|------------|------|-------|
  70. | `blockExceptionHandler`| The handler that handles the block request | `BlockExceptionHandler` | null (throw out the BlockException) |
  71. | `originParser` | Extracting request origin (e.g. IP or appName from HTTP Header) from HTTP request | `RequestOriginParser` | - |
  72. - `SentinelWebMvcConfig` configuration:
  73. | name | description | type | default value |
  74. |------|------------|------|-------|
  75. | urlCleaner | The `UrlCleaner` interface is designed for clean and unify the URL resource. | `UrlCleaner` | - |
  76. | requestAttributeName | Attribute key in request used by Sentinel (internal) | `String` | `$$sentinel_spring_web_entry_attr` |
  77. | httpMethodSpecify | Specify whether the URL resource name should contain the HTTP method prefix (e.g. `POST:`). | `boolean` | `false` |
  78. | webContextUnify | Specify whether unify web context(i.e. use the default context name). | `boolean` | `true` |
  79. - `SentinelWebMvcTotalConfig` configuration:
  80. | name | description | type | default value |
  81. |------|------------|------|-------|
  82. | totalResourceName | The resource name in `SentinelTotalInterceptor` | `String` | `spring-mvc-total-url-request` |
  83. | requestAttributeName | Attribute key in request used by Sentinel (internal) | `String` | `$$sentinel_spring_web_total_entry_attr` |