Dev Zone
February 22, 2022

Tutorial: How to implement Service Discovery with Eureka (Java)

What is Service Discovery?

When developing a system with many services, we need to specify our target while performing requests. But if the location of this service gets changed, then we also have to change our configuration to make requests to the new location. And what if we have multiple workers performing the same job? Changing configuration each time can be troublesome.

Service Discovery is about getting service details given some of its meta-data (i.e., name qualifying the service). The service that needs to be called needs to send its info to register with meta-data, which will help retrieve needed details. This meta-data should be unmodifiable. Instead of performing requests directly, we will fetch data from Service Discovery Register and then use this data.

In this tutorial, we will create a mini-system that will use Eureka for communication between services:

  1. Eureka Service Registry
  2. Eureka Client
  3. Using Eureka for Service Discovery

Source code is available here: https://gitlab.4soft.tech/service-discovery-eureka

1. Eureka Service Registry

First, we need to create a registry for services, which will be called by those to register and get information about other services.

Requirements

  • JDK (preferably 16)
  • IDE or text editor

Project Generation

  • To generate the project, we can use Spring Initializr, but I will use a plugin to IntelliJ IDEA, which is the IDE that I will be using in this article.
  • In the New Project window, select the option ‘Spring Initializr’, then fill in the general settings of the project according to our needs and preferences. Click Next. Next, search for Eureka Server dependency, select it, then click on ‘finish’.

Application Configuration

  • To enable the Service Discovery register, add the @EnableEurekaServer annotation to your main class.

package co.soft.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

  • In the application.properties file, disable the eureka client since our server will work only as a register.

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

  • You can also set the port to a specific number to avoid overlapping when running on the same machine.

server.port=8761

Testing

  • To start the application, you can use your IDE or Gradle/Maven. In the example, we used Gradle, so the given command would be Gradle bootJar in the root directory or ./gradlew bootJar while using the Gradle wrapper.
  • When we visit http://localhost:8761/, we can see the Eureka UI, which provides details about our registry.

Done! Now, we will explore this view more during client creation.

2. Greetings Service

We have built a Eureka Service Registry, so now it is time to create a client that will use it.

Requirements

  • JDK (preferably 16)
  • IDE or text editor

Project Generation

  • Generate this project as you did with the Registry Server, but select Eureka Discovery Client and Spring Web dependencies.

Application Configuration

  • To enable the Service Discovery client, add the @EnableEurekaClient annotation to your main class.

package co.soft.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

  • In application.properties, specify the service registry URL.

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

  • Also, specify the name to be registered, which by default is fetched from the application name property.

spring.application.name=greetings-service

Testing

  • Start the application using your IDE or Gradle/Maven. In this example, the command is Gradle bootJar in the root directory or ./gradlew bootJar with the Gradle wrapper.
  • Visiting http://localhost:8761/ shows that our client is registered.

3. Using Eureka for Service Discovery

Now, let's create a Eureka Service Registry and integrate it with our clients, using multiple clients to communicate within the system.

Requirements

  • JDK (preferably 16)
  • IDE or text editor

Project Generation

  • Create a second service, called Common Service, in the same way as the Greetings Service. In the application.properties file, ensure you have specified the needed properties.

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.services.greetings-service.name=greetings-service
spring.application.name=common-service
# While running all services on a single machine, make sure different ports are being used
server.port=8081

For readability, use Lombok, which generates some boilerplate code. Add Lombok dependencies into the build.gradle file (or pom.xml if using Maven) and enable annotation processing in your IDE settings.

Basic Communication

  • Create a simple endpoint in the Greetings Service and Common Service. The Common Service endpoint will make a call to our Greetings Service.

// Greetings Service endpoint
// ... (code example)

// Common Service endpoint
// ... (code example)

  • Create the GreetingsService class in OtherEurekaClientService.java.

// GreetingsService class code
// ... (code example)

  • Define the service name in application.properties and configure the RestTemplate to perform requests with it.

Testing

  • First, call the service directly to ensure it works properly. Then, call the other service which will fetch data from the first one from the registry.
  • Observe the successful communication between services.

Summary

That concludes the tutorial. I hope it was helpful! If you have any questions or would like more elaboration, please let us know in the comments.

February 22, 2022