Spring MicroService registration with Eureka Server
February 12, 2020 | Spring boot complete tutorial with example | No Comments
In Last Post ( Eureka Server ) we have seen that how to create a Eureka Server. Now we learn how to register our microservices on Eureka Server, We register our microservice on Eureka because of we need a dashboard where we can check status of our all microservices. So for registering them on eureka we have to follow below steps.
- Create a project and add required dependency in your pom.xml file
- Enable your main class as a client using @EnableEurekaClient annotation.
- Define Eureka Server path in your application.properties file.
Follow above steps:
1- Add Below dependency in pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2- Enable Eureka Client using @EnableEurekaClient
package com.javadream; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class MicroService1Application { public static void main(String[] args) { SpringApplication.run(MicroService1Application.class, args); } }
3- Define below lines in your application.properties file
spring.application.name=microservice-1 server.port=8081 eureka.client.service-url.default-zone=http://localhost:8761/eureka
Now run this application and check your Eureka Server You will get a instance of MICROSERVICE-1 on port 8081.
NOTE: Make Sure before running your microservice-1 application your eureka server is already running otherwise it will throw exception that no server found.
Complete Code On GitHub : Register MicroService Source Code
