seninel部署
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Sentinel Web Servlet Filter
  2. Sentinel provides Servlet filter integration to enable flow control for 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-web-servlet</artifactId>
  8. <version>x.y.z</version>
  9. </dependency>
  10. ```
  11. To activate the filter, you can simply configure your `web.xml` with:
  12. ```xml
  13. <filter>
  14. <filter-name>SentinelCommonFilter</filter-name>
  15. <filter-class>com.alibaba.csp.sentinel.adapter.servlet.CommonFilter</filter-class>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>SentinelCommonFilter</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>
  21. ```
  22. For Spring web applications you can configure with Spring bean:
  23. ```java
  24. @Configuration
  25. public class FilterConfig {
  26. @Bean
  27. public FilterRegistrationBean sentinelFilterRegistration() {
  28. FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
  29. registration.setFilter(new CommonFilter());
  30. // Set the matching URL pattern for the filter.
  31. registration.addUrlPatterns("/*");
  32. registration.setName("sentinelCommonFilter");
  33. registration.setOrder(1);
  34. // Set whether to support the specified HTTP method prefix for the filter.
  35. registration.addInitParameter(CommonFilter.HTTP_METHOD_SPECIFY, "false");
  36. return registration;
  37. }
  38. }
  39. ```
  40. When a request is blocked, Sentinel servlet filter will display a default page indicating the request is rejected.
  41. The HTTP status code of the default block page is **429 (Too Many Requests)**. You can customize it
  42. via the `csp.sentinel.web.servlet.block.status` configuration item (since 1.7.0).
  43. If customized block page is set (via `WebServletConfig.setBlockPage(blockPage)` method),
  44. the filter will redirect the request to provided URL. You can also implement your own
  45. block handler (the `UrlBlockHandler` interface) and register to `WebCallbackManager`.
  46. The `UrlCleaner` interface is designed for clean and unify the URL resource.
  47. For REST APIs, you have to clean the URL resource (e.g. `/foo/1` and `/foo/2` -> `/foo/:id`), or
  48. the amount of context and resources will exceed the threshold.
  49. If you need to exclude some URLs (that should not be recorded as Sentinel resources), you could also
  50. leverage the `UrlCleaner` interface. You may unify the unwanted URLs to the empty string `""` or `null`,
  51. then the URLs will be excluded (since Sentinel 1.6.3).
  52. The `RequestOriginParser` interface is useful for extracting request origin (e.g. IP or appName from HTTP Header)
  53. from HTTP request. You can implement your own `RequestOriginParser` and register to `WebCallbackManager`.