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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Sentinel Zuul 2.x Adapter
  2. This adapter provides **route level** and **customized API level**
  3. flow control for Zuul 2.x API Gateway.
  4. > *Note*: this adapter only supports Zuul 2.x.
  5. ## How to use
  6. > You can refer to demo [`sentinel-demo-zuul2-gateway`](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-zuul2-gateway).
  7. 1. Add Maven dependency to your `pom.xml`:
  8. ```xml
  9. <dependency>
  10. <groupId>com.alibaba.csp</groupId>
  11. <artifactId>sentinel-zuul2-adapter</artifactId>
  12. <version>x.y.z</version>
  13. </dependency>
  14. ```
  15. 2. Register filters
  16. ```java
  17. filterMultibinder.addBinding().toInstance(new SentinelZuulInboundFilter(500));
  18. filterMultibinder.addBinding().toInstance(new SentinelZuulOutboundFilter(500));
  19. filterMultibinder.addBinding().toInstance(new SentinelZuulEndpoint());
  20. ```
  21. ## How it works
  22. As Zuul 2.x is based on Netty, an event-driven asynchronous model, so we use `AsyncEntry`.
  23. - `SentinelZuulInboundFilter`: This inbound filter will regard all routes (`routeVIP` in `SessionContext` by default) and all customized API as resources. When a `BlockException` caught, the filter will set endpoint to find a fallback to execute.
  24. - `SentinelZuulOutboundFilter`: When the response has no exception caught, the post filter will trace the exception and complete the entries.
  25. - `SentinelZuulEndpoint`: When an exception is caught, the filter will find a fallback to execute.
  26. ## Integration with Sentinel Dashboard
  27. 1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard).
  28. 2. You can configure the rules in Sentinel dashboard or via dynamic rule configuration.
  29. > You may need to add `-Dcsp.sentinel.app.type=1` property to mark this application as API gateway.
  30. ## Fallbacks
  31. You can implement `ZuulBlockFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
  32. The default fallback provider is `DefaultBlockFallbackProvider`.
  33. By default fallback route is proxy ID (or customized API name).
  34. Here is an example:
  35. ```java
  36. // custom provider
  37. public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
  38. private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class);
  39. // you can define root as service level
  40. @Override
  41. public String getRoute() {
  42. return "my-route";
  43. }
  44. @Override
  45. public BlockResponse fallbackResponse(String route, Throwable cause) {
  46. RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route));
  47. if (cause instanceof BlockException) {
  48. return new BlockResponse(429, "Sentinel block exception", route);
  49. } else {
  50. return new BlockResponse(500, "System Error", route);
  51. }
  52. }
  53. }
  54. // register fallback
  55. ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
  56. ```
  57. Default block response:
  58. ```json
  59. {
  60. "code":429,
  61. "message":"Sentinel block exception",
  62. "route":"/"
  63. }
  64. ```