Spring Boot Eureka Client

Spring Boot Eureka Client with Examples

In this article, I am going to discuss Spring Boot Eureka Client with Examples. Please read our previous article where we discussed Spring Boot Eureka Server with Examples.

Spring Boot Eureka Client

In the previous chapter, we created a Eureka server. Now, we shall create a Eureka client that will connect to this server. Let us create a new application in Spring Boot.

Step 1: Create a new project using Spring Initializr in VS Code. Remember to include the Spring Web, Eureka Discovery Client, and Eureka Server dependency.

Spring Boot Eureka Client with Examples

Ensure that the dependencies are correct:

Spring Boot Eureka Client with Examples

Step 2: Import required packages:

Spring Boot Eureka Client with Examples

Step 3: Create another class called ServiceInstanceRestController in EurekaclientApplication.java class in the src/main/java/com/dotnet/eurekaclient directory. This class should have @RestController annotation.

Spring Boot Eureka Client with Examples

Step 4: Compile and execute the Eureka server application. Ensure the compilation is successful.

Spring Boot Eureka Client

Step 5: Log on to http://localhost:8761 to check if the Eureka server is functioning. You should see the following page:

Spring Boot Eureka Client

Step 6: Compile and execute the Eureka client application. Ensure the compilation is successful.

Spring Boot Eureka Client

Step 7: Refresh the Equreka server webpage. The client application must be visible:

Spring Boot Eureka Client

Congratulations! You now know how to connect a Spring Boot application to a Eureka Discovery Server.

The Complete Example Code
EurekaClientApplication.java
package com.dotnet.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@SpringBootApplication
public class EurekaclientApplication {

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

}

@RestController
class ServiceInstanceRestController
{
    @Autowired
    private DiscoveryClient discoveryClient;

    public List<ServiceInstance> ServiceInstanceByApplicationName (@PathVariable String applicationName)
    {
        return this.discoveryClient.getInstances(applicationName);
    }
}

In the next article, I am going to discuss Introduction to Spring Boot AOP. In this article, I try to explain Spring Boot Eureka Client with Examples. I hope you enjoy this Spring Boot Eureka Client article.

Leave a Reply

Your email address will not be published. Required fields are marked *