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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Sentinel Zuul Adapter
  2. Sentinel Zuul Adapter provides **ServiceId level** and **API Path level** flow control for Zuul gateway service.
  3. > *Note*: this adapter only support Zuul 1.x.
  4. ## How to use
  5. 1. Add Maven dependency to your `pom.xml`:
  6. ```xml
  7. <dependency>
  8. <groupId>com.alibaba.csp</groupId>
  9. <artifactId>sentinel-zuul-adapter</artifactId>
  10. <version>x.y.z</version>
  11. </dependency>
  12. ```
  13. 2. Register filters
  14. ```java
  15. // get registry
  16. final FilterRegistry r = FilterRegistry.instance();
  17. // this is property config. set filter enable
  18. SentinelZuulProperties properties = new SentinelZuulProperties();
  19. properties.setEnabled(true);
  20. // set url cleaner, here use default
  21. DefaultUrlCleaner defaultUrlCleaner = new DefaultUrlCleaner();
  22. // set origin parser. here use default
  23. DefaultRequestOriginParser defaultRequestOriginParser = new DefaultRequestOriginParser();
  24. // register filters. you must register all three filters.
  25. SentinelPreFilter sentinelPreFilter = new SentinelPreFilter(properties, defaultUrlCleaner, defaultRequestOriginParser);
  26. r.put("sentinelPreFilter", sentinelPreFilter);
  27. SentinelPostFilter postFilter = new SentinelPostFilter(properties);
  28. r.put("sentinelPostFilter", postFilter);
  29. SentinelErrorFilter errorFilter = new SentinelErrorFilter(properties);
  30. r.put("sentinelErrorFilter", errorFilter);
  31. ```
  32. ## How it works
  33. As Zuul run as per thread per connection block model, we add filters around `route Filter` to trace sentinel statistics.
  34. - `SentinelPreFilter`: Get an entry of resource, the first order is **ServiceId** (the key in RequestContext is `serviceId`, this can set in own custom filter), then **API Path**.
  35. - `SentinelPostFilter`: When success response, exit entry.
  36. - `SentinelPreFilter`: When an `Exception` caught, trace the exception and exit context.
  37. <img width="792" src="https://user-images.githubusercontent.com/9305625/47277113-6b5da780-d5ef-11e8-8a0a-93a6b09b0887.png">
  38. The order of filters can be changed in property.
  39. The invocation chain resembles this:
  40. ```bash
  41. EntranceNode: machine-root(t:3 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  42. -EntranceNode: coke(t:2 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  43. --coke(t:2 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  44. ---/coke/coke(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  45. -EntranceNode: sentinel_default_context(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  46. -EntranceNode: book(t:1 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  47. --book(t:1 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  48. ---/book/coke(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
  49. ```
  50. - `book` and `coke` are serviceId.
  51. - `/book/coke` is api path, the real API path is `/coke`.
  52. ## Integration with Sentinel Dashboard
  53. 1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard).
  54. 2. You can configure the rules in Sentinel dashboard or via dynamic rule configuration.
  55. ## Fallbacks
  56. You can implement `SentinelFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
  57. The default fallback provider is `DefaultBlockFallbackProvider`.
  58. By default fallback route is `ServiveId + URI PATH`, example `/book/coke`, first `book` is serviceId, `/coke` is URI PATH, so that both can be needed.
  59. Here is an example:
  60. ```java
  61. // custom provider
  62. public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
  63. private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class);
  64. // you can define root as service level
  65. @Override
  66. public String getRoute() {
  67. return "/coke/coke";
  68. }
  69. @Override
  70. public BlockResponse fallbackResponse(String route, Throwable cause) {
  71. RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route));
  72. if (cause instanceof BlockException) {
  73. return new BlockResponse(429, "Sentinel block exception", route);
  74. } else {
  75. return new BlockResponse(500, "System Error", route);
  76. }
  77. }
  78. }
  79. // register fallback
  80. ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
  81. ```
  82. Default block response:
  83. ```json
  84. {
  85. "code":429,
  86. "message":"Sentinel block exception",
  87. "route":"/"
  88. }
  89. ```
  90. ## Request origin parser
  91. You can register customized request origin parser like this:
  92. ```java
  93. public class MyRequestOriginParser implements RequestOriginParser {
  94. @Override
  95. public String parseOrigin(HttpServletRequest request) {
  96. return request.getRemoteAddr();
  97. }
  98. }
  99. ```