Spring Boot Java framework: how to set up service registry in micro-service architecture

Data Backend Tech
2 min readMay 20, 2023

--

Introduction

In a micro-service system, managing and discovering services can become challenging. Spring Boot provides a convenient way to address this challenge by integrating with a service registry. In this article, we will explore how to set up a service registry using Spring Boot, allowing services to register and discover each other dynamically.

Step-by-step instructions

Step 1:

Add dependencies to pom.xml To get started, open your project’s pom.xml file and add the following dependencies:

<dependencies>
<!-- Spring Boot Starter for Eureka Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

Step 2:

Enable the Eureka Server In your main application class, add the @EnableEurekaServer annotation to enable the Eureka server functionality:

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

Step 3:

Configure the Eureka Server Create a configuration file named application.properties and add the following configuration:

# Application port
server.port=8761

# Disable self-registration
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

This configuration disables the self-registration and fetching of the registry for the Eureka server itself.

Step 4:

Start the Eureka Server Run the Spring Boot application. The Eureka server will start on port 8761, and you can access the Eureka dashboard by visiting http://localhost:8761 in your web browser.

Step 5:

Register a service To register a service with the Eureka server, you need to add the @EnableEurekaClient annotation to the service's main application class:

@SpringBootApplication
@EnableEurekaClient
public class MyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceApplication.class, args);
}
}

Step 6:

Configure service registration In the service’s application.properties file, add the following configuration:

# Service name
spring.application.name=my-service

# Eureka server URL
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

This configuration sets the service name and specifies the URL of the Eureka server.

Step 7:

Test the service registration Run the service application, and it will automatically register itself with the Eureka server. You can verify the registration by visiting the Eureka dashboard at http://localhost:8761.

Summary

Setting up a service registry using Spring Boot and Eureka server allows services to register and discover each other dynamically in a micro-service system. In this article, we shared the steps to add the necessary dependencies, enable the Eureka server, configure the server and the registered services, and test the service registration using the Eureka dashboard.

--

--