前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何使用Java Spring Boot 创建一个微服务项目 二?

如何使用Java Spring Boot 创建一个微服务项目 二?

作者头像
用户1418987
发布2023-10-26 14:12:03
1560
发布2023-10-26 14:12:03
举报
文章被收录于专栏:coder

如何使用Java Spring Boot 创建一个微服务项目 二?

上一篇我们已经链接了 如何使用Java Spring Boot 创建一个微服务项目 一?这一篇我们接着实现第二部分

微服务2:货币兑换样本服务

如何使用Java Spring Boot 创建一个微服务项目 二?_java
如何使用Java Spring Boot 创建一个微服务项目 二?_java

这也是一个maven项目

pom.xml

代码语言:javascript
复制
<?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 
							https://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.2.1.RELEASE</version>
		<relativePath/> 
	</parent>
	<groupId>com.gfg.microservices</groupId>
	<artifactId>currency-conversion-sample-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>currency-conversion-servicce</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RC2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>

主要重要的java文件

CurrencyConversionSampleServiceApplication.java

代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

CurrencyConversionSampleBean.java

代码语言:javascript
复制
import java.math.BigDecimal;

public class CurrencyConversionSampleBean {
	// We need to set all the fields that is going to
	// received in response
	private Long id;
	private String from;
	private String to;
	private BigDecimal ConversionMultiple;
	private BigDecimal quantity;
	private BigDecimal totalCalculatedAmount;
	private int port;

	// default constructor
	public CurrencyConversionSampleBean() {}

	// creating constructor
	public CurrencyConversionSampleBean(
		Long id, String from, String to,
		BigDecimal conversionMultiple, BigDecimal quantity,
		BigDecimal totalCalculatedAmount, int port)
	{
		super();
		this.id = id;
		this.from = from;
		this.to = to;
		ConversionMultiple = conversionMultiple;
		this.quantity = quantity;
		this.totalCalculatedAmount = totalCalculatedAmount;
		this.port = port;
	}

	// creating setters and getters
	public Long getId() { return id; }

	public void setId(Long id) { this.id = id; }

	public String getFrom() { return from; }

	public void setFrom(String from) { this.from = from; }

	public String getTo() { return to; }

	public void setTo(String to) { this.to = to; }

	public BigDecimal getConversionMultiple()
	{
		return ConversionMultiple;
	}

	public void
	setConversionMultiple(BigDecimal conversionMultiple)
	{
		ConversionMultiple = conversionMultiple;
	}

	public BigDecimal getQuantity() { return quantity; }

	public void setQuantity(BigDecimal quantity)
	{
		this.quantity = quantity;
	}

	public BigDecimal getTotalCalculatedAmount()
	{
		return totalCalculatedAmount;
	}

	public void setTotalCalculatedAmount(
		BigDecimal totalCalculatedAmount)
	{
		this.totalCalculatedAmount = totalCalculatedAmount;
	}

	public int getPort() { return port; }

	public void setPort(int port) { this.port = port; }
}

CurrencyConversionSampleController.java

代码语言:javascript
复制
package com.gfg.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 CurrencyConversionSampleController {
	@GetMapping(
		"/currency-converter-sample/fromCurrency/{fromCurrency}/toCurrency/{toCurrency}/quantity/{quantity}")
	
	public CurrencyConversionSampleBean
	convertCurrency(@PathVariable String fromCurrency,
					@PathVariable String toCurrency,
					@PathVariable BigDecimal quantity)
	{

		// setting variables to currency exchange service
		Map<String, String> uriVariables = new HashMap<>();
		// urlParams should match properly
		uriVariables.put("fromCurrency", fromCurrency);
		uriVariables.put("toCurrency", toCurrency);
		// 调用货币兑换示例服务
// http://localhost:8000/currency-exchange-sample/fromCurrency/{fromCurrency}/toCurrency/{toCurrency}
// 是从第一部分调用的服务。它的响应已经接收,并通过 CurrencyConversionSampleBean 我们获取了结果回来
		ResponseEntity<CurrencyConversionSampleBean>
			responseEntity
			= new RestTemplate().getForEntity(
				"http://localhost:8000/currency-exchange-sample/fromCurrency/{fromCurrency}/toCurrency/{toCurrency}",
				CurrencyConversionSampleBean.class,
				uriVariables);
		CurrencyConversionSampleBean response
			= responseEntity.getBody();
		// 创建一个新的响应Bean并获取响应,将其放入Bean中。因此,输出Bean应包含从响应接收到的所有字段。
		return new CurrencyConversionSampleBean(
			response.getId(), fromCurrency, toCurrency,
			response.getConversionMultiple(), quantity,
			quantity.multiply(
				response.getConversionMultiple()),
			response.getPort());
	}
}

