Search Tutorials


Spring Cloud Tutorial- Microservice Discovery using Netflix Eureka Hello World Example | JavaInUse

Spring Cloud- Microservice Registration and Discovery using Netflix Eureka

In this series we learn how to use Netflix Spring Cloud Components. In these posts we make use of Netflix component Eureka for service registry and discovery.

Spring Cloud - Table Of Contents

Microservice Registration and Discovery with Spring cloud using Netflix Eureka- Part 1. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 2. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 3. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 4. Spring Cloud- Netflix Eureka + Ribbon Simple Example Spring Cloud- Netflix Eureka + Ribbon + Hystrix Fallback Simple Example Spring Cloud- Netflix Hystrix Circuit Breaker Simple Example Spring Cloud- Netflix Feign REST Client Simple Example Spring Cloud- Netflix Zuul +Eureka Simple Example Spring Cloud Config Server using Native Mode Simple Example Spring Cloud Config Server Using Git Simple Example Spring Boot Admin Simple Example Spring Cloud Stream Tutorial - Publish Message to RabbitMQ Simple Example Spring Cloud Stream Tutorial - Consume Message from RabbitMQ Simple Example Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus Spring Cloud Tutorial - Stream Processing Using Spring Cloud Data Flow Spring Cloud Tutorial - Distributed Log Tracing using Sleuth and Zipkin Example Spring Cloud Tutorial - Spring Cloud Gateway Hello World Example Spring Cloud Tutorial - Spring Cloud Gateway Filters Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka + Netflix Hystrix Example Spring Cloud Tutorial - Secure Secrets using Spring Cloud Config + Vault Example
In previous post we registered a microservice to Eureka server. This post we consume this service by discovering the employee-producer service from the Eureka server.

Video

This tutorial is explained in the below Youtube Video.


sprcloud_4-3

Lets Begin-

  • Modify the employee-consumer service as follows-

    Modify the pom.xml with spring cloud dependencies as follows-
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.javainuse</groupId>
    	<artifactId>employee-consumer</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.4.1.RELEASE</version>
    		<relativePath /> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-eureka</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    
    	</dependencies>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>Camden.SR6</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    
    </project>
    
    Modfy the ConsumerControllerClient class to autowire the DiscoveryClient dependency.
    package com.javainuse.controllers;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.client.RestClientException;
    import org.springframework.web.client.RestTemplate;
    @Controller
    public class ConsumerControllerClient {
    	
    	@Autowired
    	private DiscoveryClient discoveryClient;
    	
    	public void getEmployee() throws RestClientException, IOException {
    		
    		List<ServiceInstance> instances=discoveryClient.getInstances("employee-producer");
    		ServiceInstance serviceInstance=instances.get(0);
    		
    		String baseUrl=serviceInstance.getUri().toString();
    		
    		baseUrl=baseUrl+"/employee";
    		
    		RestTemplate restTemplate = new RestTemplate();
    		ResponseEntity<String> response=null;
    		try{
    		response=restTemplate.exchange(baseUrl,
    				HttpMethod.GET, getHeaders(),String.class);
    		}catch (Exception ex)
    		{
    			System.out.println(ex);
    		}
    		System.out.println(response.getBody());
    	}
    
    	private static HttpEntity<?> getHeaders() throws IOException {
    		HttpHeaders headers = new HttpHeaders();
    		headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
    		return new HttpEntity<>(headers);
    	}
    }
    




Modify the application.properties to include the eureka server url-
server.port=8091
eureka.client.serviceUrl.defaultZone=http://localhost:8090/eureka

Add the bootstrap.properties as follows-
spring.application.name=employee-consumer
Run this as java application. We can see the the employee-producer is successfully consumed-

sprcloud_4-1
Go to URL-
http://localhost:8090/
We can see the Eureka Server page as follows-

sprcloud_4-2
  • So the producer and client are successfully registered with Eureka Server.

    Download Source Code

    Download it -
    Employee Consumer Service with Eureka Registration

    See Also

    Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions
  •