Categories
microservices spring-boot

register spring boot micro-services to eureka discovery

This tutorial is a continuation of our previous tutorial, Here we will learn about register spring boot micro-services to eureka discovery

If you want to learn about eureka please refer my previous post https://beginnersbug.com/create-netflix-eureka-discovery-server-using-spring-boot/

Prerequisite
  • JDK
  • Eclipse
  • Spring boot micro service knowledge
  • Netflix Eureka knowledge
Annotation used to register
@EnableDiscoveryClient
Dependency
<parent>                                                                
  <groupId>org.springframework.boot</groupId>                           
  <artifactId>spring-boot-starter-parent</artifactId>                   
  <version>2.2.6.RELEASE</version>                                      
  <relativePath /> <!-- lookup parent from repository -->               
</parent> 

<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>

<dependencyManagement>                                                  
  <dependencies>                                                        
    <dependency>                                                        
      <groupId>org.springframework.cloud</groupId>                      
      <artifactId>spring-cloud-dependencies</artifactId>                
      <version>${spring-cloud.version}</version>                        
      <type>pom</type>                                                  
      <scope>import</scope>                                             
    </dependency>                                                       
  </dependencies>                                                       
</dependencyManagement> 
application.properties
server.port=8080
spring.application.name=student
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
Main Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class StudentApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudentApplication.class, args);
	}
}
Register spring boot

Time needed: 30 minutes

Steps

  1. Create a spring boot application

    Use https://start.spring.io/ to create spring boot applicationregister spring boot micro-services to eureka discovery server

  2. Add Dependency

    Add eureka discovery client and spring web dependency in the text box

  3. Click on the generate

    Once you clicked Generate you project will download

  4. Import into your IDE

    Import the downloaded project into your IDE. Here I used eclipse IDE for my development

  5. Add @EnableDiscoveryClient annotation

    Once you imported you project in IDE, Go to the Main class and add @EnableDiscoveryClient annotation.
    This annotation will register spring boot micro-services to eureka discovery

  6. change application.properties

    eureka.client.registerWithEureka=true
    eureka.client.fetchRegistry=true
    eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

  7. Build and Run

    Now you can run your application

  8. Testing

    Open browser and navigate to http://localhost:8761/

  9. You can see your student micro service under Instances

Exception

In case if you didn’t configure the properties properly, you might get below exception.

TransportException: Cannot execute request on any known server
Solution

Confirm below property in your application.properties

eureka<span class="token punctuation">.</span>client<span class="token punctuation">.</span>serviceUrl<span class="token punctuation">.</span>defaultZone<span class="token operator">=</span>http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">8761</span><span class="token operator">/</span>eureka

Make sure your discovery server is running on http://localhost:8761

Github

https://github.com/rkumar9090/student

Related Articles

create netflix eureka discovery server using spring boot

Leave a Reply

Your email address will not be published. Required fields are marked *