Spring Cloud Day2 消费者订单模块构建和工程重构

本文记录了Spring Cloud微服务架构的学习过程,包括热部署Devtools的配置,消费者订单模块的创建步骤,如创建Maven项目、编写YML、启动类和业务类。同时介绍了RestTemplate在访问REST服务中的应用。最后,文章详细阐述了如何进行工程重构,将支付和订单模块中的共性部分提取到新的cloud-api-commons模块,并完成打包发布。

Spring Cloud 微服务分布式架构学习

记录一下自己学习Spring Cloud的过程~


热部署Devtools

1、Adding devtools to your project
在cloud-provider-payment8001模块的pom文件中增加devtools依赖

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <scope>runtime</scope>
     <optional>true</optional>
</dependency>

2、Adding plugin to your pom.xml
在父工程的pom文件中添加

<build>
    <!--<finalName>你的工程名</finalName>(单一工程时添加)-->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

3、Enabling automatic build在这里插入图片描述
4、Update the value of
快捷键:Shift + Option + Command + /
在这里插入图片描述
5、重新启动idea


消费者订单模块创建(上)

创建为订单模块步骤:
1、建Module
2、改POM
3、写YML
4、主启动
5、业务类
Step1: 创建模块cloud- consumer-order80的Maven项目
Step2: 改POM
修改模块cloud- consumer-order80的pom文件
与上篇中的支付模块pom文件类似,此处不在说明。
Step3: 写YML

server:
	port: 80

Step4: 主启动类

package com.demo.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @author by wanyun
 * @Date: 2022/08/21/12:04 下午
 */
@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}

Step5: 业务类
客户端只需要controller和entities实体就可以了,不需要有dao、services等
订单模块的entities中的主实体Payment和Json封装体CommonResult与支付模块中的一致,在此也不再进行说明。
在controller中订单模块如何调用支付模块?
RestTemplate提供了多种便捷访问远程HTTP服务的方法,是一种便捷的访问restful服务模版类,是Spring提供用于访问Rest服务的客户端模版工具类。
在订单模块中添加ApplicationContextConfig

package com.demo.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
 * @author by wanyun
 * @Date: 2022/08/21/12:59 下午
 */
@Configuration
public class ApplicationContextConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

OrderController.java

package com.demo.springcloud.controller;
import com.demo.springcloud.entities.CommonResult;
import com.demo.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
 * @author by wanyun
 * @Date: 2022/08/21/12:11 下午
 */
@RestController
@Slf4j
public class OrderController {
    public static final String PAYMENT_URL = "http://localhost:8001";
    @Resource
    private RestTemplate restTemplate;
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }
    @PostMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

Step6: 分别启动80端口和8001端口
运行窗口显示
在这里插入图片描述
IDEA老版本中在DashBoard中运行,新版本在Services中运行
客户端80接口直接查询URL:http://localhost:80/consumer/payment/get/31
客户端80接口直接新增URL:http://localhost:80/consumer/payment/create?serial='wanyun2'
其中端口号80可以省略。

在测试时别忘了在8001端口的controller中添加注解@RequestBody,否则新增属性字段为空。
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。引用@RequestBody的使用

工程重构

将支付模块和订单模块中的重复部分提取出来,新建cloud-api-commons模块,进行项目重构。
1、在cloud-api-commons模块中新建包entities,将订单和支付模块中实体Payment和通用封装类CommonResult移到共用模块中。
在这里插入图片描述

2、使用maven命令( clean install ) 将共用模块打包发布上传到本地共用库中提供给支付和订单模块调用。
在这里插入图片描述
3、重新修改80和8001
a:删除之前的entities. 其中groupId与共用模块中的groupid要一致
b:pom导入共用模块依赖

<dependency>
     <groupId>org.example</groupId>
     <artifactId>cloud-api-commons</artifactId>
     <version>${project.version}</version>
</dependency>

项目总架构如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值