0
0
SpringbootHow-ToBeginner · 4 min read

How to Use Eureka for Service Discovery in Spring Boot

To use Eureka for service discovery in Spring Boot, add the spring-cloud-starter-netflix-eureka-server dependency to create a Eureka server and annotate your main class with @EnableEurekaServer. Then, add spring-cloud-starter-netflix-eureka-client to your client services and use @EnableEurekaClient to register them with the server automatically.
📐

Syntax

Using Eureka in Spring Boot involves two main parts: the Eureka Server and Eureka Clients.

  • Eureka Server: A Spring Boot app annotated with @EnableEurekaServer to act as the service registry.
  • Eureka Client: Spring Boot apps annotated with @EnableEurekaClient that register themselves to the Eureka Server.
  • Dependencies: Use spring-cloud-starter-netflix-eureka-server for the server and spring-cloud-starter-netflix-eureka-client for clients.
  • Configuration: Set Eureka server URL and client properties in application.yml or application.properties.
java
/* Eureka Server Main Application */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

/* Eureka Client Main 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 service registering with it. The server runs on port 8761 and the client on port 8081.

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


/* Eureka 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 Eureka Client */
server:
  port: 8081

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: true
    fetch-registry: true

spring:
  application:
    name: service-client
Output
Eureka Server UI available at http://localhost:8761 showing registered client 'service-client' running on port 8081.
⚠️

Common Pitfalls

  • Not disabling self-registration on the Eureka Server: The server should not register itself as a client; set register-with-eureka: false and fetch-registry: false in server config.
  • Incorrect service URL: Clients must point to the correct Eureka server URL in defaultZone.
  • Missing annotations: Forgetting @EnableEurekaServer on the server or @EnableEurekaClient on clients prevents proper registration.
  • Port conflicts: Ensure server and clients run on different ports.
yaml
/* Wrong: Eureka Server registers itself as client (bad) */
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true

/* Right: Disable self-registration on server */
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
📊

Quick Reference

Remember these key points when using Eureka with Spring Boot:

  • Use @EnableEurekaServer for the registry server.
  • Use @EnableEurekaClient for services that register.
  • Configure application.yml properly for ports and URLs.
  • Check Eureka dashboard at http://localhost:8761 to see registered services.

Key Takeaways

Add Eureka Server dependency and annotate main class with @EnableEurekaServer to create the registry.
Add Eureka Client dependency and annotate client apps with @EnableEurekaClient to register them automatically.
Configure server and client URLs correctly in application properties or YAML files.
Disable self-registration on the Eureka Server to avoid it registering as a client.
Use the Eureka dashboard at port 8761 to monitor registered services.