Wednesday, May 1, 2019

11 - Spring boot Consuming a microservice with RestTemplate and Eureka



Part 1 to install Eureka Server and let it discover the Eureka client


application.properties

  
server.port=8080
spring.application.name=consumer-service

  
  

POM.xml

    

<!--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>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>Tuto10EurekaClientConsumer</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Tuto10EurekaClientConsumer</name>
 <description>Demo project for Spring Boot</description>

 <properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>Greenwich.SR1</spring-cloud.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-netflix-eureka-client</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
  </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>${spring-cloud.version}</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>

  
  


Tuto10EurekaClientConsumerApplication


package com.example.clientConsumer;

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

 public static void main(String[] args) {
  SpringApplication.run(Tuto10EurekaClientConsumerApplication.class, args);
 }
 
    @Bean
    @LoadBalanced
 public RestTemplate restTemplate() {
  
  return new RestTemplate();
 }
}

  


ClientConsumerController


package com.example.clientConsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientConsumerController {
 
 @Autowired
 RestTemplate restTemplate;
 
 @RequestMapping("/consumer")
 public String getClientMessage() {
  
  String url = "http://localhost:8081/hello";
  String url1 = "http://myclient-service/hello";
  String res = restTemplate.getForObject(url1, String.class);
  return "Message : "+res;
 }

}

  


This is just a trick, hope it paved your way out. 


No comments:

Post a Comment