Build API Gateway using Spring Cloud Gateway

I showed you how to build an API Gateway using Zuul Proxy of Spring Cloud Netflix. In this tutorial, I introduce you to another way, using another library in the Spring framework ecosystem, that is, Spring Cloud Gateway!

To put it simply, in principle, an API Gateway will allow us to configure it so that when one request comes to it, this request will be forwarded to this service, and the other will be forwarded to that service. Spring Cloud Gateway has the same function. The Spring Cloud Gateway supports us with filters, so we can change any request to internal services such as adding request parameters, changing request URLs, adding headers, and so on… We can add new custom filters, meaning you can write Java code, to add any other processing logic before a request is actually forwarded to the target service.

As an example for this tutorial, to simply give you an overview of how Spring Cloud Gateway works, I will create a new application using Spring Cloud Gateway as the API Gateway and two other service projects, Student Service and Class Service, as the services that we need to call. Depending on the user’s request, this Gateway application will forward the request to the corresponding service.

The high-level architecture of this application is as follows:

The application project has the following content:

Now, I will implement 2 services, Student Service and Class Service, 2 simple APIs, returning 2 simple lines of text. When the user requests to the Gateway application, depending on the request, the user will receive the corresponding response!

Student Service

This service is a Spring Boot application. The simple API that I will expose for this service will be as follows:

As you can see, I only expose the endpoint “/students/hello” with the response being the line “Hello from Student Service”.

This service will run on port 8081:

Class Service

Similar to Student Service, Class Service is also a Spring Boot application and it also exposes a simple endpoint as follows:

This service will run on port 8082:

API Gateway

The API Gateway application will also be a Spring Boot project with Spring Cloud Gateway dependency to act as an API Gateway.

To configure services in Spring Cloud Gateway, we have 2 ways:

  • Using properties
  • Or using Java code provided by Spring Cloud Gateway.

In this tutorial, I will guide you to use properties!

We will use a set with at least the following properties to configure a service:

In which:

  • service_name is the name of the service
  • service_uri is the host and port information of the service
  • predicates is the list of requests that the API Gateway will catch up and forward requests from users to that service.

You can define multiple services, each service is an element in the routes:

In my example, you can configure the information of the above services in API Gateway as follows:

With the above declaration, requests to the Gateway application with endpoints starting with /students will be forwarded to the Student Service, and requests with endpoints starting with /classes will be forwarded to the Class Service!

My Gateway application will run on the default port 8080, so now if you run the API Gateway application and the 2 services above, then request to the API Gateway with the request http://localhost:8080/classes/hello, you will see the following result:

If you request to http://localhost:8080/students/hello, you will see the response from Student Service as follows:

So we have successfully built a simple API Gateway application that can handle requests and forward requests to the corresponding backend services!

Add Comment