Sentinel supports flow control for API gateways including Spring Cloud Gateway and Zuul 1.x/2.x.

image

Sentinel 1.6.0 introduced the sentinel-api-gateway-adapter-common module, which includes common management logic for gateway rules and customized API groups.

  • GatewayFlowRule: The flow rule specifically designed for API gateway, enabling flow control for different routes or customized API groups. It also supports flow control by request attributes (e.g. HTTP header, client IP and URL parameters).
  • ApiDefinition: The user-defined API group, which can be regarded as a group of URL patterns.

Spring Cloud Gateway

Sentinel provides the integration module with Spring Cloud Gateway, providing two levels of flow control:

  • Route level: flow control for defined routes in Spring properties file.
  • Customized API group level: flow control for customized API groups defined in GatewayApiDefinitionManager of Sentinel.

Add the following dependency in pom.xml (if you are using Maven):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>

Then you only need to inject the corresponding SentinelGatewayFilter and SentinelGatewayBlockExceptionHandler instance in Spring configuration. For example:

  1. @Configuration
  2. public class GatewayConfiguration {
  3. private final List<ViewResolver> viewResolvers;
  4. private final ServerCodecConfigurer serverCodecConfigurer;
  5. public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
  6. ServerCodecConfigurer serverCodecConfigurer) {
  7. this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
  8. this.serverCodecConfigurer = serverCodecConfigurer;
  9. }
  10. @Bean
  11. @Order(-1)
  12. public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
  13. // Register the block exception handler for Spring Cloud Gateway.
  14. return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
  15. }
  16. @Bean
  17. public GlobalFilter sentinelGatewayFilter() {
  18. // By default the order is HIGHEST_PRECEDENCE
  19. return new SentinelGatewayFilter();
  20. }
  21. }

The gateway adapter will regard all routeId (defined in Spring properties) and all customized API definitions (defined in GatewayApiDefinitionManager of sentinel-api-gateway-adapter-common module) as resources.

You can register various kinds of customized callback in GatewayCallbackManager:

  • setBlockHandler: register a customized BlockRequestHandler to handle the blocked request. The default implementation is DefaultBlockRequestHandler, which returns default message like Blocked by Sentinel: FlowException.

Zuul 1.x

Sentinel provides the integration module with Zuul 1.x, providing two levels of flow control:

  • Route level: flow control for defined routes in Zuul properties file.
  • Customized API group level: flow control for customized API groups defined in GatewayApiDefinitionManager of Sentinel.

To introduce Sentinel to your Zuul gateway, add the following dependency in pom.xml (if you are using Maven):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-zuul-adapter</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>

Then for Spring Cloud Zuul users, we only need to inject the three filters in Spring configuration class like this:

  1. @Configuration
  2. public class ZuulConfig {
  3. @Bean
  4. public ZuulFilter sentinelZuulPreFilter() {
  5. // We can provider the filter order here.
  6. return new SentinelZuulPreFilter(10000);
  7. }
  8. @Bean
  9. public ZuulFilter sentinelZuulPostFilter() {
  10. return new SentinelZuulPostFilter(1000);
  11. }
  12. @Bean
  13. public ZuulFilter sentinelZuulErrorFilter() {
  14. return new SentinelZuulErrorFilter(-1);
  15. }
  16. }

Zuul 2.x

Sentinel provides the integration module with Zuul 2.x, providing two levels of flow control:

  • Route level: flow control for defined routes (routeVIP in SessionContext)
  • Customized API group level: flow control for customized API groups defined in GatewayApiDefinitionManager of Sentinel.

To introduce Sentinel to your Zuul 2.x API gateway, add the following dependency in pom.xml (if you are using Maven):

  1. <dependency>
  2. <groupId>com.alibaba.csp</groupId>
  3. <artifactId>sentinel-zuul2-adapter</artifactId>
  4. <version>x.y.z</version>
  5. </dependency>