Exploring the New Spring Cloud Gateway

1. Overview

In this article, we’ll explore the main features of the Spring Cloud Gateway project, a new API based on Spring 5, Spring Boot 2 and Project Reactor.

The tool provides out-of-the-box routing mechanisms often used in microservices applications as a way of hiding multiple services behind a single facade.

For an explanation of the Gateway pattern without the Spring Cloud Gateway project, check out our previous article.

2. Routing Handler

Being focused on routing requests, the Spring Cloud Gateway forwards requests to a Gateway Handler Mapping – which determines what should be done with requests matching a specific route.

Let’s start with a quick example of how to the Gateway Handler resolves route configurations by using RouteLocator:

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
      .route("r1", r -> r.host("**.baeldung.com")
        .and()
        .path("/baeldung")
        .uri("http://baeldung.com"))
      .route(r -> r.host("**.baeldung.com")
        .and()
        .path("/myOtherRouting")
        .filters(f -> f.prefixPath("/myPrefix"))
        .uri("http://othersite.com")
        .id("myOtherID"))
    .build();
}

Notice how we made use of the main building blocks of this API:

  • Route – the primary API of the gateway. It is defined by a given identification (ID), a destination (URI) and set of predicates and filters

  • Predicate – a Java 8’s Predicate – which is used for matching HTTP requests using headers, methods or parameters

  • Filter – a standard Spring’s WebFilter

3. Dynamic Routing

Just like Zuul, Spring Cloud Gateway provides means for routing requests to different services.

The routing configuration can be created by using pure Java (RouteLocator, as shown in the example in section 2.1) or by using properties configuration:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
      - id: baeldung
        uri: baeldung.com
      - id: myOtherRouting
        uri: localhost:9999

4. Routing Factories

Spring Cloud Gateway matches routes using the Spring WebFlux HandlerMapping infrastructure.

It also includes many built-in Route Predicate Factories. All of these predicates match different attributes of the HTTP request. Multiple Route Predicate Factories can be combined via the logical “and”.

Route matching can be applied both programmatically or via configuration properties file using a different type of Route Predicate Factories.

4.1. Before Route Predicate Factory

The Before Route Predicate Factory takes one parameter: a datetime. This predicate matches requests that happen before the current datetime:

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: http://baeldung.com
        predicates:
        - Before=2017-09-11T17:42:47.789-07:00[America/Alaska]

The Java configuration can be represented as:

