Eureka Server - Client Registration Example | Spring Cloud + Spring Boot | Java Inspires

Eureka Server and Client Registration Example Using Spring Cloud and Spring Boot

Introduction

Discover how to build robust microservices using Spring Cloud Eureka for service discovery and registration. This guide demonstrates step-by-step how to set up a Spring Boot Eureka Server and register REST API provider clients in the service registry for scalable, maintainable microservice architectures.

What Is Eureka Server?

Eureka Server is a powerful component from Spring Cloud Netflix that acts as a service registry. Microservices and REST APIs can register themselves as Eureka clients, enabling dynamic discovery, load balancing, and fault tolerance in cloud-native applications.

Prerequisites

  • Java 11 or above
  • Maven
  • Spring Boot (version 2.3.3 or compatible)
  • Basic understanding of Spring Boot and REST APIs.

Project Structure Overview

This tutorial includes two Spring Boot applications:
- Eureka Server (Service Registry)
- Eureka Client (REST API provider)

Step 1: Build Eureka Server (Service Registry)

Maven Dependencies
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Reference the proper Spring Cloud BOM in <dependencyManagement> to ensure version compatibility.

Configure application.properties
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

info.app.name=Eureka Server
info.app.description=This is Eureka Server - a Spring Boot application
info.app.version=1.0.0

Make sure to set register-with-eureka and fetch-registry to false so the server does not register itself as a client.

Main Application Class
@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoEurekaServerApplication.class, args);
  }
}

Annotate your main class with @EnableEurekaServer to activate registry features.

Pro Tip: Always start the Eureka Server before any clients so all clients can register successfully.

Step 2: Create REST API Provider and Register as Eureka Client

  • Generate a second Spring Boot application (the microservice or REST API).
  • Add the following dependency:
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

  • Configure application.properties of the client:
spring.application.name=api-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

This will register your app under the name api-service in the Eureka registry.

Step 3: Run and Verify the Setup

  1. Start the Eureka Server application and ensure it runs on port 8761.
  2. Start each client REST application. After startup, visit http://localhost:8761/ to see registered clients in the Eureka dashboard.
  3. All active clients will appear in the Eureka UI with their status and ports for quick verification.

Common Issues and Troubleshooting

  • If a client or Eureka server does not appear in the dashboard, check for errors in the logs and verify configuration values such as service-url.defaultZone and server port.
  • Ensure matching Java and dependency versions for compatibility with Spring Cloud Netflix Eureka.

Benefits of Using Eureka in Microservices

  • Dynamic Service Discovery: Removes the need for managing static service lists.
  • Load Balancing: Enables distribution of calls among multiple client instances.
  • Fault Tolerance: Automatic removal of offline instances from the registry.

Conclusion

Spring Cloud Eureka provides a seamless solution for registering and discovering microservices within distributed systems. By following this step-by-step example, developers can efficiently set up service registry infrastructure, making their Spring Boot microservices both scalable and maintainable.

Previous Post Next Post

Blog ads

ads