Dynamic Routing and Service Discovery in API Gateway
Last Updated :
20 May, 2024
API gateways are an integral part of a microservice architecture that act as a single point of entry for customers to interact with microservices. The API gateway efficiently processes incoming requests and distributes them to the appropriate microservices. Dynamic routing involves routing client requests to different microservices based on certain parameters such as the request URL, HTTP header, or request payload. The service discovery API gateway allows you to dynamically find instances of available microservices without having to hardcode their locations.
Spring Boot typically implements dynamic routing and service discovery using frameworks such as Spring Cloud Routing and Eureka.
Step-by-step Implementation of the Dynamic Routing and Service Discovery in API Gateway
Below are the steps to implement Dynamic Routing and Service Discovery in API Gateway.
Set up the Eureka-Server
Step 1: Create a Spring project using Spring Initializr and add the below dependencies:
Dependencies:
- Spring Web
- Eureka Server
- Spring Dev Tools
- Lombok
Below is the Folder Structure:

Step 2: Open the application.properties file and write the below dependencies.
spring.application.name=EurekaServerService
server.port=9099
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka
Step 3: In the main class, add @EnableEurekaServer annotation to enable Eureka server functionality.
Java
package org.example.eurekaserverservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerServiceApplication.class, args);
}
}
Step 4: After completing the project, run it as spring application and once it runs successfully, it will start at port 9099.

Create the User-service microservice
Step 1: Create a spring project using spring initializer. On creating the project, add the below dependencies.
Dependencies:
- Spring Web
- Eureka Server Client
- Spring Dev Tools
- Lombok
After creating the project, the folder structure will be like below:

Step 2: Open the application.properties file. Then put the below code for the server port and eureka client configuration.
spring.application.name=user-service
server.port=8086
eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone= http://localhost:9099/eureka
Step 3: Create the UserController class.
Go to src > org.example.userservice > UserController and write the code.
Java
package org.example.userservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/client")
public String check() {
return "Welcome to client";
}
}
Step 4: Now,Open the main class and write the @EnableDiscoveryClient into it.
Java
package org.example.userservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
Step 5: After completing the spring project, it run as spring application once it runs successful then it starts at port 8086.

Create the API Dynamic Routing
Step 1: Create a spring project using spring initializer on creating the project add the below dependencies into the project.
Dependencies:
- Spring Web
- Eureka Server Client
- Spring Dev Tools
- Lombok
- Spring Cloud Routing
After creating the Spring project, the file structure looks like the image below.
Step 2: Open the application.properties
file and rename it to application.yml
. Then, add the following YAML code for configuring the server port, Eureka client, and API routing:
server:
servlet:
context-path: /
port: 9056
spring:
application:
name: API-GATEWAY
profiles:
active:
- default
cloud:
gateway:
default-filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
routes:
- id: USER-SERVICE
uri: lb://user-service
predicates:
- Path= /**
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "*"
allowedHeaders: "*"
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9099/eureka
instance:
prefer-ip-address: true
Step 3: Open the main class and add the @EnableDiscoveryClient
annotation to enable service discovery functionality.
Java
package org.example.apigateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
Step 4: Run the application
Once the Spring project is completed and successfully runs as a Spring application, it will start at port 9056.

Eureka Dashboard:

Dynamic Routing API
Get the User-service microservice API access through the API Gateway routing mechanism port of the application.
GET http://localhost:9056/client
Output:

This example project demonstrates how to the implement the dynamic routing and services discovery in the API Gateway of the Spring Boot using Spring Cloud Routing and Eureka. By following the above steps, we can create the scalable and resilient microservices architecture with the efficient request routing.
Similar Reads
Amazon Web Service - Introduction to API Gateway Firstly, API stands for Application Program Interface. An API Gateway is a management tool that acts as an interface between users and microservices. The Amazon API Gateway is an AWS service that allows users to create, publish, secure, maintain and monitor APIs at any scale. You can create APIs in
12 min read
Dynamic Service Discovery and Load Balancing with Consul Consul was developed by HashiCorp and it is a powerful tool that serves as the service mesh solution providing a full-featured control plan with service discovery, configuration, and segmentation functionality. These features can allow the distributed services to communicate with each other in the d
5 min read
Service Discovery and Service Registry in Microservices Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable smal
5 min read
API Gateway vs Service Mesh: Top Differences Software development has been changed completely by the revolution of microservices, and this fosters agility, scalability, and maintainability. However, it can be a complex task to manage communication between them. The article below discusses API gateways and service meshes which are the two most
11 min read
Service Discovery in Distributed Systems In todayâs cloud-driven and microservices-oriented world, the complexity of distributed systems has grown exponentially. With numerous services working in concert across different servers and environments, keeping track of where each service resides and ensuring seamless communication between them i
7 min read
Client Side Service Discovery in Microservices Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable smal
6 min read