How to Use Spring Cloud Eureka Service Discovery

In this article, we are going to learn How to User Spring Cloud Eureka Service Discovery. In the previous article we have seen how we can configure our config server using Eureka as We as we will understand by writing an Eureka Client in this example.

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 Micro services running on different ports. How do we manage these? Maintenance and managing different ports and their addresses are fairly complex, as the volume of Micro services 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 Micro services 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.

When a client registers with Eureka, it provides meta-data about itself such as host and port, health indicator URL, home page etc. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.

package com.frugalis.EurekaServer;

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);
    }
}

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

server.port=8761

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

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

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

$ mvn clean install

$ mvn spring-boot:run

Navigate to http://localhost:8761/eureka.If you see a Screen like this below, Then you have successfully set up 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.

spring.application.name=customer-service
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
management.security.enabled=false
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2
  • 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.
  • Here “defaultzone” is a fallback value that provides the service URL for any client that doesn’t express a preference .

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.

Have a look at the tutorial in practical guide of implementing a Eureka Service and Client Implementation.