个性化阅读
专注于IT技术分析

Spring Cloud货币换算及货币兑换服务介绍

本文概述

在本节中, 我们将创建几个微服务:CurrencyCalculationService和CurrencyExchangeService。

货币换算及货币兑换服务介绍

注意:在本教程中, 我们引用了货币换算服务作为货币计算服务。两种服务具有相同的含义, 因此请不要混淆。

让我们了解这些服务的功能。

在上图中, CurrencyExchangeService使用JPA与数据库进行对话并返回特定货币的兑换值。例如, USD转换为INR。

当我们调用CurrencyExchangeService时, 我们需要传递两个参数:from(convert from)和to(convert to)。例如, 如果我们要将货币从USD转换为INR。

考虑URL http:// localhost:8000 / currency-exchange / from / USD / to / INR。它重新调整以下响应:

{
id: 101, from: "USD", to: "INR", conversionMultiple: 72, port: 8000
}

货币兑换服务将返回转换倍数。转换倍数等于1美元等于72卢比。货币转换器服务使用货币兑换服务。假设货币转换器服务希望将100 USD转换为INR。因此它将调用货币兑换服务, 并将转换我们在path参数中提供的指定金额。例如:

http:// localhost:8100 / currency-converter / from / USD / to / INR / quantity / 100

{
Id: 101, from: "USD", to: "INR", conversionMultiple: 72, quantity: 100
totalCalculatedAmount: 7200, port: 8000
}

我们将在示例中使用Spring Cloud实现这两项服务。

设置货币兑换服务

步骤1:打开spring初始化程序http://start.spring.io。

步骤2:选择项目:Maven项目, 语言:Java和Spring Boot版本2.2.0 M6或更高版本。提供组名和工件ID。我们分别为组名和工件ID提供了com.srcmini.microservices和currency-exchange-service。

货币换算及货币兑换服务介绍

步骤3:添加依赖项Web, DevTools, Actuator和Config Client。

步骤4:点击Generate Project按钮。它将下载项目的zip文件。

步骤5:将其解压缩到本地磁盘中。

步骤6:导入项目。

单击文件菜单->导入->现有Maven项目->下一步->浏览->选择项目->完成

导入需要一些时间。项目导入完成后, 将显示以下项目目录。不要考虑目录中的data.sql文件, 因为我们将在以后创建它。

货币换算及货币兑换服务介绍

步骤7:打开application.properties文件, 并配置应用程序名称和端口号。

application.properties

spring.application.name=currency-exchange-service.
server.port=8000

当我们运行currency-exchange-service时, 它将运行但不执行任何服务。下一步, 我们将在currency-exchange-service中实现代码。

对货币兑换服务进行硬编码

现在, 我们将创建一个将货币从USD转换为INR的服务。

步骤1:在com.srcmini.microservices.currencyexchangeservice包中创建一个名为CurrencyExchangeController的类文件(REST控制器)。

CurrencyExchangeController.java

package com.srcmini.microservices.currencyexchangeservice;
import java.math.BigDecimal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController 
public class CurrencyExchangeController 
{
@GetMapping("/currency-exchange/from/{from}/to/{to}")		//where {from} and {to} are path variable
public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to)	//from map to USD and to map to INR
{	
return new  ExchangeValue(1000L, from, to, BigDecimal.valueOf(65));
}
}

步骤2:创建一个名称为ExchangeValue的类文件。

ExchangeValue.java

package com.srcmini.microservices.currencyexchangeservice;
import java.math.BigDecimal;
public class ExchangeValue 
{
private Long id;
private String from;
private String to;
private BigDecimal conversionMultiple;

public ExchangeValue()
{	
}
//generating constructor using fields
public ExchangeValue(Long id, String from, String to, BigDecimal conversionMultiple) {
super();
this.id = id;
this.from = from;
this.to = to;
this.conversionMultiple = conversionMultiple;
}
//generating getters
public Long getId() 
{
return id;
}
public String getFrom() 
{
return from;
}
public String getTo() 
{
return to;
}
public BigDecimal getConversionMultiple() 
{
return conversionMultiple;
}
}

步骤3:运行CurrencyExchangeServiceApplication.java。它在我们在application.properties文件中配置的端口8000上运行。

我们在浏览器上收到以下响应:

{
id: 101, from: "USD", to: "INR", conversionMultiple: 72, port: 8000
}

