Microservice Registration and Discovery with Eureka and FeignClient

netflix-eureka

In this post, I will try to guide you on how to develop with Spring’s wrapping of Netflix’s 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.

To implement this mechanism of service registration and service discovery we will utilize 3 applications.

Application 1 – EurekaServer – This is the service registry

Application 2 – EurekaClient – This is the application with endpoints (REST) that registers itself to EurekaServer

Application 3 – FeignClient – This is a registry-aware (discovery-aware) client. You can think of it as a discovery-aware Spring RestTemplate.

 

To create these 3 applications as fast as possible I would recommend using http://start.spring.io/ then importing the created projects into the IDE of your choice. If you have IntelliJ IDEA ultimate version, then spring initializr integration is already in there inside the IDE for you. You don’t need to go the spring initializr website to bootstrap your spring boot applications.

Add the following dependencies to your applications while creating them either with spring boot initializr website or IntelliJ IDEA, so that all the necessary artifacts are in your classpath:

EurekaServer – Eureka Server (In the sample code I also added “web” dependency but that is not really necessary for the purpose of this exercise)

EurekaClient – Eureka Discovery (spring-cloud-starter-eureka), Web (spring-boot-starter-web, to implement RETController and RESTful endpoints)

FeignClient – Feign (spring-cloud-starter-feign), Eureka Discovery

Below are the annotations that you need to use. I will briefly touch them here. Review the code I shared on GitLab for a better understanding.

@EnableEurekaServer – This is the annotation to create EurekaServer application. Nothing else is needed.

@EnableEurekaClient – This annotation should be used on the main application class of EurekaClient project. This is the annotations that will get your RESTful endpoints to be registered on EurekaServer. @EnableEurekaClient annotation is also used on the main application class of EurekaClient project but that is just there to monitor the EurekaClient application on Spring Eureka web console.

@EnableFeignClients – This is used on the main application class of FeignClient application so that we can create Feign Clients like GreetingClient.

@FeignClient – We provide the Feign Clients with the application name, freeing them from the dependencies of hostname and port information of the services/service providers. That is one of the biggest advantages of using Eureka and Feign Clients.