seninel部署
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Sentinel Zuul Adapter
  2. Sentinel Zuul Adapter provides **route level** and **customized API level**
  3. flow control for Zuul API Gateway.
  4. > *Note*: this adapter only support Zuul 1.x.
  5. ## How to use
  6. 1. Add Maven dependency to your `pom.xml`:
  7. ```xml
  8. <dependency>
  9. <groupId>com.alibaba.csp</groupId>
  10. <artifactId>sentinel-zuul-adapter</artifactId>
  11. <version>x.y.z</version>
  12. </dependency>
  13. ```
  14. 2. Register filters
  15. For Spring Cloud Zuul users, we only need to inject the three filters in Spring configuration class like this:
  16. ```java
  17. @Configuration
  18. public class ZuulConfig {
  19. @Bean
  20. public ZuulFilter sentinelZuulPreFilter() {
  21. // We can provider the filter order here.
  22. return new SentinelZuulPreFilter(10000);
  23. }
  24. @Bean
  25. public ZuulFilter sentinelZuulPostFilter() {
  26. return new SentinelZuulPostFilter(1000);
  27. }
  28. @Bean
  29. public ZuulFilter sentinelZuulErrorFilter() {
  30. return new SentinelZuulErrorFilter(-1);
  31. }
  32. }
  33. ```
  34. For original Zuul users:
  35. ```java
  36. // Get filter registry
  37. final FilterRegistry r = FilterRegistry.instance();
  38. // We need to register all three filters.
  39. SentinelZuulPreFilter sentinelPreFilter = new SentinelZuulPreFilter();
  40. r.put("sentinelZuulPreFilter", sentinelPreFilter);
  41. SentinelZuulPostFilter postFilter = new SentinelZuulPostFilter();
  42. r.put("sentinelZuulPostFilter", postFilter);
  43. SentinelZuulErrorFilter errorFilter = new SentinelZuulErrorFilter();
  44. r.put("sentinelZuulErrorFilter", errorFilter);
  45. ```
  46. ## How it works
  47. As Zuul run as per thread per connection block model, we add filters around route filter to trace Sentinel statistics.
  48. - `SentinelZuulPreFilter`: This pre-filter will regard all proxy ID (`proxy` in `RequestContext`) and all customized API as resources. When a `BlockException` caught, the filter will try to find a fallback to execute.
  49. - `SentinelZuulPostFilter`: When the response has no exception caught, the post filter will complete the entries.
  50. - `SentinelZuulPreFilter`: When an exception is caught, the filter will trace the exception and complete the entries.
  51. <img width="792" src="https://user-images.githubusercontent.com/9305625/47277113-6b5da780-d5ef-11e8-8a0a-93a6b09b0887.png">
  52. The order of filters can be changed via the constructor.
  53. The invocation chain resembles this:
  54. ```bash
  55. -EntranceNode: sentinel_gateway_context$$route$$another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:8 1mb:1 1mt:9)
  56. --another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:1 1mt:5)
  57. --another_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:0 1mt:4)
  58. -EntranceNode: sentinel_gateway_context$$route$$my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:6 1mb:0 1mt:6)
  59. --my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2)
  60. --some_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2)
  61. ```
  62. ## Integration with Sentinel Dashboard
  63. 1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard).
  64. 2. You can configure the rules in Sentinel dashboard or via dynamic rule configuration.
  65. ## Fallbacks
  66. You can implement `SentinelFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
  67. The default fallback provider is `DefaultBlockFallbackProvider`.
  68. By default fallback route is proxy ID (or customized API name).
  69. Here is an example:
  70. ```java
  71. // custom provider
  72. public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
  73. private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class);
  74. // you can define root as service level
  75. @Override
  76. public String getRoute() {
  77. return "my-route";
  78. }
  79. @Override
  80. public BlockResponse fallbackResponse(String route, Throwable cause) {
  81. RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route));
  82. if (cause instanceof BlockException) {
  83. return new BlockResponse(429, "Sentinel block exception", route);
  84. } else {
  85. return new BlockResponse(500, "System Error", route);
  86. }
  87. }
  88. }
  89. // register fallback
  90. ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
  91. ```
  92. Default block response:
  93. ```json
  94. {
  95. "code":429,
  96. "message":"Sentinel block exception",
  97. "route":"/"
  98. }
  99. ```
  100. ## Request origin parser
  101. You can register customized request origin parser like this:
  102. ```java
  103. public class MyRequestOriginParser implements RequestOriginParser {
  104. @Override
  105. public String parseOrigin(HttpServletRequest request) {
  106. return request.getRemoteAddr();
  107. }
  108. }
  109. ```