在响应中设置动态端口

CurrencyExchangeService确定货币的兑换价值。 CurrencyCalculationService使用CurrencyExchangeService来确定一种货币在另一种货币中的价值。在下一主题的后面, 我们将创建CurrencyExchangeService的多个实例。

目前, 该服务在端口8000上运行。稍后, 我们将在端口8001、8002等上运行它。下一步, 我们将为currency-exchange-service设置一个端口。

步骤1:打开ExchangeValue.java文件并添加端口变量。仅为port变量生成getter和setter。

ExchangeValue.java

package com.srcmini.microservices.currencyexchangeservice;
import java.math.BigDecimal;
public class ExchangeValue 
{
private Long id;
private String from;
private String to;
private BigDecimal conversionMultiple;
private int port;
public ExchangeValue()
{	
}
//generating constructor using fields
public ExchangeValue(Long id, String from, String to, BigDecimal conversionMultiple) {
super();
this.id = id;
this.from = from;
this.to = to;
this.conversionMultiple = conversionMultiple;
}
//generating getters
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Long getId() 
{
return id;
}
public String getFrom() 
{
return from;
}
public String getTo() 
{
return to;
}
public BigDecimal getConversionMultiple() 
{
return conversionMultiple;
}
}

我们已经在application.properties文件中配置了应用程序名称和端口号, 因此无需再次配置。

现在从环境中获取端口号。

步骤3:打开CurrencyExchangeController.java并获取环境的属性。

CurrencyExchangeController.java.

package com.srcmini.microservices.currencyexchangeservice;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController 
public class CurrencyExchangeController 
{
@Autowired
private Environment environment;
@GetMapping("/currency-exchange/from/{from}/to/{to}") //where {from} and {to} are path variable
public ExchangeValue retrieveExchangeValue(@PathVariable String from, @PathVariable String to)  //from map to USD and to map to INR
{	
//taking the exchange value
ExchangeValue exchangeValue= new ExchangeValue (1000L, from, to, BigDecimal.valueOf(65));
//picking port from the environment
exchangeValue.setPort(Integer.parseInt(environment.getProperty("local.server.port")));
return exchangeValue;
}
}

当我们刷新浏览器时, URL更改为:http:// localhost:8000 / currency-exchange / from / USD / to / INR。

{
id: 1000, from: "USD", to: "INR"
conversionMultiple: 65, port: 8000
}

目前, CurrencyExchangeServiceApplication正在端口8000上运行。

现在, 我们将在其他端口号上运行CurrencyExchangeServiceApplication。为此, 我们必须根据需要将application.properties文件中的端口从8000更改为8001、8002等。

假设我们要创建CurrencyExchangeServiceApplication的两个实例。为此, 我们必须在外部设置端口。

让我们创建一个在端口8001上运行的CurrencyExchangeServiceApplication实例。

步骤1:右键点击项目->运行方式->运行配置。

或单击突出显示的符号->运行配置。

货币换算及货币兑换服务介绍

步骤2:将CurrencyExchangeServiceAppication重命名为CurrencyExchangeServiceAppication8000, 然后单击Apply(应用)按钮。

货币换算及货币兑换服务介绍

步骤3:右键单击CurrencyExchangeServiceApplication8000->复制。

货币换算及货币兑换服务介绍

它生成CurrencyExchangeServiceApplication8000的重复文件。我们将在端口8001上运行它。

步骤4:单击Arguments(参数)选项卡, 然后在VM参数文本框中输入–Dserver.port = 8001。分别单击”应用”和”运行”按钮。

注意:无论我们在VM参数中传递什么值, 它都会覆盖application.properties文件的配置。

货币换算及货币兑换服务介绍

单击”运行”按钮后, 它将开始在端口8001上运行。

步骤5:在URL http:// localhost:8001 / currency-exchange / from / USD / to / INR中更改端口号, 然后按Enter键。我们得到以下回应:

{
id: 1000, from: "USD", to: "INR", conversionMultiple: 65, port: 8001
}

现在, 我们有两个CurrencyExchangeServiceApplication实例, 它们在两个不同的端口8000和8001上运行。

点击这里下载货币兑换服务


赞(0)
未经允许不得转载:srcmini » Spring Cloud货币换算及货币兑换服务介绍

评论 抢沙发

评论前必须登录!