Creating microservice consumers

To consume this RESTful microservice, let's create a consumer web application. This web application will consume RESTful service endpoints. Spring provides several ways to consume microservices, but in this web application, we will use the RestTemplate class. This RestTemplate class allows you to send HTTP requests to a RESTful server and fetch data in a number of formats, such as JSON and XML.

The format of data depends on the presence of marshalling classes on the classpath of the web application. The web application will support the JSON format if Jackson JARS are present in the classpath. Similarly, it will support the XML format if JAXB JARS are present in the classpath.

In this web application, the WEB-APPLICATION component depends on the backend microservice (ACCOUNT-SERVICE). This application will talk to the account microservice by using a logical service name rather than hardcoding the location of the microservice, and this web application asks Eureka to resolve the host and port of this microservice.

Let's see the following main application class of this web application:

package com.dineshonjava.webapplication; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.loadbalancer.LoadBalanced; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.context.annotation.Bean; 
import org.springframework.web.client.RestTemplate; 
 
@SpringBootApplication 
@EnableEurekaClient 
public class WebApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(WebApplication.class, args); 
   } 
    
    @LoadBalanced     
    @Bean 
    RestTemplate restTemplate() { 
        return new RestTemplate(); 
    } 
} 

This class has the @EnableEurekaClient annotation to register itself with the registry service. And, one more thing; I have configured a bean that is RestTemplate using the @LoadBalanced annotation. That means our web application has load-balanced RestTemplate.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.181.57