Service Registration and Discovery using Springboot and Netflix Eureka

Rahul Kumar
4 min readJun 29, 2020

What is Eureka?
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component, the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing.

ref- https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance

At Netflix, a much more sophisticated load balancer wraps Eureka to provide weighted load balancing based on several factors like traffic, resource usage, error conditions etc to provide superior resiliency.

In simple word, Eureka is a service Registry or we can say it is an embedded server provided by Netflix third party which integrate with spring framework.

Our main purpose to use Eureka: Microservice Registration and Discovery with Spring Cloud and Netflix’s Eureka

How is Eureka used at Netflix?
At Netflix, Eureka is used for the following purposes apart from playing a critical part in mid-tier load balancing.

  1. For red/black deployments with Netflix Asgard- an open source service which makes cloud deployment easier. Eureka interacts with Asgard to make switching between older/newer release of a deployment of services quick and seamless in case of problems — especially given that launching 100’s of instances to deploy a release could take a long time.
  2. For our cassandra deployments to take instances out of traffic for maintenance.
  3. For our memcached caching services to identify the list of nodes in the ring.
  4. Used for carrying other additional application specific metadata about services for various other reasons.

When should I use Eureka?

Generally, in microservice architecture we develop separate services and expose each API as service.

Services calling in microservices architecture

Here using rest-client we are accessing three services. As the scale of my application will grow, my API will also grow so as to rest API URLs. To overcome this issue Netflix came up with Eureka as a service registry.
So Eureka as a Service Registry, just register all microservices with Eureka server then there is no need to call each service by passing the exact URL like host, port. Only pass your service name which is registered with Eureka server and append the endpoint URL.

Normally we use URLs like this to get the response from any service URL -

http://localhost:8082/products

Instead of pass this URL from the client just pass like below you no need to bother about what’s the port and hostname. So when you will use Eureka as a service registry, it will take care of the route if that service is registered with Eureka. You’ll just have to pass the endpoint URLs.

So now to hit the any product API, you’ll have to pass the URLs like —

http://Product-Service/products

How Eureka Works?

Once you register your API on the Eureka server, this internally keeps one instance of your service, so that it can delegate the request to the service with proper details.

Start a Eureka Service Registry

You first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer to stand up a registry with which other applications can communicate. This is a regular Spring Boot application with one annotation (@EnableEurekaServer) added to enable the service registry.

Annotations Eureka Server

Add some properties to application.yml to handle all of these requirements

Properties in application.yml

Talking to the Registry

Now that you have started a service registry, you can stand up a client that both registers itself with the registry and uses the @EnableEurekaClient abstraction to interrogate the registry for its own host and port.

Then you’ll have to configure your rest controller and then register your service with Eureka using application.yml

Compile and run your Eureka server as well as your rest service. Go to http://localhost:8761/eureka , you’ll be able to see your registered service.

Summary

Congratulations! You have just used Spring to stand up a Netflix Eureka service registry and to use that registry in a client application.

--

--

Rahul Kumar

Programmer | Freelancer | Thinker | Open Source | Tech Mantra