也就是说这个项目是在端口8100上启动的。现在我们可以执行以下URLS

代码语言:javascript
复制
http://localhost:8100/currency-converter-sample/fromCurrency/USD/toCurrency/INR/quantity/1000
如何使用Java Spring Boot 创建一个微服务项目 二?_spring_02
如何使用Java Spring Boot 创建一个微服务项目 二?_spring_02

当这个服务被调用时,它会依次调用。

假设:

currency-exchange-sample 正在端口 8000 中运行,并产生所需的响应

代码语言:javascript
复制
http://localhost:8000/currency-exchange-sample/fromCurrency/USD/toCurrency/INR

然后逻辑写成

代码语言:javascript
复制
ResponseEntity<CurrencyConversionSampleBean>respnotallow=new RestTemplate().getForEntity("http://localhost:8000/currency-exchange-sample/fromCurrency/{fromCurrency}/toCurrency/{toCurrency}",CurrencyConversionSampleBean.class, uriVariables);
        CurrencyConversionSampleBean response=responseEntity.getBody();  
        // 创建一个新的响应 bean 并获取响应并将其放入 Bean 中
        // 因此输出 bean 应该具有从响应接收到的所有字段
        返回新的CurrencyConversionSampleBean(response.getId(),fromCurrency,toCurrency,response.getConversionMultiple(),数量,quantity.multiply(response.getConversionMultiple()),response.getPort());

我们看到totalCalculatedAmount 为78 * 1000,即78000。我们从第一个URL 中获取“conversionMultiple”,并将其与此处的数量值相乘。非常理想的是,我们不需要将交换服务逻辑引入到该应用程序中,即第 1 部分项目可以分开,第 2 部分项目可以在此处调用第 1 部分 URL。因此微服务可以单独运行,其他服务可以使用它们。其余可检查的网址

例子1

代码语言:javascript
复制
http://localhost:8100/currency-converter-sample/fromCurrency/EUR/toCurrency/INR/quantity/5000

这会依次调用

代码语言:javascript
复制
http://localhost:8000/currency-exchange-sample/fromCurrency/EUR/toCurrency/INR

输出

如何使用Java Spring Boot 创建一个微服务项目 二?_java_03
如何使用Java Spring Boot 创建一个微服务项目 二?_java_03

例子2

代码语言:javascript
复制
http://localhost:8100/currency-converter-sample/fromCurrency/AUD/toCurrency/INR/quantity/2000

这会依次调用

代码语言:javascript
复制
http://localhost:8000/currency-exchange-sample/fromCurrency/AUD/toCurrency/INR

输出

如何使用Java Spring Boot 创建一个微服务项目 二?_ci_04
如何使用Java Spring Boot 创建一个微服务项目 二?_ci_04
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何使用Java Spring Boot 创建一个微服务项目 二?
    • 微服务2:货币兑换样本服务
      • pom.xml
        • CurrencyConversionSampleServiceApplication.java
          • CurrencyConversionSampleBean.java
          • CurrencyConversionSampleController.java
            • 假设:
              • 例子1
                • 输出
                  • 例子2
                    • 输出
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档