In previous post we registered a microservice to Eureka server. This post we consume this service by discovering the student-producer service from the Eureka server.
Part 1 - Overview of Netflix components.
Part 2 - Develop an Student service to produce and consume REST API using Spring Boot
Part 3 - Use Eureka for Service Registration
THIS - Use Eureka for Service Discovery
Part 5 - Load balancing using Netflix Eureka + Ribbon
First, we will modify the student-consumer service. 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>org.example</groupId>
<artifactId>student-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Modify the ConsumerControllerClient class to autowire the DiscoveryClient dependency.
package com.stackdev.controllers;
import java.io.IOException;
import java.util.List;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
@RestController
public class ConsumerControllerClient {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/student/consumer", method = RequestMethod.GET)
public String getStudent() throws RestClientException, IOException {
List<ServiceInstance> instances=discoveryClient.getInstances("student-producer");
ServiceInstance serviceInstance=instances.get(0);
String baseUrl=serviceInstance.getUri().toString();
System.out.println("Base URL: " +baseUrl);
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());
return ("connected to:" +baseUrl);
}
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 employee-producer is successfully consumed -
Go to URL - http://localhost:8091/
We can see the Eureka Server page as follows -
So the producer and client are successfully registered with Eureka Server. In the next post we make use of Netflix Ribbon for Client Side Load Balancing.