Tuesday, June 3, 2025

MS - Call rest service to service call Step2; Call with Autowired RestTemplate object

 Earlier we just made a RestTemplate object and call the other rest-service using "getForObject" method providing url and response type

//Phase 1 - without Discovery/Eureka client
Greet greet = restTemplate.getForObject("http://localhost:8090/greet", Greet.class);

Here Problems

1. URL is hard coded and also port --> Better if we can call with service name -->What we need to do


Solution

1. who knows the service-name ?

1.1 Discovery server has the 'service-name', so we configure the rest-client service with discovery-server making rest-client service as client for Discovery server. @EnableDiscoveryClient

2. Make RestTemplate Autowired and Load balanced


Now Step2: 

1. Make RestTemplate @Autowored

2. To make it Autowired, we need to create the bean of RestTemplate object. 

2.1 So here we create a bean inside Springboot main class 

2.2 make it @LoadBalanced

3. To find the service add @EnableDiscoveryClient for the main class and discovery client dependency

3.1 add Eureka client dependency

    3.1.1 when added dependency - add properties configure version

    3.1.2 add dependency management

3.2 add eureka client properties to configure

Below are the code blocks with changes

Main Class

package com.example.rest_client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class RestClientApplication {

public static void main(String[] args) {
SpringApplication.run(RestClientApplication.class, args);
}

@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}


Controller class

package com.example.rest_client.controller;

import com.example.rest_client.model.Greet;
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 GreetClientController {

// private RestTemplate restTemplate = new RestTemplate();
@Autowired
private RestTemplate restTemplate;

@RequestMapping("/greet-call")
Greet greet() {
//Phase 1 - without Discovery/Eureka client
// Greet greet = restTemplate.getForObject("http://localhost:8090/greet", Greet.class);
//With Discovery client - Now we can use service name - and with other option available via eureka server
// you need to configure DiscoveryClient - else cannot find hte hostname "greet-service" which is registered inEureka server
Greet greet = restTemplate.getForObject("http://greet-service/greet", Greet.class);
return greet;
}
}


application properties

spring.application.name=rest-client
server.port=8095

#phase 2 - add eureka
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/

POM

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

When you add the above dependency you need to configure the version in the properties and the dependency management

<properties>

<spring-cloud.version>2025.0.0</spring-cloud.version>
</properties>
<!--    added dependency mgt after adding discovery client 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>


No comments:

Post a Comment