Eureka in Spring Boot


1. Setup Eureka Server

Step 1: Create a Spring Boot Project

  • Dependencies:
    • Spring Web
    • Eureka Server

Step 2: pom.xml Example

<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-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Step 3: Main Class

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);
    }
}

Step 4: application.yml

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: false
  • Start the server and go to http://localhost:8761. You should see the Eureka dashboard.

2. Setup Eureka Client (Microservice)

Step 1: Create a Spring Boot Project

  • Dependencies:
    • Spring Web
    • Eureka Discovery Client

Step 2: pom.xml Example

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2021.0.6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Step 3: Main Class

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

Step 4: application.yml

server:
  port: 8081

spring:
  application:
    name: demo-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

Step 5: Simple REST Controller

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Demo Service!";
    }
}
  • Start the service and check the Eureka dashboard. You should see demo-service registered.

3. Optional: Multiple Clients

You can create more microservices (e.g., user-service, order-service) using the same Eureka client configuration. All services will register to the Eureka server.


✅ Key Points:

  • Eureka Server: Handles service registry.
  • Eureka Client: Registers itself to the server.
  • Spring Boot + Netflix Eureka makes microservices discoverable easily.
  • @EnableDiscoveryClient tells Spring Boot to register with Eureka.
  • Multiple clients can communicate using service names instead of hardcoding URLs.

Leave a Reply