Enable Eureka on the Client Side

In the last post we have setup Eureka as a server. Now we have to create a new microservice that act as a client application and when we start this microservice it ‘ll register to the Eureka server.

The process is very simple we just have to follow the below step to register our microservice to the EurekaServer.

First add the below dependency in your pom.xml file

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

The main dependency is eureka-client. We have added starter-web dependency also because we ‘ll create a endpoint that display all the service registered to the eureka server.

Now use the @EnableDiscoveryClient annotation in your main class.

package com.javadream.EurekaClient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

Add below properties in your application.properties file

spring.application.name=EurekaClient
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

Just see the last property that we mentioned above eureka.client.serviceUrl.defaultZone, We have to give the url of our Eureka server appended by /eureka as a value for this property. As in our case eureka server is running on http://localhost:8761 so our value ‘ll be look like,

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

Now Just run your Eureka server first. Once your Eureka Server is UP and running then run this client application. Once this client application UP and running go to your eureka dashboard you ‘ll see this client application instance register succesfully.

Eureka dashboard

Till now everything good. Now we ‘ll create a endpoint in our client microservice that display list of registered microservice to the eureka server. For this we create a controller class and put the below code there.

package com.javadream.EurekaClient.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EurekaClientController {


	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("/fetchAllServices")
	public List<String> fethcAllRegisteredService() {
		List<String> services = discoveryClient.getServices();
		return services;
	}
}

Now just restart your client microservice and try to access the /fetchAllServices endpoint. You ‘ll see the list of all the microservice that is registered on eureka server.

As of now we have only one application registered to the eureka server that is client application itself. So when we ‘ll call this /fetchAllServices endpoint we ‘ll see our service name in the output result. It might take some time to display as if you not see the result just wait for on1 or two minute and try to referesh the page you ‘ll see the result.

Eureka Client microservice

You can take more details about eureka client from the Spring official documentations https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html