文章目录
搭建模块步骤:
- 在Project中创建Module
- 修改pom.xml
- 写application.yaml
- 编写主启动类
- 编写各层业务类
1、父项目搭建
1、项目依赖
创建Project主要是为了便于模块的统一管理,包括子模块的依赖版本控制等
Maven项目可以通过dependencyManagement
对整个项目中的jar包进行控制
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>indi.zhihuali</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!--统一管理jar包和版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>8.0.18</mysql.version>
<druid.verison>1.1.16</druid.verison>
<mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison>
</properties>
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.verison}</version>
</dependency>
<!-- mybatis-springboot整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.verison}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、dependencyManagement
dependencyManagement和dependencies的区别:
- dependencyManagement一般用于父工程,子模块中就不需要再指定版本和group了
- 若多个子模块都引用同一依赖,可以对子模块进行统一管理,而不需要逐个修改版本了
- dependencyManagement只是声明依赖,而不引入依赖
- 子项目需要显式声明需要使用的依赖,否则并不能直接使用父类中声明的依赖
- 只有子项目显式声明了需要使用的依赖且并没有指定具体版本时,才会从父项目中继承依赖
- version和scope均取自父项目
2、子模块搭建
1、支付模块
1、pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>indi.zhihuali</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider-payment-8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
2、编写主配置文件
server:
port: 8001
spring:
application:
name: payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 1234
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: indi.zhihuali.pojo
3、编写主启动类
package indi.zhihuali;
import org.apache.ibatis.annotations.Param;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ program: spring-cloud
* @ description:
* @ author: zhihua li
* @ create: 2021-09-01 18:43
**/
@SpringBootApplication
public class PaymentMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class, args);
}
}
4、编写各层业务类
- 本步骤中主要涉及到pojo、mapper、service、controller及响应mapper配置文件的编写,大致与SSM项目相同
- 特殊之处在于前后端分离的思想:比如在后端进行插入或查询操作中,不需要将具体的数据返回给前端,而是返回一个封装好的json串,因此只需要对返回的结果进行封装即可,包括状态码、消息、数据等信息
5、前后端分离思想示例
pojo.CommonResult.java
package indi.zhihuali.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ program: spring-cloud
* @ description:
* @ author: zhihua li
* @ create: 2021-09-01 18:57
**/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
private Integer code; //响应状态码
private String message; //响应消息
private T data; //响应数据
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
}
controller.PaymentController.java
package indi.zhihuali.controller;
import indi.zhihuali.pojo.CommonResult;
import indi.zhihuali.pojo.Payment;
import indi.zhihuali.service.PayMentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @ program: spring-cloud
* @ description:
* @ author: zhihua li
* @ create: 2021-09-01 19:19
**/
@Slf4j
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Autowired
private PayMentService payMentService;
@PostMapping("/create")
public CommonResult create(Payment payment) {
int result = payMentService.create(payment);
log.info("----" + result);
return result > 0 ? new CommonResult(200, "插入数据成功", result) :
new CommonResult(444, "插入数据失败");
}
@GetMapping("/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
Payment result = payMentService.getPaymentById(id);
log.info("----" + result);
return result != null ? new CommonResult(200, "查询成功!", result) :
new CommonResult(444, "查询id为:" + id + "的数据失败");
}
}
如上:返回时只需要返回json而不是真实数据,前后端分离
2、订单模块
1、pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>indi.zhihuali</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer-order-80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、编写主配置文件
server:
port: 80
3、主启动类同上
4、业务类
- 由于订单模块主要调用支付模块的业务,所以需要复制其实体类进行响应操作
- 当需要跨进程调用接口时(如本项目中订单模块调用支付模块的操作),需要通过
RestTemplate
来实现
5、RestTemplate
1、向Spring容器中注入RestTemplate
package indi.zhihuali.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @ program: spring-cloud
* @ description:
* @ author: zhihua li
* @ create: 2021-09-02 09:52
**/
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
2、使用RestTemplate
package indi.zhihuali.controller;
import indi.zhihuali.pojo.CommonResult;
import indi.zhihuali.pojo.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @ program: spring-cloud
* @ description:
* @ author: zhihua li
* @ create: 2021-09-02 09:50
**/
@RestController
@Slf4j
@RequestMapping("/consumer/payment")
public class OrderController {
// 指定跨进程访问的路径,也就是上面编写的支付模块的请求url
private final static String PAYMENT_URL = "http://localhost:8001/payment";
@Autowired
// 注入RestTemplate
private RestTemplate restTemplate;
@GetMapping("/create")
public CommonResult<Payment> create(Payment payment) {
// 通过调用postForObject方法,利用post方法进行创建(有坑)
/* public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
URL:请求路径
request:请求参数,POST请求通常携带参数完成写操作
responseType:响应类型即返回类型
*/
return restTemplate.postForObject(PAYMENT_URL + "/create", payment, CommonResult.class);
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL + "/get/" + id, CommonResult.class);
}
}
3、使用RestTemplate小坑
当跨进程请求时,需要给被调用的方法参数内添加**@RequestBody**注解,否则接收无法接收到改参数的值
- @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
- GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交
- 在后端的同一个接收方法里,@RequestBody 与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个
- 注:一个请求,只有一个RequestBody;一个请求,可以有多个RequestParam
由于在支付模块中create()
为Post方式,而直接在URL中输入请求路径只能发送Get请求
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
// URL:请求路径
// request:请求参数,POST请求通常携带参数完成写操作
// responseType:响应类型即返回类型
订单模块中该方法的请求方式为Get,可以从上面的方法解读中看出,下图中的payment是直接被封装到请求体中,因此可以调用支付模块中的Post请求
3、抽取公共模块
上面两个模块中,可以发现业务类中pojo具有重复情况,且pojo包应该在整个项目中都是公共的,因此想到抽离出来作为新的模块
1、pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>indi.zhihuali</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-commons</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--糊涂工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
2、将pojo包转进公共模块api-common中
后续编码过程中所有的公共部分都加在api-common模块中
3、使用Maven打包
不需要其他的代码,直接通过Maven中的clean、install来打jar包即可
4、在其他两模块中引入该jar包
<dependency>
<groupId>indi.zhihuali</groupId>
<artifactId>api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>