Getting Started with Spring Cloud Gateway

Reading Time: 3 minutes

Introduction

First of all, we will know what is “Spring Cloud Gateway”. Spring Cloud Gateway provides a library for building API gateways on top of Spring and Java. API gateway is the single entry point to multiple microservices. On the other hand, we can understand that any external system can not access all other microservices directly. It can access through only one common entry to other microservices i.e spring cloud API gateway. It provides a flexible way of routing requests based on a number of criteria. Also, it focuses on cross-cutting concerns such as security, resiliency, and monitoring.

In the world microservice world, the spring cloud gateway becomes an integral part of the whole system. This is because gateway proxies the actual endpoint and acts as a single cumulative service exposed backed by numerous services which the client doesn’t have clue at all. Not only this is the desired security mode also because of this isolation, but service also becomes loosely coupled.

Architecture of Spring Cloud Gateway:

Spring Cloud Gateway Architecture

Basically, the spring cloud gateway consists of 3 main blocks:

  • Route
  • Predicate
  • Filter

Route:

It consists of destination URI, a condition that has to satisfy — Or in terms of technical terms, Predicates, and one or more filters. On the other hand, we can say that the destination that we want a particular request to route to.

Predicate:

In a nutshell, a Predicate in Spring Cloud Gateway is an object that tests if the given request fulfills a given condition. For each route, we can define one or more prepositions, which, if satisfied, we accept requests for configured backend after installing any filters.

Filter:

A filter is an object used to intercept the HTTP requests and responses of your application. By using a filter, we can perform two operations at two instances − Before sending the request to the controller. Before sending a response to the client.

Tools used for Spring Cloud Gateway:

  • Command line client for http calls
  • Java IDE
  • Command line like zsh, bash, DOS command or PowerShell
  • a website and diagnosis tool which converts Http GET request data into a JSON response

Step 1: Create a project

Download the spring cloud gateway project using start.spring.io and extract it as follow:

http https://start.spring.io/starter.zip dependencies==cloud-gateway,actuator baseDir==spring-cloud-gateway-demo | tar -xzvf –

We can check easily the project is working fine or not by building and running the code and checking the Spring Boot actuator endpoint.

./mvnw package spring-boot:run

Your spring boot application is running now. Go to the browser and type in the URL bar http://localhost:8080/actuator/health. As a result, you will get a JSON-formatted message {"status":"UP"} that indicates everything is working fine.

Now stop your server by ctrl+c and let’s get back to the next section.

Step 2: Add a re-route instruction to the gateway

In this step, you need to open src/main/java/com/example/demo/DemoApplication.java in your IDE and add the following method.

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
        // Add a simple re-route from: /get to: http://httpbin.org:80
        // Add a simple "Hello:World" HTTP Header
        .route(p -> p
        .path("/get") // intercept calls to the /get path
        .filters(f -> f.addRequestHeader("Hello", "World")) // add header
        .uri("http://httpbin.org:80")) // forward to httpbin
        .build();
}

Here we are building a new route for our gateway. Any request to http: // localhost: 8080 / get will be matched with this route command and our two changes to the request will be made. The filter() method handles things like adding or changing titles, in our case setting the Hello header to the value World. Additionally, the uri () method redirects our request to the new host. It is important to note that the route/find route is saved when forwarding a message.

Now after that compile your new code and start the server again,

./mvnw package spring-boot:run

Now let’s move towards the testing part.

Step 3: Test your new Gateway

To test the gateway, send a HTTP GET request to http://localhost:8080/get and see what is it shows.

http localhost:8080/get –print=HhBb

You will get a response like this:

Conclusion:

Basically in this blog, we learned about the basics of a spring cloud gateway. Now you need to have a running Spring Cloud Gateway app and you have learned how to transfer the applications you receive to another repository. You can use this process to automatically transfer requests for your Gateway application to any other service.

For more, you can refer to the documentation https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-cloud-gateway.html

knoldus

Written by 

Hello! Currently, I am Software Consultant at Knoldus Inc. I have completed my B.Tech from Abdul Kalam Technical University. Right now I am focusing on JAVA Frameworks. Apart from that my hobbies are playing guitar and listening to music.

1 thought on “Getting Started with Spring Cloud Gateway4 min read

Comments are closed.