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:
Source code is available here: https://gitlab.4soft.tech/service-discovery-eureka
First, we need to create a registry for services, which will be called by those to register and get information about other services.
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);
}
}
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
Done! Now, we will explore this view more during client creation.
We have built a Eureka Service Registry, so now it is time to create a client that will use it.
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);
}
}
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=greetings-service
Now, let's create a Eureka Service Registry and integrate it with our clients, using multiple clients to communicate within the system.
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.
// Greetings Service endpoint
// ... (code example)
// Common Service endpoint
// ... (code example)
// GreetingsService class code
// ... (code example)
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.