Setting Up Netflix Eureka in Spring Boot Microservices

Setting Up Netflix Eureka in Spring Boot Microservices

Setting Up Netflix Eureka in Spring Boot Microservices

In a microservices architecture, managing the dynamic nature of service instances can be challenging. Netflix Eureka, a popular service registry and discovery server, comes to the rescue. In this post, we will demonstrate how to implement client-side service discovery using Spring Cloud Netflix Eureka in a microservices architecture with Spring Boot.

1. Introduction

Consider two services: a Product Service running on port 8081 and an Order Service on 8021. If multiple instances of these services are spawned, each instance would likely run on a different port. Directly referencing services via hardcoded addresses is neither scalable nor maintainable. This is where Eureka comes into play.

Eureka allows services to find and communicate with each other without hard-coding hostnames and ports. The key components include:

  1. Eureka Server: It is a server that stores the addresses (host and ports) of all the registered microservices.
  2. Eureka Client: It’s a microservice registered on the central server and it updates and retrieves addresses to/from the central Eureka server.

2. Setting up the Eureka Server

Netflix Eureka requires a central server that acts as the service registry. To set up the Eureka server:

2.1. Create a new Spring Boot project

Creating a new Spring Boot project becomes straightforward using the Spring Initializr. Visit the Spring Initializr website, and create a spring boot project boilerplate.

2.1 Adding Necessary Dependencies

In the Eureka server’s pom.xml file, make sure we have the following dependencies:

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

2.2 Enable Eureka Server

In the main application class, enable Eureka Server by adding the annotation @EnableEurekaServer.

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

2.3 Configure Eureka Server

In application.properties, specify the server port and disable the registration as this will act as a server:

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

For a standalone Eureka server, the Eureka server does not need to register itself with its own registry, so it’s set to false. In more advanced setups, where we might have multiple Eureka servers in a peer-aware configuration, this could be set to true.

By default, the Eureka server will log warnings if it can’t find peers. Since this setup involves a single Eureka instance, suppress this logging with:

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

Start the Spring Boot application, and Eureka Server should be up and running at http://localhost:8761

image-4 Setting Up Netflix Eureka in Spring Boot Microservices

3. Implement Eureka Clients

Now, let’s modify the product and order microservices so that they will register themselves with the Eureka server.

3.1 Adding Necessary Dependencies

Add the Eureka client dependency to both product and order microservices’ pom.xml.

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

3.2 Configure Eureka URL

In the product service’s application.properties, specify the Eureka server location and the service name:

spring.application.name=product-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

In the order service’s application.properties, specify the Eureka server location and the service name:

spring.application.name=order-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

Run each microservice application, access the Eureka server dashboard at http://localhost:8761 in the web browser. We should see order and product registered microservices.

image-5 Setting Up Netflix Eureka in Spring Boot Microservices

4. Update Service Discovery

With the Eureka server and microservices up and running, we can test service discovery. In order service, update the product-service.name:

product-service.name=product-service/api/products

In the ProductClient, make sure we specify the name using the correct configuration property.

@FeignClient(name = "${product-service.name}")
public interface ProductClient {
    @GetMapping("/{id}")
    ResponseEntity<Map<String, Object>> getProductById(@PathVariable("id") Long id);
}

5. Testing with Postman

We can try to invoke the API via Postman. First, we insert one product record.

image-6 Setting Up Netflix Eureka in Spring Boot Microservices

Follow by inserting one order record with the productId set to 1.

image-7 Setting Up Netflix Eureka in Spring Boot Microservices

Now, send a GET request to the http://localhost:8082/api/orders/1 endpoint to retrieve the order record.

image-8 Setting Up Netflix Eureka in Spring Boot Microservices

We should see the product details returned in the productDto object. This proves that Eureka has routed the request to product service correctly.

6. Conclusion

Netflix Eureka simplifies service discovery in a microservices architecture, allowing services to locate and communicate with one another dynamically. In this post, we’ve demonstrated how to set up an Eureka server and create microservices that register with and discover other services using Eureka. This essential component can help you build scalable and resilient microservices-based applications. In the next post, we will discover how to implement a client-side load balancer to ensure efficient distribution of incoming requests among service instances using Ribbon.

Share this content:

Leave a Comment

Discover more from nnyw@tech

Subscribe now to keep reading and get access to the full archive.

Continue reading