CodeGym /Courses /Module 5. Spring /Lecture 247: Hands-on: configuring Eureka for microservic...

Lecture 247: Hands-on: configuring Eureka for microservice discovery

Module 5. Spring
Level 24 , Lesson 6
Available

Eureka is basically the "phone book" for your microservices. Instead of hard-coding IPs and ports for services, you can let them register in a single registry where they'll be available for other clients or an API Gateway when needed. This makes supporting and scaling a microservice system much simpler. Imagine you have 10 microservices and their addresses change on every deploy. With Eureka you don't have to memorize those addresses — everything updates automatically.


What we'll do today?

  1. Install and configure Eureka Server, which will act as the service registry.
  2. Connect a microservice as a Eureka Client so it registers with the server.
  3. Test it all in practice using the Eureka Dashboard console.
  4. Fix common registration and interaction issues.

Step 1: Setting up the Eureka Server

To start, you need to create a new Spring Boot project that will serve as the Eureka Server.

1.1 Creating the project

  • Use Spring Initializr:
    • Specify dependencies:
      • Spring Boot Starter Web
      • Spring Boot Starter Actuator
      • Eureka Server

1.2 pom.xml configuration

If you're using Maven, make sure the dependencies are added correctly:


<dependencies>
    <!-- Eureka Server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!-- Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Actuator for Monitoring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

Don't forget to specify the Spring Cloud version in application.properties or inside the <dependencyManagement> section:


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

1.3 Configure application.yml

Create the file src/main/resources/application.yml:

server:
  port: 8761 # Standard port for Eureka Server
spring:
  application:
    name: eureka-server
eureka:
  client:
    register-with-eureka: false # The server itself won't register with other servers
    fetch-registry: false      # We don't try to fetch the registry
  server:
    enable-self-preservation: false # Disable self-preservation mode for simplicity

1.4 Enabling the Eureka Server

Add the @EnableEurekaServer annotation to the main application class:


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

Run the app. Your Eureka Server should now be available at: http://localhost:8761.


Step 2: Setting up the Eureka Client

Now let's create a microservice that will register itself with the Eureka Server.

2.1 Create a new project

Use Spring Initializr:

  • Dependencies:
    • Spring Boot Starter Web
    • Spring Boot Starter Actuator
    • Eureka Client

2.2 pom.xml configuration

Add the required dependencies:

<dependencies>
    <!-- Eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Actuator for Monitoring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

2.3 Configure application.yml

Create the file src/main/resources/application.yml:


server:
  port: 8081 # Specify the microservice port
spring:
  application:
    name: service-client # Microservice name
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # Eureka server URL

2.4 Enabling the Eureka Client

In the main class add the @EnableEurekaClient annotation:


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

Step 3: Verify interaction

  1. Start both apps: the Eureka Server and the Service Client.
  2. Open the Eureka Dashboard: http://localhost:8761.
  3. You should see a microservice named service-client registered in the registry. It will appear under Instances currently registered with Eureka.

Step 4: Common errors and how to fix them

If the client doesn't show up in the Eureka Dashboard, check the following:

  1. Incorrect Eureka server URL:
    • Make sure the URL in application.yml is correct and the server is running.
  2. Incompatible Spring Cloud version:
    • Ensure Spring Cloud and Spring Boot versions are compatible. For example, Spring Boot 3.x pairs with Spring Cloud 2022.x.
  3. CORS issues:
    • If your microservice makes cross-domain requests, configure the CORS policy accordingly.

Now you've got a working Eureka Server and Eureka Client setup! In real projects this lets you automatically register and discover microservices, saving you from manually managing their addresses and ports. It's a big step toward a professional microservice architecture.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION