microservices
  1. microservices-connecting-microservice-to-eureka

Connecting Microservice to Eureka

In Microservices architecture, Service Discovery is an important aspect. Eureka is a Netflix’s Service Registry and a Service Discovery component. In this tutorial, we will learn how to connect Microservice to Eureka for Service Registration and Discovery.

Syntax

The syntax to connect Microservice to Eureka is as follows:

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

spring.application.name=Your_Service_Name
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

Example

In this example, we will create a simple Microservice using Spring Boot and connect it with the Eureka server for Service Registration and Discovery.

Step 1: Create Microservice

First, let's create a Microservice. We will create a simple RESTful service that returns a list of products.

@RestController
@RequestMapping("/products")
public class ProductController {

    @GetMapping
    public List<Product> getProducts() {
        List<Product> products = new ArrayList<>();
        products.add(new Product("1", "Product 1"));
        products.add(new Product("2", "Product 2"));
        products.add(new Product("3", "Product 3"));
        return products;
    }
}

Step 2: Add Eureka Dependency

Next, we need to add the Eureka client dependency in our pom.xml file.

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

Step 3: Configure Eureka Server URL

We need to configure the Eureka server URL in our application.yml file.

spring.application.name=product-service
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true

Step 4: Enable Eureka Client

We need to enable the Eureka client in our Spring Boot application.

@SpringBootApplication
@EnableDiscoveryClient
public class ProductServiceApplication {

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

Step 5: Run Eureka Server

We need to run the Eureka server on port 8761.

java -jar eureka-server-0.0.1-SNAPSHOT.jar

Step 6: Run Microservice

We can run the Microservice using the following command.

java -jar product-service-0.0.1-SNAPSHOT.jar

Step 7: Test Microservice

We can test our Microservice using the following URL.

http://localhost:8080/products

Step 8: Test Eureka Server

We can test the Eureka server using the following URL.

http://localhost:8761/

Explanation

In this example, we have created a simple Microservice using Spring Boot and connected it with the Eureka server for Service Registration and Discovery.

We have added the Eureka client dependency in our pom.xml file and configured the Eureka server URL in our application.yml file. We have enabled the Eureka client in our Spring Boot application. We have run the Eureka server and Microservice and tested them using the URLs provided.

Use

Connecting Microservice to Eureka is useful for Service Registration and Discovery in a Microservices architecture.

Important Points

  • Eureka is a Service Registry and a Service Discovery component.
  • We need to add the Eureka client dependency and configure the Eureka server URL in our Spring Boot application.
  • We need to enable the Eureka client in our Spring Boot application.
  • We can test the Eureka server and Microservice using the respective URLs provided.

Summary

In this tutorial, we have learned how to connect Microservice to Eureka for Service Registration and Discovery. We created a simple Microservice using Spring Boot and connected it with the Eureka server. We added the Eureka client dependency, configured the Eureka server URL, and enabled the Eureka client in our Spring Boot application. We ran the Eureka server and Microservice and tested them using the URLs provided.

Published on: