从货币转换服务调用货币兑换服务

我们已经准备好了currency-exchange-service,并且已经设置了currency-calculation-service(currency-conversion-service)。现在,我们将从货币计算服务中调用货币兑换服务。

我们使用RestTemplate()构造函数来调用外部服务。让我们创建一个RestTemplate并尝试调用currency-exchange-service。

步骤1: 选择currency-conversion-service项目。

步骤2: 打开CurrencyConversionController.java并创建一个新的RestTemplate,以调用currency-exchange-service应用程序。

步骤3: 调用RestTemplate类的getForEntity()方法。

getForEntity(): 这是RestTemplate类的方法,它使用指定的URL使用HTTP GET方法检索实体。它将响应转换并存储在ResponseEntity中。它返回ResponseEntity

参数: 它接受两个参数:

  • URL: URL。
  • responseType: 返回值的类型。
ResponseEntity<CurrencyConversionBean>responseEntity=new RestTemplate().getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}", CurrencyConversionBean.class, uriVariables);  

步骤4: 在URL参数中,将currency-converter-service的URL放在那里,即http://localhost:8000/currency-exchange/from/{from}/to/{to}。它从请求中获取变量{from}{to}的值。无论请求中发送什么,我们都会将其发送到currency-exchange-service。

步骤5: 在上述URL中,我们需要传递两个值“from”“to”。为了传递这些值,为URI变量创建一个Map。将uriVariables作为参数传递到URI中。

Map<String, String>uriVariables=new HashMap<>();  
uriVariables.put("from", from);  
uriVariables.put("to", to);  

步骤6: 我们期望的响应类型是CurrencyConversionBean,因此将响应存储在CurrencyConversionBean中。

CurrencyConversionBean response=responseEntity.getBody();  

CurrencyConversionController.java

package cn.javatiku.microservices.currencyconversionservice;  
import java.math.BigDecimal;  
import java.util.HashMap;  
import java.util.Map;  
import org.springframework.http.ResponseEntity;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
@RestController  
public class CurrencyConversionController  
{  
@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}") //where {from} and {to} represents the column   
//returns a bean back  
public CurrencyConversionBeanconvertCurrency(@PathVariable String from, @PathVariable String to, @PathVariableBigDecimal quantity)  
{  
//setting variables to currency exchange service  
Map<String, String>uriVariables=new HashMap<>();  
uriVariables.put("from", from);  
uriVariables.put("to", to);  
//calling the currency-exchange-service  
ResponseEntity<CurrencyConversionBean>responseEntity=new RestTemplate().getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}", CurrencyConversionBean.class, uriVariables);  
CurrencyConversionBean response=responseEntity.getBody();  
//creating a new response bean and getting the response back and taking it into Bean  
return new CurrencyConversionBean(response.getId(), from,to,response.getConversionMultiple(), quantity,quantity.multiply(response.getConversionMultiple()),response.getPort());  
}  
}  

步骤7: 分别运行两个服务。当我们运行货币转换时,返回的响应如下所示:

invoking-currency-exchange-service-from-currency-conversion-service.png

转换倍数与数量相乘,返回totalCalculatedAmount为65000.00。这意味着1000美元等于65000.00印度卢比。它还显示端口8000,表示另一个服务(currency-exchange-service)正在端口8000上运行。

标签: spring, Spring教程, spring cloud, spring cloud教程, spring cloud框架, spring cloud面试题, springcloud组件, springcloud微服务架构, springcloud入门教程, springcloud主件, spring cloud架构图