How to Use API Gateway with Spring Cloud

In this post, I will show how we can use the API Gateway pattern with Spring Cloud. With microservice architecture becoming more and more useful, it has become equally complex how to handle calls to the microservices.

The purpose of microservices is to decouple the application into loosely coupled microservices that can interact with clients and with each other easily.

Importantly, the ease of development and deployment make microservices easier to design based on specific needs.

API Gateway Design Pattern

When the enterprise architecture scales, it becomes complicated with the number of microservices. Clients can directly call these microservices, but there are a few challenges

  1. Each client has to make a request to the exposed microservice API. In many cases, it might have to make multiple server round trips. As a result of this, it increases network latency.
  2. Every microservice has to implement common functionalities like authentication, logging, and authorization.
  3. It becomes harder to change microservice without affecting the client. In reality, the client doesn’t need to know microservice and its implementation behind.

To address these issues, the architecture now contains another layer between the client and the microservices. This is API Gateway.

API Gateway acts like a proxy that routes the request to the appropriate microservices and returns a response to the client. Microservices can also interact with each other through this Gateway.

API Gateway with Spring Cloud

Usage of API Gateway

There are a few functionalities that API Gateway provides.

Routing

The major usage of API Gateway is routing the request from the client to the appropriate server or microservice. Particularly, API Gateway hides the implementation of API from the client.

Common Functionalities

API Gateway can also implement extra common functionalities and in-process reducing the load from microservices.  These common functionalities include logging, authentication, authorization, load balancing, response caching, retry policies, circuit breakers, rate limiter.

Different API Gateways

There are a number of API Gateways available and one can use any of these based on the needs.

  • Netflix API Gateway (Zuul)
  • Amazon API Gateway
  • Mulesoft
  • Kong API Gateway
  • Azure API Gateway

Overall, which API Gateway to use will depend on your use case. But the most of these gateways provide options to scale, flexibility and support.

In this demo, I will be showing how to use spring-cloud-starter-netflix-zuul library for Netflix API Gateway.

Example of API Gateway with Spring Cloud

However, we will develop two microservices. We will also build an API Gateway using Spring Cloud. This API Gateway will act as a reverse proxy to route to either of the microservices.

So let’s create the first microservice. This will contain a CustomerController like below:


@RestController
@RequestMapping("/customer")
public class CustomerController
{
    @GetMapping("/total")
    public List customers()
    {
        List list = new ArrayList<>();
        list.add("Microsoft");
        list.add("Amazon");
        list.add("Apple");
        return list;
    }
}

This microservice will be running on port 8081. server.port=8081.

Now, let’s create another microservice. This will contain VendorController like below:


@RestController
@RequestMapping("/vendor")
public class VendorController
{
    @GetMapping("/total")
    public List vendors()
    {
        List list = new ArrayList<>();
        list.add("CJI Consultants");
        list.add("Signature Consultants");
        list.add("Deloitte");
        return list;
    }
}

This microservice will be running on port 8082. server.port=8082

API Gateway with Spring Cloud

After all, we will create an API Gateway using Spring Cloud. We need to include the following dependencies:

dependencies {
 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
 implementation 'org.springframework.boot:spring-boot-starter-webflux'
 implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
 testImplementation 'org.springframework.boot:spring-boot-starter-test'
 testImplementation 'io.projectreactor:reactor-test'
}

Note the dependency of spring-cloud-starter-gateway. Nevertheless, we will need a RouteLocator type bean to route our requests. This is where we add the configuration in our Api Gateway.

package com.betterjavacode.apigatewaydemo.config;


import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringCloudConfig
{
    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder routeLocatorBuilder)
    {
        return routeLocatorBuilder.routes()
                .route("customerModule", rt -> rt.path("/customer/**")
                        .uri("http://localhost:8081/"))
                .route("vendorModule", rt -> rt.path("/vendor/**")
                        .uri("http://localhost:8082/"))
                .build();

    }
}

As shown above, this configuration bean builds a RouteLocator to route requests related to two modules. Also, note that our gateway service is running at port 8080. If a request is initiated with a gateway address, the API gateway will route it to the appropriate service.

Demo

Let’s start out our microservices and API Gateway service. Two microservices are running on ports 8081 and 8082. The API gateway service is running on port 8080.

Now if I access http://localhost:8080/vendor/total, I will get the list of vendors as follows:

API Gateway with Spring Cloud - List of Vendors

If I access http://localhost:8080/customer/total, I will get the list of customers as follows:

API Gateway with Spring Cloud - List of Customers

 

Conclusion

Conclusively, I showed how to use API Gateway with Spring Cloud. API Gateway is an important design concept. With an increasing number of microservices, it becomes important to have a common pattern that can handle a lot of the common workload of these services, and API Gateway helps with that.

My book Simplifying Spring Security is currently on discount sale if you are interested.