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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Sentinel Spring MVC Interceptor
  2. Sentinel provides Spring MVC Interceptor integration to enable flow control for web requests, And support url like '/foo/{id}'
  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-spring-webmvc-adapter</artifactId>
  8. <version>x.y.z</version>
  9. </dependency>
  10. ```
  11. Configure interceptor
  12. ```java
  13. @Configuration
  14. public class InterceptorConfig implements WebMvcConfigurer {
  15. @Override
  16. public void addInterceptors(InterceptorRegistry registry) {
  17. //Add sentinel interceptor
  18. addSpringMvcInterceptor(registry);
  19. //If you want to sentinel the total flow, you can add total interceptor
  20. addSpringMvcTotalInterceptor(registry);
  21. }
  22. private void addSpringMvcInterceptor(InterceptorRegistry registry) {
  23. //Configure
  24. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  25. //Custom configuration if necessary
  26. config.setHttpMethodSpecify(true);
  27. config.setOriginParser(request -> request.getHeader("S-user"));
  28. //Add sentinel interceptor
  29. registry.addInterceptor(new SentinelInterceptor(config)).addPathPatterns("/**");
  30. }
  31. private void addSpringMvcTotalInterceptor(InterceptorRegistry registry) {
  32. //Configure
  33. SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig();
  34. //Custom configuration if necessary
  35. config.setRequestAttributeName("my_sentinel_spring_mvc_total_entity_container");
  36. config.setTotalResourceName("my-spring-mvc-total-url-request");
  37. //Add sentinel interceptor
  38. registry.addInterceptor(new SentinelTotalInterceptor(config)).addPathPatterns("/**");
  39. }
  40. }
  41. ```
  42. Configure 'BlockException' handler, there are three options:
  43. 1. Global exception handling in spring MVC. <Recommend>
  44. ```java
  45. @ControllerAdvice
  46. @Order(0)
  47. public class SentinelSpringMvcBlockHandlerConfig {
  48. private Logger logger = LoggerFactory.getLogger(this.getClass());
  49. @ExceptionHandler(BlockException.class)
  50. @ResponseBody
  51. public String sentinelBlockHandler(BlockException e) {
  52. AbstractRule rule = e.getRule();
  53. logger.info("Blocked by sentinel, {}", rule.toString());
  54. return "Blocked by Sentinel";
  55. }
  56. }
  57. ```
  58. 2. Use `DefaultBlockExceptionHandler`
  59. ```java
  60. //SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig();
  61. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  62. config.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
  63. ```
  64. 3. `implements BlockExceptionHandler`
  65. ```java
  66. //SentinelWebMvcTotalConfig config = new SentinelWebMvcTotalConfig();
  67. SentinelWebMvcConfig config = new SentinelWebMvcConfig();
  68. config.setBlockExceptionHandler((request, response, e) -> {
  69. String resourceName = e.getRule().getResource();
  70. //Depending on your situation, you can choose to process or throw
  71. if ("/hello".equals(resourceName)) {
  72. //Do something ......
  73. //Write string or error page;
  74. response.getWriter().write("Blocked by sentinel");
  75. } else {
  76. //Handle it in global exception handling
  77. throw e;
  78. }
  79. });
  80. ```
  81. Configuration
  82. - Common configuration in `SentinelWebMvcConfig` and `SentinelWebMvcTotalConfig`
  83. | name | description | type | default value |
  84. |------|------------|------|-------|
  85. | blockExceptionHandler| The handler when blocked by sentinel, there are three options:<br/>1. The default value is null, you can hanlde `BlockException` in spring MVC;<br/>2.Use `DefaultBlockExceptionHandler`;<br/>3. `implements BlockExceptionHandler` | `BlockExceptionHandler` | `null` |
  86. | originParser | `RequestOriginParser` interface is useful for extracting request origin (e.g. IP or appName from HTTP Header) from HTTP request | `RequestOriginParser` | `null` |
  87. - `SentinelWebMvcConfig` configuration
  88. | name | description | type | default value |
  89. |------|------------|------|-------|
  90. | urlCleaner | The `UrlCleaner` interface is designed for clean and unify the URL resource. For REST APIs, you can to clean the URL resource (e.g. `/api/user/getById` and `/api/user/getByName` -> `/api/user/getBy*`), avoid the amount of context and will exceed the threshold | `UrlCleaner` | `null` |
  91. | requestAttributeName | Attribute name in request used by sentinel, please check record log, if it is already used, please set | `String` | sentinel_spring_mvc_entry_container |
  92. | httpMethodSpecify | Specify http method, for example: GET:/hello | `boolean` | `false` |
  93. `SentinelWebMvcTotalConfig` configuration
  94. | name | description | type | default value |
  95. |------|------------|------|-------|
  96. | totalResourceName | The resource name in `SentinelTotalInterceptor` | `String` | spring-mvc-total-url-request |
  97. | requestAttributeName | Attribute name in request used by sentinel, please check record log, if it is already used, please set | `String` | sentinel_spring_mvc_total_entry_container |