Java Spring Boot Microservices – Integration of Eureka and Spring Cloud Gateway
Last Updated :
04 Jan, 2025
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 small components with some specified responsibilities. It is considered the building block of modern applications.
What is Eureka?
Service Discovery is one of the major things of a microservice-based architecture. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly functional, with each server copying the state of the registered services to the others.
What is Spring Cloud Gateway?
Spring Cloud Gateway provides a library for making API gateways on top of Spring and Java. It provides a flexible way of routing requests based on a number of criteria, as well as focuses on cross-cutting problems like security, resiliency, and monitoring. Some of the important features of Spring Cloud Gateway are:
- It is Built on Spring Framework 5, Project Reactor, and Spring Boot 2.0
- You can integrate Circuit Breaker with Spring Cloud Gateway
- You can integrate Spring Cloud DiscoveryClient
- Predicates and filters are specific to routes
- Path Rewriting
- It is able to match routes on any request attribute, etc.
Note: Please refer to this article to know more about Spring Cloud Gateway.
So in this article, we will integrate all the 2 popular tools with our Microservices. We are going to develop 1 microservice, 1 API Spring Cloud Gateway, and 1 service discovery using Eureka.
Developing Service Discovery using Eureka
To develop a Service Discovery using Eureka, please refer to this article Java Spring Boot Microservices – Developing Service Discovery.
Developing API Gateway Using Spring Cloud Gateway
Step 1: Create a New Spring Boot Project in Spring Initializr
To create a new Spring Boot project, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things
- Project: Maven
- Language: Java
- Packaging: Jar
- Java: 17
Please choose the following dependencies while creating the project.
- Gateway (SPRING CLOUD ROUTING)
- Eureka Discovery Client
Below is the complete pom.xml file. Please cross-verify if you have missed some dependencies
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ey</groupId>
<artifactId>spring-cloud-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Cloud Gateway</name>
<description>Spring Cloud Gateway</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>netflix-candidates</id>
<name>Netflix Candidates</name>
<url>https://artifactory-oss.prod.netflix.net/artifactory/maven-oss-candidates</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Step 2: Make Changes in Your application.yaml file
Now make the following changes in your application.yaml file.
server:
port: 8085
spring:
application:
name: API-GATEWAY-SERVICE
cloud:
gateway:
routes:
- id: DEMO-SERVICE
uri: http://localhost:9090
predicates:
- Path=/demo/**
Here,
- id: You can give any id as of now.
- uri: Here you have to provide the port in which your microservice is running
- predicates (- Path): Here we have provided the path "/demo/**", which means any request starting with path "/demo/**" route it to DEMO-SERVICE.
Develop the DEMO-SERVICE
To create a new service, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things
- Project: Maven
- Language: Java
- Packaging: Jar
- Java: 17
Please choose the following dependencies while creating the project.
- Spring Web
- Eureka Discovery Client
Below is the complete pom.xml file. Please cross-verify if you have missed some dependencies
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gfg</groupId>
<artifactId>demo-ms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-ms</name>
<description>Demo project for Spring Cloud Gateway</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>netflix-candidates</id>
<name>Netflix Candidates</name>
<url>https://artifactory-oss.prod.netflix.net/artifactory/maven-oss-candidates</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Step 2: Make Changes in Your application.properties File
Now make the following changes in your application.properties file.\
server.port=9090
spring.application.name=DEMO-SERVICE
Step 3: Create a DemoController
Go to the src > main > java > controller and create a class DemoController and put the below code. In this, we have created a simple REST API in our controller class.
Java
package com.gfg.demo.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/gfg")
public ResponseEntity<String> getAnonymous() {
return ResponseEntity.ok("Welcome to GeeksforGeeks");
}
}
Now run your application and test it out.
Testing in Postman
Now run all the 3 applications. Let's test our API. Hit the following URL
http://localhost:9090/demo/gfg
And you are going to get a response like this

Now we can get the same response by using our API gateway port which is 8085. Now hit the following URL
http://localhost:8085/demo/gfg
And you are going to get a response like this

So this is how the API gateway works. If you have hundreds of microservices then you don't need to remember the port of all microservices. You can just configure them in your API Gateway and you can access all your API by using only one port. And this is our eureka dashboard.

Similar Reads
Java Spring Boot Microservices - Integration of Eureka, Feign & Spring Cloud Load Balancer
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
13 min read
Routing and Request Transformation in API Gateways in Spring Cloud Microservices
API gateways play a crucial role in modern microservices architectures by serving as the centralized entry point for client requests. They can handle the routing requests to the appropriate microservices and it can often involve the request transformation to adapt the client requests to the specific
10 min read
API Composition and Aggregation with Spring Cloud Gateway in Java Microservices
API Composition and Aggregation is the critical pattern in the microservices architecture. It can enable combining the data from multiple microservices into a single response which is essential for reducing the number of client-side requests and improving the overall efficiency of the data retrieval
8 min read
Auto-Scaling Microservices with Eureka and Spring Boot
Auto-scaling Microservices with Eureka and Spring Boot involves leveraging Eureka for the service discovery and Spring Boot for building microservices that can dynamically scale based on the demand. This setup allows for efficient resource utilization and it can ensure that the system can handle var
7 min read
Getting Started with Spring Boot and Eureka Service Registry
Eureka is the other hand in the Service Registry supplied by Netflix. It can be designed for the resilient mid-tier for the load balancing and failover of the middle-tier servers. When used together then the spring boot and Eureka can enable the developers to build the microservices architectures. T
5 min read
Java Spring Boot Microservices - Developing Service Discovery
In Microservices, Service Discovery helps us by providing a database of available service instances so that services can be discovered, registered, and de-registered based on usage. For a detailed explanation of Service Discovery please refer to this article Service Discovery and Service Registry in
3 min read
API Gateway Security Best Practices in Java Microservices
An API Gateway acts as a front-end for receiving API requests, enforcing throttling and security policies, passing requests to the back-end service, and then passing the response back to the requester. It sits between external clients and microservices, providing a unified entry point for multiple s
4 min read
Secure Service Registration with Eureka and Spring Boot Microservices
In microservice architecture, service registration and discovery can plays the crucial role. Eureka is a part of the Spring Cloud Netflix project and it provides the mechanism to register and discover the services. It ensure the secure communication between the services. It is important to secure th
4 min read
Efficient Load Balancing and Metrics Monitoring in Spring Cloud Microservices
In Spring Microservices, services are often decentralized and distributed across multiple nodes. This architecture can enhance resilience and scalability but also introduces challenges such as efficient request distributions and performance monitoring. Load balancing and metrics monitoring are essen
6 min read
Implementing Round Robin Load Balancing in Spring Boot Microservices
Round Robin Load Balancing is a method of distributing client requests across multiple servers in a sequential manner. It ensures that each server handles an equal number of requests over time. This approach can be particularly useful for evenly distributing the load and avoiding overburdening a sin
7 min read