How to Use Spring Cloud Eureka Service Discovery

How to Use Spring Cloud Eureka Service Discovery:-

In this article, we are going to learn How to User Spring Cloud Eureka Service Discovery. We are going to explore and understand how different microservices can register themselves and discover other services and call their API.

Spring cloud eureka service discovery is going to solve one of the core problems developers face while implementing a microservices architecture.

Lets try and understand this, Suppose we have a set of microservices running on different ports. How do we manage these? Maintenance and managing different ports and their addresses are fairly complex, as the volume of microservices increases. Once your services become distributed across regions it becomes much harder.


In a distributed environment this concept is called service registration and discovery. We build a system where we maintain a registry of all the microservices deployed. All other microservices can act as a client and register themselves with this system, they can also discover other microservices registered in the system.




How this system keeps a track of Working and functional Microservices that are UP and Running?

This system will receive periodic heartbeats from the services registered in this system , if any of the services fails to send a heartbeat it de-registers that.

  • This way it maintains a well-balanced ecosystem of microservices. This system discussed above is called  Service Discovery Server.
  • We are going to use Eureka as our Service discovery which is well integrated inside Spring Cloud.

We are going to Create Our Eureka Service Discovery Server and register microservices. let’s take a step forward and start coding our Eureka Service Discovery.

Eureka Service Discovery Server:-

Create a Spring boot project from Spring Boot initializer. Add the dependencies showed below. Extract project and open using eclipse or any other IDE.

SpringCloudEureka

Now open your main file and add annotation@EnableEurekaServer on the class level. This annotation tells Spring Cloud that we Setup a registry, So that other services can talk to the registry . In short, this annotation enables the Service Registry.

Now let’s add some properties to our application.properties.

Spring Cloud Eureka Service can have one or more replica of Registry server as well. If you don’t have one or more replica the spring cloud logs some error messages which we suppressed in our properties file.

In a typical prod environment, we will have multiple instances of our registry server.  Eureka Service Registry server tries to register themselves as well, hence we suppressed that as well in our properties file.



Run the Eureka Registry Server:-

Navigate to http://localhost:8761/eureka.If you see a Screen like this below, Then you have successfully setup a basic Eureka Registry Server.

Spring Eureka Server

How To Register Our Microservices With Eureka Service Registry:-

Now that Our Eureka Service Registry is up and running, we will use our existing microservice that we created earlier in this post and register the microservice with Eureka Service Registry.

  • There are several implementations of service discovery like Zookeeper and Consul, We are using Eureka Discovery. Please add eureka discovery client dependency.spring-cloud-starter-netflix-eureka-client.
  • Now we need to add @EnableDiscoveryClient in our Spring Boot application to enable and register our service with Eureka Service Registry. We also need to add few properties inapplication.properties

We configure default-zone as the address and port of our Eureka server.

  • Add your service name as application name and add zones for your service.
  • We are disabling security as of now but you can always configure security like we do it using spring security.

If anyone of you wants to code with security please lemme know. We can configure multiple instances for our microservices in a production environment and register with Eureka.




 

Leave a Reply

Your email address will not be published. Required fields are marked *