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.



Dynamic Routing and Service Discovery in API Gateway

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.

Similar Reads

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....