0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Service Registry in Spring Boot for Microservices

To use a service registry in Spring Boot, add a registry server like Spring Cloud Eureka Server to register your services. Then, configure your Spring Boot applications as Eureka clients to register themselves and discover other services automatically.
📐

Syntax

Using a service registry in Spring Boot involves two main parts: the registry server and the client applications.

  • Registry Server: A Spring Boot app annotated with @EnableEurekaServer to act as the service registry.
  • Client Application: Spring Boot apps annotated with @EnableEurekaClient or using spring-cloud-starter-netflix-eureka-client dependency to register themselves and discover others.
  • Configuration: Both server and clients need properties in application.yml or application.properties to specify registry URLs and service names.
java
/* Registry Server Application */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

/* Client Application */
@SpringBootApplication
@EnableEurekaClient
public class ServiceClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceClientApplication.class, args);
    }
}
💻

Example

This example shows a simple Eureka server and a client registering itself. The server runs on port 8761 and the client on port 8080.

java/yaml
/* Eureka Server Application.java */
package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

/* application.yml for Eureka Server */
server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: false

/* Service Client Application.java */
package com.example.serviceclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

/* application.yml for Service Client */
server:
  port: 8080

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
Output
When running, the Eureka server UI is accessible at http://localhost:8761 showing the registered client service.
⚠️

Common Pitfalls

  • Forgetting to add @EnableEurekaServer on the registry server app causes it not to start as a registry.
  • Clients must point to the correct registry URL in application.yml or they won't register.
  • Setting register-with-eureka or fetch-registry incorrectly on the server can break service discovery.
  • Not including the Eureka client dependency in client apps will prevent registration.
  • Running multiple services on the same port causes conflicts.
java
/* Wrong: Missing @EnableEurekaServer on server app */
@SpringBootApplication
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

/* Right: Add @EnableEurekaServer */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
📊

Quick Reference

Key properties for Eureka client:

  • eureka.client.service-url.defaultZone: URL of the Eureka server
  • spring.application.name: Unique service name
  • server.port: Port number for the service

Annotations:

  • @EnableEurekaServer: Marks the registry server
  • @EnableEurekaClient: Marks a client service

Key Takeaways

Add @EnableEurekaServer to create a service registry server in Spring Boot.
Configure clients with @EnableEurekaClient and point them to the registry URL.
Set correct properties in application.yml for both server and clients to enable registration and discovery.
Ensure unique service names and ports to avoid conflicts in the registry.
Include the necessary Spring Cloud Netflix Eureka dependencies in your projects.