//..route definition
.route(r -> r.before(LocalDateTime.now().atZone(ZoneId.systemDefault()))
.id("before_route")
.uri("http://baeldung.com")

4.2. Between Route Predicate Factory

The Between Route Predicate Factory takes two parameters: datetime1, and datetime2. This predicate matches requests that happen after datetime1 (inclusive) and before datetime2 (exclusive). The datetime2 parameter must be after datetime1:

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: http://baeldung.com
        predicates:
        - Between=2017-09-10T17:42:47.789-07:00[America/Alaska], 2017-09-11T17:42:47.789-07:00[America/Alaska]

And the Java configuration looks like this:

ZonedDateTime datetime1 = LocalDateTime.now().minusDays(1).atZone(ZoneId.systemDefault());
ZonedDateTime datetime2 = LocalDateTime.now().atZone(ZoneId.systemDefault())
//..route definition
.route(r -> r.between(datetime1, datetime2))
.id("between_route")
.uri("http://baeldung.com")

4.3. Header Route Predicate Factory

The Header Route Predicate Factory takes two parameters: the header name, and a regular expression. This predicate matches a header matching the regular expression:

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: http://baeldung.com
        predicates:
        - Header=X-Request-Id, \d+

The Java configuration can be represented as:

//..route definition
.route(r -> r.header("X-Request-Id", "\\d+")
.id("header_route")
.uri("http://baeldung.com")

4.4. Host Route Predicate Factor

The Host Route Predicate Factory takes one parameter: the hostname pattern. The pattern is an Ant-style pattern with “.” as the separator.

This predicate matches the Host header with the given the pattern:

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://baeldung.com
        predicates:
        - Host=**.baeldung.com

Here’s the Java configuration alternative:

//..route definition
.route(r -> r.host("**.baeldung.com")
.id("host_route")
.uri("http://baeldung.com")

4.5. Method Route Predicate Factory

The Method Route Predicate Factory takes one parameter: the HTTP method to match:

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://baeldung.com
        predicates:
        - Method=GET

The Java configuration can be represented as:

//..route definition
.route(r -> r.method("GET")
.id("method_route")
.uri("http://baeldung.com")

4.6. Path Route Predicate Factory

The Path Route Predicate Factory takes one parameter: a Spring PathMatcher pattern:

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles/{articleId}

The Java configuration:

//..route definition
.route(r -> r.path("/articles/"+articleId)
.id("path_route")
.uri("http://baeldung.com")

4.7. Query Route Predicate Factory

The Query Route Predicate Factory takes two parameters: a required param and an optional regexp:

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://baeldung.com
        predicates:
        - Query=articleId, \w

And the Java configuration:

//..route definition
.route(r -> r.query("articleId", "\w")
.id("query_route")
.uri("http://baeldung.com")

4.8. RemoteAddr Route Predicate Factory

The RemoteAddr Route Predicate Factory takes a list (minimum of 1) of CIDR-notation strings, e.g., 192.168.0.1/16 (where 192.168.0.1 is an IP address, and 16 is a subnet mask):

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://baeldung.com
        predicates:
        - RemoteAddr=192.168.1.1/24

And the corresponding Java configuration:

//..route definition
.route(r -> r.remoteAddr("192.168.1.1/24")
.id("remoteaddr_route")
.uri("http://baeldung.com")

5. WebFilter Factories

Route filters make the modification of the incoming HTTP request or outgoing HTTP response possible.

Spring Cloud Gateway includes many built-in WebFilter Factories.

5.1. AddRequestHeader WebFilter Factory

The AddRequestHeader WebFilter Factory takes a name and value parameter:

spring:
  cloud:
    gateway:
      routes:
      - id: addrequestheader_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - AddRequestHeader=X-SomeHeader, bael

Here’s the corresponding Java config:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_request"))
  .uri("http://baeldung.com")
  .id("addrequestheader_route")

5.2. AddRequestParameter WebFilter Factory

The AddRequestParameter WebFilter Factory takes a name and value parameter:

spring:
  cloud:
    gateway:
      routes:
      - id: addrequestparameter_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - AddRequestParameter=foo, bar

The corresponding Java configuration:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.addRequestParameter("foo", "bar"))
  .uri("http://baeldung.com")
  .id("addrequestparameter_route")

5.3. AddResponseHeader WebFilter Factory

The AddResponseHeader WebFilter Factory takes a name and value parameter:

spring:
  cloud:
    gateway:
      routes:
      - id: addrequestheader_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - AddResponseHeader=X-SomeHeader, Bar

The corresponding Java configuration:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.addResponseHeader("X-SomeHeader", "Bar"))
  .uri("http://baeldung.com")
  .id("addresponseheader_route")

5.4. Circuit Breaker WebFilter Factory

Hystrix is used as Circuit-Breaker WebFilter Factory and takes a single name parameter, which is the name of the Hystrix command:

spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - Hystrix=someCommand

The corresponding Java configuration:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.hystrix("some-command"))
  .uri("http://baeldung.com")
  .id("hystrix_route")

5.5. RedirectTo WebFilter Factory

The RedirectTo WebFilter Factory takes a status and a URL parameter. The status should be a 300 redirect HTTP code, such as 301:

spring:
  cloud:
    gateway:
      routes:
      - id: redirectto_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - RedirectTo=302, http://foo.bar

And the corresponding Java configuration:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.redirect("302","http://foo.bar"))
  .uri("http://baeldung.com")
  .id("redirectto_route")

5.6. RewritePath WebFilter Factory

The RewritePath WebFilter Factory takes a path regexp parameter and a replacement parameter. This uses Java regular expressions to rewrite the request path.

Here is a configuration example:

spring:
  cloud:
    gateway:
      routes:
      - id: rewritepath_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles/**
        filters:
        - RewritePath=/articles/(?<articleId>.*), /$\{articleId}

The Java configuration can be represented as:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.rewritePath("(?<articleId>.*)", articleId))
  .uri("http://baeldung.com")
  .id("rewritepath_route")

5.7. RequestRateLimiter WebFilter Factory

The RequestRateLimiter WebFilter Factory takes three parameters: replenishRate, capacity, and keyResolverName.

  • replenishRate represents how many requests per second do you want a user to be allowed to do

  • capacity defines how much bursting capacity would be allowed

  • keyResolverName – is the name of a bean that implements the KeyResolver interface

The KeyResolver interface allows pluggable strategies to derive the key for limiting requests:

spring:
  cloud:
    gateway:
      routes:
      - id: requestratelimiter_route
        uri: http://baeldung.com
        predicates:
        - Path=/articles
        filters:
        - RequestRateLimiter=10, 50, userKeyResolver

The Java configuration can be represented as:

//...route definition
.route(r -> r.path("/articles")
  .filters(f -> f.requestRateLimiter().configure(c -> c.setRateLimiter(myRateLimiter)))
  .uri("http://baeldung.com")
  .id("requestratelimiter_route")

6. Spring Cloud DiscoveryClient Support

Spring Cloud Gateway can be easily integrated with Service Discovery and Registry libraries, such as Eureka Server and Consul:

@Configuration
@EnableDiscoveryClient
public class GatewayDiscoveryConfiguration {

    @Bean
    public DiscoveryClientRouteDefinitionLocator
      discoveryClientRouteLocator(DiscoveryClient discoveryClient) {

        return new DiscoveryClientRouteDefinitionLocator(discoveryClient);
    }
}

6.1. LoadBalancerClient Filter

The LoadBalancerClientFilter looks for a URI in the exchange attribute property using ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR.

If the URL has a lb scheme (e.g., lb://baeldung-service) it’ll use the Spring Cloud LoadBalancerClient to resolve the name (i.e., baeldung-service) to an actual host and port.

The unmodified original URL is placed in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.

7. Monitoring

Spring Cloud Gateway makes use of the Actuator API, a well-known Spring-Boot library that provides several out-of-the-box services for monitoring the application.

Once the Actuator API is installed and configured, the gateway monitoring features can be visualized by accessing /gateway/ endpoint.

8. Implementation

We’ll now create a simple example of the usage of Spring Cloud Gateway as a proxy server using the path predicate.

8.1. Dependencies

The Spring Cloud Gateway is currently in the milestones repository, on version 2.0.0.RC2. This is also the version we’re using here.

To add the project, we’ll use the dependency management system:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway</artifactId>
            <version>2.0.0.RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Next, we’ll add the necessary dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

8.2. Code Implementation

And now we create a simple routing configuration in the application.yml file:

spring:
  cloud:
    gateway:
      routes:
      - id: baeldung_route
        uri: http://baeldung.com
        predicates:
        - Path=/baeldung/
management:
  endpoints:
    web:
      exposure:
        include: "*'

And the Gateway application code:

@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

After the application starts, we can access the url: “http://localhost/actuator/gateway/routes/baeldung_route” to check all routing configuration created:

{
    "id":"baeldung_route",
    "predicates":[{
        "name":"Path",
        "args":{"_genkey_0":"/baeldung"}
    }],
    "filters":[],
    "uri":"http://baeldung.com",
    "order":0
}

We see that the relative url: “/baeldung” is configured as a route, ** so hitting the url “http://localhost/baeldung” we’ll be redirected to “http://baeldung.com“, as was configured in our example.

9. Conclusion

In this article, we explored some of the features and components that are part of Spring Cloud Gateway. This new API provides out-of-the-box tools for gateway and proxy support.

The examples presented here can be found in our GitHub repository.

Leave a Reply

Your email address will not be published.