Spring Boot - Eureka Server

 Microservices communicate with each other through exposed endpoints. For this communication to take place the dependent service should know the address of the other microservice to invoke its endpoint through a REST call, to get its job done. 

Traditionally, all these endpoints has been hardcoded in configuration properties of the respective system.  However, with dynamic scaling of services in place it become hard to keep track of their address every time a new instance was created or destroyed. To overcome this problem, we use Eureka servers. It contains a registry of services and a REST API that can be used to register a service, deregister a service, and discover the location of other services like running port and IP address. All of this happens dynamically as soon as the instance of the service comes up or is destroyed.

Eureka comes with two components - Eureka Client and Eureka Server.

Eureka Server

Keeps a record of all the services which have registered.

Enabling Eureka Server

Navigate to https://start.spring.io/ and download the Spring Boot project with Eureka Server dependency. If you already haven empty Spring Boot project, then you need add explicitly the below dependency to it's pom.xml

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

And in the application.properties add the following

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Since it is the discovery server, you set the register-with-eureka and fetch-registry to false, this needs to be enabled on the client. That's all we need to add from dependency and configuration perspective in the server application. Next, we have to make our application an Eureka Server, and for that we need to annotate our application's main class with an @EnableEurekaServer annotation.

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerPrototypeApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerPrototypeApplication.class, args);
}

}

That's is all, we are all set with Eureka Server. Run the application and navigate to http://localhost:8761, you will see the Eureka dashboard like below. As you can see the server is up, but no client is connected to it yet. Since we haven't registered any clients so far.



Eureka Client

It can be a microservice that is ready to work so it register itself to the Eureka service.

Enabling Eureka Client

To include Eureka client in our project we just need to add a dependency to our spring boot application.

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Next, we need to add the following in the application.properties.

spring.application.name=zuul-api-gateway
server.port=8765
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.application.name represents the name of the application, it will be registered in the Eureka server with that name. And we need to set the address of our Eureka Server Instance (http://localhost:8761/eureka/) in the property eureka.client.service-url.defaulZone.

Finally we will annotate our client application with @EnableEurekaClient or @EnableDiscoveryClient

Now start the client application, and refresh your Eureka Dashboard. It the instance was not showing up give it some time, it will take some time to register.


That's all, you have your Eureka Server and Client up and running.

Happy Programming...!!!


Comments

Popular posts from this blog

Method Reference in Java Streams

PHP LARAVEL Directory Structure

PHP LARAVEL - Hello World Laravel