VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/auto-scaling-microservices-with-eureka-and-spring-boot/

⇱ Auto-Scaling Microservices with Eureka and Spring Boot - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Auto-Scaling Microservices with Eureka and Spring Boot

Last Updated : 23 Jul, 2025

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 varying loads seamlessly.

Eureka is a service registry that allows microservices to self-register and discover new users Spring Boot can simplify the development of microservices by providing features such as embedded servers and dependency injection.

Auto-scaling configures the number of instances of microservices based on metrics such as CPU usage or incoming requests. This dynamic adjustment of resources allows the system to handle varying loads efficiently.

Key Terminologies:

  • Eureka: A service registry that enables microservices to self-register and discover each other. It plays a vital role in service discovery and load balancing.
  • Spring Boot: Provides an easy way to develop microservices with embedded servers and dependency injection, streamlining the development process.
  • Auto-Scaling: Automatically adjusts the number of instances of a microservice based on metrics like CPU usage or incoming requests. This ensures the system can handle fluctuations in load efficiently.

Step-by-step implementation of the Auto-Scaling Microservices with Eureka and Spring Boot

Below are the steps to implement Auto-Scaling Microservices with Eureka and Spring Boot.

Set up the Eureka-server

Step 1: Create a Spring project using Spring Initializr, and include the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server
  • Spring Dev Tools
  • Lombok

After creating the Spring project, the file structure will resemble the image below.

👁 Project Structure


Step 2: Open the application.properties file and add the following code to configure the server port and Eureka server settings for the project.

spring.application.name=eureka-server-config

server.port=9099
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: In the main class, include the annotation @EnableEurekaServer to activate the Eureka server functionality.


pom.xml:


Step 4: Once the Spring project is completed and run as a Spring application successfully, it will start at port 9099.

👁 Eureka Server Started


Create the User-service Microservice

Step 1: Create the Spring project using Spring Initializr. When creating the project, add the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server Client
  • Spring Dev Tools
  • Lombok

After creating the Spring project, the file structure should resemble the image below.

👁 File Structure


Step 2: Open the application.properties file and insert the following code to configure the server port and Eureka client settings for the project.

spring.application.name=user-service
server.port=8086


management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
management.prometheus.metrics.export.enabled=true


autoscaler.cpu.threshold=0.75

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

spring.data.mongodb.uri=mongodb://localhost:27017/demo


Step 3: Create a new package named "model". Within this package, create a new Java class named "User".

Go to src > org.example.userservice > model > User and put the below code.


Step 4: Create the new package and it named as the repository in that package create the new Java class and it named as UserRepository .

Go to src > org.example.userservice > repository > UserRepository and put the below code.


Step 5: Create the new package and it named as the service in that package create the new Java class and it named as UserService.

Go to src > org.example.userservice > service > UserService and put the below code.


Step 6: Create the new package and it named as the service in that package create the new Java class and it named as UserServiceImpl.

Go to src > org.example.userservice > service > UserServiceImpl and put the below code.


Step 7: Create a new Java class named AutoScaler.

Go to src > org.example.userservice > AutoScaler and put the below code.

This AutoScaler class is designed to automatically scale the number of instances of the microservice based on CPU usage.

How Auto-Scaling Works:

  • Monitoring: The autoScale() method checks CPU usage every minute.
  • Decision Making: If CPU usage exceeds the threshold, it triggers a scale-up; if below, it triggers a scale-down.
  • Scaling Actions:
    • Up-Scaling: New instances are created by interacting with a container orchestration platform (e.g., Kubernetes). The new instances register themselves with Eureka.
    • Down-Scaling: Excess instances are terminated, and they de-register from Eureka to stop receiving new requests. The system ensures in-progress requests are completed.

Enhanced Implementation:

To refine auto-scaling:

  • Metrics Collection: Use tools like Spring Boot Actuator and Micrometer for accurate CPU metrics.
  • Scaling Algorithms: Implement more sophisticated algorithms that consider historical data and trends.
  • Integration with Orchestration Platforms: Use an orchestration platform (e.g., Kubernetes) to manage instances. Integrate with its API to handle instance creation and termination dynamically.

To actually create and manage multiple instances, the AutoScaler class could be expanded like this:


Additional Considerations:

  • Metrics Collection: Use Spring Boot Actuator and Micrometer for real CPU metrics.
  • Scaling Algorithms: Consider trends over time rather than instant values.
  • Logging and Cool-Down: Add detailed logging and implement cool-down periods to prevent rapid fluctuations.

Verify Auto-Scaling

To verify auto-scaling:

  • Monitoring: Check the number of running instances and metrics on your monitoring dashboard.
  • Testing: Simulate high load conditions to observe the scaling behavior and ensure the system adjusts instances as expected.

Step 8: Create a new package named "controller". Within this package, create a new Java class named "UserController".

Go to src > org.example.userservice > Controller > UserController and put the below code.


Step 9: Run the application

Once completed the project after that run the application once runs the application successfully looks like the below image.

👁 User Service Started


Eureka Dashboard:

👁 Eureka Dashboard:


APIS Outputs:

Create the user:

POST http://localhost//8086/api/users

Output:

👁 User Created


Get the users:

GET http://localhost//8086/api/users

Output:

👁 Get Users

This implementation covers the basics of setting up auto-scaling for microservices using Eureka and Spring Boot. For a production-ready solution, integrate with container orchestration platforms to handle instance creation and termination dynamically based on load.

Comment

Explore