In the previous tutorial, I introduced you to the Eureka Server to allow services in a Microservices system can register and search for each other. In this tutorial, I will show you how services can be registered with Eureka Server using Eureka Client, with Spring Cloud Netflix.
As an example of this tutorial, I will be using the Eureka Server that I created using Spring Cloud Netflix in my previous post. The UI of Eureka Server is as follows:
Now, I will create a service using Spring Boot project that uses the Eureka Client and Web dependencies as follows:
Result:
To register this service with Eureka Server, we have 3 steps to do:
First of all you need to declare our application as an Eureka Client first.
To do this, declare annotation @EnableEurekaClient to the main class of the application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springcloud.eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class SpringCloudEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaClientApplication.class, args); } } |
The second step, we need to do is declare the Eureka Server information in the Spring Boot application.properties configuration file.
We will declare the eureka.client.service-url.default-zone property with the URL value that points to the Eureka Server as follows:
1 |
eureka.client.service-url.default-zone=http://localhost:8761/ |
The last step is to identify our service with other services, you need to name our application.
We will use the spring.application.name property and declare it in the configuration file of the application, application.properties, to do this.
1 |
spring.application.name=Eureka Client Example |
At this point, if you start our application and access the Eureka Server, you will see the following result:
As you can see, in the “Instances currently registered with Eureka” section, the application which we just created, has registered with Eureka Server and is ready for other services to find it.