Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

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.

Our article Spring Cloud Gateway Routing Predicate Factories explores routing factories in more detail.

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 as well as the possibility to create custom filters.

Our article Spring Cloud Gateway WebFilter Factories explores WebFilter factories in more detail.

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.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ismo
Ismo
2 years ago

Hi,

nice tutorial ! I have some questions about the Spring Cloud Gateway. Is Spring Cloud Gateway supported with “loadBalancing” like the Spring Zuul Gateway with Ribbon? Does the Spring Cloud Gateway supports FIlter like Zuul ? And is the Spring Cloud Gateway non-blocking ?

Thanks and greets.

Grzegorz Piwowarek
2 years ago
Reply to  Ismo

Spring Cloud Gateway is pretty much a facade for hiding your microservices architecture and exposing them as one thing – so this works on the server-side of things. Of course, you can throw in Ribbon which works on the client-side (at least this is how I understand it)

Spring Cloud Gateway supports filters like Zuul.
Also, what do you mean when you ask if it’s non-blocking? From the client side, it’s a standard HTTP call, when making an HTTP request, you can wrap perform it asynchronously but that’s your responsibility

Ismo
Ismo
2 years ago

Hi Grzegorz,

i mean the “LoadBalancing” on the Api-Gateway, not client-side. Zuul 1 can loadbalancing automatically with Ribbon. My question was, if the Spring-Cloud-Gateway do this also with “Ribbon” under the hood automatically ? And about the non-blocking thing, Netflix Zuul 2 (it will be released) will be full non-blocking with RxJava. My question about the non-blocking for Spring-Cloud-Gateway was, can i use for example the “Reactor WebClient” to route requests manually etc. ?

Greets

Grzegorz Piwowarek
2 years ago
Reply to  Ismo

Isn’t the Ribbon’s load balancing done on the client side?
As far as I’m aware, no server-side load balancing functionality from the Spring Cloud Gateway and it’s implemented in the standard “blocking” thread-per-request way. I’m not 100% sure but if it was not the case, they would be writing everywhere about this 🙂

Of course, you can use the Reactive WebClient with it

Anton
Anton
2 years ago
Reply to  Ismo

Nothing is said about body modifying, neither request, nor response. Is it possible?

Grzegorz Piwowarek
2 years ago
Reply to  Anton

Yes, you can do that just like in Zuul by implementing custom filters. Actually, it’s exactly the same interface as used in Zuul – ZuulFilter

Anton
Anton
2 years ago

As I can see, there are only GatewayFilterFactory and GlobalFilter and GatewayFilter interfaces to implement. But as I can implement (theoretically) request body modification, I don’t see posiblity to change RxNetty-based response body – when I try, I’m getting “java.lang.IllegalStateException: Only one connection receive subscriber allowed”. Do I miss smth?

Grzegorz Piwowarek
2 years ago
Reply to  Anton

hmm, I messed up different projects indeed.

And when it comes to the RxNetty case, it looks like a bug – you might want to start an issue on Gateway’s GitHub

Pravin
Pravin
2 years ago

I have written custom filters using gateway filter factory. How to make these filters execute before executing default routing.

Grzegorz Piwowarek
2 years ago
Reply to  Pravin

That’s a very good point – I was wondering about the same but it looks like it’s not yet supported

Anton
Anton
2 years ago

Interested in body modification, see WeirdBob kind explanations: https://github.com/spring-cloud/spring-cloud-gateway/issues/47 and his short but comprehensive example: https://gist.github.com/WeirdBob/b25569d461f0f54444d2c0eab51f3c48

I’m new to Spring 5 and rx, but Spencer’s code is pretty clean and self-explanatory. Read, learn, try.

saman
saman
2 years ago

When I get request form path for example /bar is it possible in spring cloud gateway to call multiple microservices and integrate their result (for example JSON) and send as response of /bar ? How can i do it? thanks

Comments are closed on this article!