DEV Community

Cover image for How to Use Spring Boot Eureka Server in Spring Boot 3.3.0+
FullStackJava
FullStackJava

Posted on

How to Use Spring Boot Eureka Server in Spring Boot 3.3.0+

Image description

Spring Boot 3.3.0 and later versions bring many enhancements and changes. One crucial aspect of modern microservices architecture is service discovery, and Netflix Eureka is a popular choice for this purpose. In this detailed blog, we will guide you through setting up a Eureka Server and registering microservices with it using Spring Boot 3.3.0 or newer.

Table of Contents

  1. Introduction to Eureka Server
  2. Setting Up the Eureka Server
  3. Configuring Eureka Clients
  4. Running and Testing the Setup
  5. Conclusion

1. Introduction to Eureka Server

Netflix Eureka is a REST-based service registry for resilient mid-tier load balancing and failover. It provides a way for services to register themselves and to discover other registered services. This is particularly useful in microservices architecture to manage service instances dynamically.

2. Setting Up the Eureka Server

Step 1: Create a New Spring Boot Project

You can use Spring Initializr to create a new Spring Boot project. Ensure you include the Eureka Server dependency.

  • pom.xml:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <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-actuator</artifactId>
    </dependency>
    <!-- Other dependencies as required -->
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.0</version> <!-- Replace with the latest BOM version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Enter fullscreen mode Exit fullscreen mode

Step 2: Enable Eureka Server

Create a main application class and annotate it with @EnableEurekaServer.

  • EurekaServerApplication.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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Application Properties

Configure the application properties to set up the Eureka server.

  • application.yml:
server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    wait-time-in-ms-when-sync-empty: 0

spring:
  application:
    name: eureka-server
Enter fullscreen mode Exit fullscreen mode

Step 4: Run the Eureka Server

Run the application. Your Eureka Server should now be up and running at http://localhost:8761.

3. Configuring Eureka Clients

Next, let's set up a Eureka client (a microservice that registers itself with the Eureka server).

Step 1: Create a New Spring Boot Project for the Client

Again, use Spring Initializr to create a new Spring Boot project for the client. Include the Eureka Client dependency.

  • pom.xml:
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <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-actuator</artifactId>
    </dependency>
    <!-- Other dependencies as required -->
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.0</version> <!-- Replace with the latest BOM version -->
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Enter fullscreen mode Exit fullscreen mode

Step 2: Enable Eureka Client

Create the main application class and annotate it with @EnableEurekaClient.

  • EurekaClientApplication.java:
package com.example.eurekaclient;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Application Properties

Configure the application properties to register the client with the Eureka server.

  • application.yml:
server:
  port: 8080

spring:
  application:
    name: eureka-client

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Simple REST Controller

Create a simple REST controller to test the Eureka client.

  • GreetingController.java:
package com.example.eurekaclient;

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

@RestController
public class GreetingController {

    @GetMapping("/greeting")
    public String greeting() {
        return "Hello from Eureka Client!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Run the Eureka Client

Run the Eureka client application. It should register itself with the Eureka server.

4. Running and Testing the Setup

  1. Start the Eureka Server: Run the Eureka server application. Access the Eureka dashboard at http://localhost:8761. You should see an empty registry initially.

  2. Start the Eureka Client: Run the Eureka client application. After a few moments, the client should appear in the Eureka dashboard, indicating successful registration.

  3. Access the Client Service: You can access the client service at http://localhost:8080/greeting. This will return "Hello from Eureka Client!".

5. Conclusion

Setting up a Eureka server and client in Spring Boot 3.3.0+ is straightforward thanks to the robust support provided by Spring Cloud Netflix. By following this guide, you can establish a reliable service discovery mechanism for your microservices architecture, enhancing the scalability and resilience of your applications.

Summary

  • Setup Eureka Server: Create a Spring Boot project, include the Eureka Server dependency, configure it, and run.
  • Setup Eureka Client: Create a separate Spring Boot project, include the Eureka Client dependency, configure it, create a simple REST controller, and run.
  • Test the Setup: Verify the client registration in the Eureka dashboard and test the client service endpoint.

With Eureka in place, your microservices can dynamically discover and communicate with each other, simplifying load balancing and fault tolerance management. This setup forms a solid foundation for building and scaling a resilient microservices ecosystem.

Top comments (0)