Pull to refresh

Introduction to Spring Cloud Gateway

An API Gateway provides a single entry point for all the microservices running downstream. The rise in popularity of microservices-driven architecture has led to the discovery of many API gateways. Some of them are Zuul, Linkerd, Nginx, Spring Cloud Gateway and many more. In this article, we will discuss mainly about Spring Cloud Gateway.

Cloud gateway as a Non- Blocking Gateway


Many e-commerce giants such as Amazon, Alibaba has hundred of microservices running and get millions of traffic. To accommodate this traffic, a blocking gateway is definitely not a solution.

A Blocking gateway such as Zuul1 requires as many threads as no of requests to handle requests coming to an API gateway and hence a lot of resources is required to handle these requests.
Hence, spring cloud team provided their own non-blocking gateway solution as Spring Cloud Gateway — a reactive Gateway built upon Project Reactor, Spring WebFlux, and Spring Boot 2.0.
A non-blocking model requires less no of resources to serve the same amount of requests as compared to blocking gateway.

Spring Cloud Gateway Architecture


As we discussed above, the spring cloud gateway is a non-blocking gateway and hence it is built upon Project Reactor and does not work in a traditional Servlet Container. Below is the architecture diagram of Spring Cloud.

image

Once a request reaches to the gateway, the first thing gateway does is to match the request with each of the available route based on the predicate defined. Once, the route has matched the request moves to web handler and the filters will be applied to the request.

Spring Cloud Gateway Implementation


To get started with the implementation, head over to start.spring.io and download a sample spring boot project which includes gateway artifact and import into your IDE. The complete source code can be found here.

Once, the project is imported, you can start implementing the routes.

Routing in Spring Cloud



A route is a basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if the aggregate predicate is true.
Below is a simple route implementation that routes all the request that matches with the regex /api/v1/first/ to FIRST-SERVICE. In the meantime, you can define multiple out of the box filters to modify the request at the gateway level.

builder.routes()
                .route(r -> r.path("/api/v1/first/**")
                        .filters(f -> f.rewritePath("/api/v1/first/(?.*)", "/${remains}")
                                .addRequestHeader("X-first-Header", "first-service-header")
                        .uri("lb://FIRST-SERVICE/")
                        .id("first-service"))


In the above request, we have used in-built filters to rewrite the URL and add a custom header as X-first-Header on the fly.

Below is the controller class implementation of our first microservice that will handle the request for /api/v1/first/test

@RestController
public class FirstController {

    @GetMapping("/test")
    public String test(@RequestHeader("X-first-Header") String headerValue){
        return headerValue;
    }

}

Conclusion


It is very easy to get started with Spring cloud gateway and it provides excellent integration with Spring Cloud. It provides many filters out of the box and also provides flexibility to write custom filter implementations.
Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.
Change theme settings