seninel部署
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 support Zuul 2.x.
  5. ## How to use
  6. > You can refer to 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, a event-drive model, so we use `AsyncEntry` to do flow control.
  23. - `SentinelZuulInboundFilter`: This inbound filter will regard all proxy ID (`proxy` in `SessionContext`) 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. ## Fallbacks
  30. You can implement `ZuulBlockFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
  31. The default fallback provider is `DefaultBlockFallbackProvider`.
  32. By default fallback route is proxy ID (or customized API name).
  33. Here is an example:
  34. ```java
  35. // custom provider
  36. public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
  37. private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class);
  38. // you can define root as service level
  39. @Override
  40. public String getRoute() {
  41. return "my-route";
  42. }
  43. @Override
  44. public BlockResponse fallbackResponse(String route, Throwable cause) {
  45. RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route));
  46. if (cause instanceof BlockException) {
  47. return new BlockResponse(429, "Sentinel block exception", route);
  48. } else {
  49. return new BlockResponse(500, "System Error", route);
  50. }
  51. }
  52. }
  53. // register fallback
  54. ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
  55. ```
  56. Default block response:
  57. ```json
  58. {
  59. "code":429,
  60. "message":"Sentinel block exception",
  61. "route":"/"
  62. }
  63. ```
  64. ## Request origin parser
  65. You can register customized request origin parser like this:
  66. ```java
  67. public class MyRequestOriginParser implements RequestOriginParser {
  68. @Override
  69. public String parseOrigin(HttpRequestMessage request) {
  70. return request.getInboundRequest().getOriginalHost() + ":" + request.getInboundRequest().getOriginalPort();
  71. }
  72. }
  73. ```