Spring Cloud Microservices — Part 2— Integrating API Gateway

Okan Ardıç
3 min readOct 12, 2022

--

Communicating with Microservices via API Gateway

Introduction

To see the complete list of Spring Cloud Microservices tutorial series, you can check this link.

Let’s assume that we have multiple microservices with replicas, and they are already registered to a discovery service (naming server) such as Eureka. Now, we need a proper way to communicate with these services and also load balance requests between service replicas. API Gateway is a good solution to handle these issues.

API Gateway is connected to the discovery server and acts as a gateway to access the registered services with additional benefits such as load balancing requests, filtering requests, providing an authentication layer, logging requests etc. Instead of calling services directly, requests are made towards the API Gateway, and it forwards the requests to the appropriate services on behalf of us. You can also use it as an authentication layer, eliminating the need for implementing an authentication mechanism on every service.

1- Adding Dependencies

To start an API Gateway service add the following dependencies to pom.xml:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

spring-cloud-starter-gateway and spring-cloud-starter-netflix-eureka-client dependencies are required to start the API Gateway and integrate with the service discovery (i.e. Eureka Server).

2- Configuring Gateway Routes

Configure application.yml file as follows:

server:
port: 9191
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: USER-SERVICE
uri: lb://USER-SERVICE
predicates:
- Path=/users/**
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
hostname: localhost

spring.cloud.gateway.discovery.locator.enabled = true is required to enable API Gateway to be able to forward requests to the registered services by service discovery (i.e. Netflix Eureka).

spring.cloud.gateway.discovery.locator.lower-case-service-id = true lets us keep the service names in lower case in URLs (ex. http://api-gateway/user-service instead of http://api-gateway/USER-SERVICE), since Eureka capitalizes service names. You can configure it as per your requirements.

routes section is the place where the request matching is made. In the above example we are matching the requests made to the path /users/** will be forwarded to User Service. For example, a request made to http://<api-gateway-host>/users/1 will be transformed to http://<user-service-host>/users/1. There are several predicates supported by Spring Cloud Gateway, you can find them all at this link.

3- Accessing Services via API Gateway

Add @EnableEurekaClient to one of the configuration classes like the example below:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}

To give an example, let’s say that we have an API gateway hosted at http://localhost:9191 and a service called user-service which returns the user with ID = 1 with the URL http://localhost:8080/users/1. All of the following URLs can be used to dispatch request to the same endpoint:

http://localhost:9191/users/1
http://localhost:9191/user-service/users/1
http://localhost:9191/api-gateway/users/1

user-service and api-gateway will be automatically rewritten by Spring org.springframework.cloud.gateway.filter.GatewayFilter and requests will be routed to the correct service.

Conclusion

In this tutorial, we have seen how to integrate API Gateway with microservices and how it interacts with the discovery server to communicate with the microservices.

In the next tutorial, we will discuss about OpenFeign which is used to simplify service-to-service calls.

Next Tutorial: Using Feign for Simplifying REST Calls

Source Code

You can download the complete source code of this tutorial series from this link.

References

https://cloud.spring.io/spring-cloud-gateway/reference/html/appendix.html

--

--