Spring Cloud: Service Discovery With Eureka
Learn the fundamentals of Service Discovery and Spring Cloud Eureka with code examples.

One of the main building blocks of a micro-service architecture is service discovery. Spring cloud provides multiple solutions such as Eureka, Zookeeper, Cloud Foundry and Consul to facilitate the process of service discovery. This piece is aimed at providing you a simple explanation of service discovery with Eureka. I will be covering the following topics in this article.
- What is Service Discovery?
- What is Spring Cloud Eureka?
- Implementing a Eureka Server and a Client
- Inter-Microservice Communication via Eureka
- Understanding Eureka Client-Server Communication
What is Service Discovery?
A distributed system typically comprises a large number of services which communicate with each other to perform certain operations. Service discovery is the process of one service dynamically discovering the network location (IP address and port) of another service to communicate with it.
Imagine a scenario in which one REST service (Service A) is trying to invoke another REST service (Service B). In order to make a request, Service A needs to know the network location (IP address and port) of Service B. In a conventional SOA (Service Oriented Architecture) ecosystem, services’ network locations would hardly change, as they are deployed in on-premise data centers. Consequently, you can afford to maintain the network locations of services in configuration files, which will be updated infrequently. For example, Service A can maintain the IP address and port of Service B in a configuration file and use those values when making a request. Following figure illustrates this flow.

However, this approach is nearly impossible in a cloud based microservice architecture due to following reasons.
Increased number of services: As you may well aware, microservice architecture is all about breaking down monoliths into fine grained services. This results in an increased number of services that forms a complex communication mesh. Therefore, it is difficult for one service to maintain the network locations of all the other services, that it has to communicate with, in a property file.

Dynamically assigned network locations: Microservices are generally deployed in the cloud. Server instances in cloud have dynamically assigned network locations. In addition, due to its basic features such as auto scaling, servers just come and go in cloud. Each time a service is started in a new instance, its network location changes. Therefore, it is hard to maintain the target IP addresses and port numbers of a particular microservice in a property file, as the values tend to change quite frequently.
These complications raised the need to have a more sophisticated mechanism for microservices to dynamically discover the network locations of other microservices for communication. The concept of service discovery was introduced as a result. Service discovery mechanism uses a central registry to maintain the network locations of all the microservices. If for some reason the IP address and the port number of a particular microservice changes, new values will be immediately re-registered in the registry.

What is Spring Cloud Eureka
Eureka is a REST based service which is primarily used for acquiring information about services that you would want to communicate with. This REST service is also known as Eureka Server. The Services that register in Eureka Server to obtain information about each other are called Eureka Clients. Following diagram illustrates how Eureka clients and server fit in together.

As the above diagram indicates, Service A and Service B are registered in Eureka server. If any of the these two services want communicate with the other service, it can obtain the target IP address and the port via the Eureka server.
Implementing a Eureka Server and a Client
Setting up a Spring based Eureka server and a client is fairly straight forward. Spring.io provides a very concise step by step guide here, which will help you to quickly setup an Eureka server and a client.
And also, I implemented a sample Eureka server and a client which I will use to cover this section. Please note that I will not go through step by step, but will explain the important points. You can find the code for this sample application here.
Setting Up the Eureka Server
“pom.xml” File








