Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

In this tutorial, 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 the Gateway Handler resolves route configurations by using RouteLocator:

public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
      .route("r1", r -> r.host("**.baeldung.com")
      .route(r -> r.host("**.baeldung.com")
        .filters(f -> f.prefixPath("/myPrefix"))

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 Predicate — which is used for matching HTTP requests using headers, methods or parameters
  • Filter — a standard Spring 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) or by using properties configuration:

    name: gateway-service  
      - 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 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 and 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:

public class GatewayDiscoveryConfiguration {
    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:


Next, we'll add the necessary dependencies:


8.2. Code Implementation

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

      - id: baeldung_route
        uri: http://baeldung.com
        - Path=/baeldung/
        include: "*'

And here's the Gateway application code:

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:


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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Cloud footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!