Eureka Service Registry: Introduction, Core Concepts, and Hands‑On Setup

This article explains what Eureka is, describes its core concepts such as registration, renewal, fetching registries, cancellation and eviction, and provides step‑by‑step instructions with code to set up a Eureka server, a service provider and a consumer using Spring Cloud, illustrating load‑balancing and cache‑refresh behavior.

Shepherd Advanced Notes
Shepherd Advanced Notes
Shepherd Advanced Notes
Eureka Service Registry: Introduction, Core Concepts, and Hands‑On Setup

Eureka Overview

Eureka is an open‑source service registration and discovery framework from Netflix, consisting of a Eureka Server and Eureka Client .

Eureka architecture diagram
Eureka architecture diagram

Eureka Server : exposes services via REST and provides registration and discovery functions.

Application Server : a service provider that embeds a Eureka Client to register itself with the server.

Application Client : a service consumer that embeds a Eureka Client to obtain the service list from the server.

Core Concepts

Register : a client sends its metadata (IP, port, health‑check URL, home page, etc.) to the server when it registers.

Renew : the client sends a heartbeat every 30 seconds (default). If the server does not receive a heartbeat for 90 seconds, the instance is removed.

Fetch Registries : the client pulls the registry from the server every 30 seconds, caches it locally, and updates the cache when the data differ. Communication uses JSON or XML; the server compresses the registry without loss of information.

Cancel : on shutdown the client calls DiscoveryManager.getInstance().shutdownComponent() to remove its entry.

Eviction : after three missed renew cycles (90 seconds) the server evicts the instance.

Setup Steps

1. Eureka Server

Add the Spring Cloud Netflix Eureka Server dependency:

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

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

Configure application.yml:

server:
  port: 8761  # Eureka Server port

spring:
  application:
    name: eureka-server

eureka:
  client:
    register-with-eureka: false  # Do not register to itself
    fetch-registry: false       # Do not fetch registry

Add the @EnableEurekaServer annotation to the main class and run the application. The UI is available at http://127.0.0.1:8761.

Eureka server UI
Eureka server UI

Repository: https://github.com/ShepherdZFJ/shepherd-example/tree/main/eureka-server

2. Service Provider

Add the Spring Cloud Netflix Eureka Client dependency:

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

<dependencies>
    <!-- Spring Cloud Netflix Eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

Test controller implementation:

package com.shepherd.eureka;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @RestController
    @Slf4j
    static class TestController {
        @Value("${server.port}")
        private String serverPort;

        @GetMapping("/provider")
        public String test(String name) {
            log.info("Called, {}", serverPort);
            return "provider:" + name;
        }
    }
}

Repository: https://github.com/ShepherdZFJ/shepherd-example/tree/main/eureka-provider

3. Service Consumer

Define a RestTemplate bean:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Consumer controller using DiscoveryClient and LoadBalancerClient:

package com.shepherd.eureka;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

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

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    @Slf4j
    static class TestController {
        @Resource
        private DiscoveryClient discoveryClient;
        @Resource
        private RestTemplate restTemplate;
        @Resource
        private LoadBalancerClient loadBalancerClient;

        @GetMapping("/consumer")
        public String hello(String name) {
            // Obtain an instance of the provider
            ServiceInstance instance = loadBalancerClient.choose("eureka-provider");
            if (instance == null) {
                throw new IllegalStateException("No instance found");
            }
            String targetUrl = instance.getUri() + "/provider?name=" + name;
            log.info("Chosen instance: {}", targetUrl);
            String response = restTemplate.getForObject(targetUrl, String.class);
            return "consumer:" + response;
        }
    }
}

Repository: https://github.com/ShepherdZFJ/shepherd-example/tree/main/eureka-consumer

Running the Example

Start the Eureka Server, then the provider and consumer applications. The Eureka UI shows both services registered. Launch multiple provider instances on different ports to observe the default round‑robin strategy of LoadBalancerClient.

Eureka UI with registered services
Eureka UI with registered services

When a provider is stopped, an immediate call from the consumer fails because the client still holds the stale instance in its local cache. The client refreshes the cache every 30 seconds; after this interval the stopped provider is no longer returned by discovery calls.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

microservicesservice discoverySpring Booteurekaspring cloudresttemplateloadbalancerclient
Shepherd Advanced Notes
Written by

Shepherd Advanced Notes

Dedicated to sharing advanced Java technical insights, daily work snippets, and the power of persistent effort.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.