by BehindJava

How to configure Eureka Discovery Service as a Eureka Client and register it in Eureka server

Home » microservices » How to configure Eureka Discovery Service as a Eureka Client and register it in Eureka server

In this tutorial we are going to learn about Eureka Discovery Service and Configuring Eureka client in the Eureka Server using Spring Boot.

Firstly, we need to generate as Spring application using Spring Initializr and add the following dependencies as shown below.

EurekaClient

Once the project is generated. It is download as a ZIP file, UNZIP the project and import in your Spring Tool Suite.

So, to make our Spring Boot application work as a Eureka Discovery client, we need add an annotation called @EnableEurekaClient in the main class as shown below.

Code Snippet:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClientpublic class UserMicroServiceApplication {

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

}

Create a Rest Controller in the UserMicroService application to hit the http endpoint through the automatic port number and Ip address provided by the Eureka server.

Code Snippet:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UserRestController {

	@GetMapping("/status/check")
	public String status() {
		return "UserMicroService  is Working!!!";
	}
}

Now we need to add few configurations under src/main/resources in the application.properties as shown below.

Code Snippet:

server.port=0
spring.application.name=user-ms1
eureka.client.service-url.defaultZone=http://localhost:8010/eurekaspring.devtools.restart.enabled=true

Once all the above development done in the UserMicroService application. we need to run the Eureka server application and now we need to run the UserMicroService application which is configured as a Eureka client and it gets registered in the Eureka server automatically due to the configurations provided in the application.properties as shown in the above.

Now hit the URL http://localhost:8010/ on which Eureka server is running and we can see our UserMicroService in the “Instances currently registered with Eureka”. Under status we can find the Eureka server assigned Ip address and port number on which our UserMicroService is running.

Test the above HTTP Get endpoint using the URL http://IP-Address:PortNo/users/status/check

title

In the next tutorial we are going to learn about configuring the custom routes using the Spring Cloud API Gateway.