有道无术,术尚可求,有术无道,止于术。
本系列Seata 版本 2.0.0
本系列Spring Boot 版本 3.2.0
本系列Spring Cloud 版本 2023.0.0
源码地址:https://gitee.com/pearl-organization/study-seata-demo
文章目录
- 1. 概述
- 2. 数据库
- 3. 项目搭建
- 3.1 引入依赖
- 3.2 配置
- 3.3 业务代码
- 3.4 测试
1. 概述
接下来我们使用Spring Cloud Alibaba
微服务框架,搭建一个测试项目,模拟电商用户下单操作:
2. 数据库
使用的是Mysql 8.0.29
,首先创建三个数据库:
seata_account
:账户seata_order
:订单seata_stock
:库存
seata_account
库插入t_account
表:
CREATE TABLE `t_account` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`amount` double(14,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO t_account
(user_id, amount)
VALUES(1, 10000.00);
seata_order
库插入t_order
表:
CREATE TABLE `t_order` (
`id` int NOT NULL AUTO_INCREMENT,
`order_no` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`user_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`commodity_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`count` int DEFAULT '0',
`amount` double(14,2) DEFAULT '0.00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
seata_stock
库插入t_stock
表:
CREATE TABLE `t_stock` (
`id` int NOT NULL AUTO_INCREMENT,
`commodity_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`count` int DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `commodity_code` (`commodity_code`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO t_stock
(commodity_code, name, count)
VALUES('IPHONE', '苹果手机', 10000);
3. 项目搭建
项目技术栈:
JDK 17
(自行安装)Maven 3.6.3
Mybatis Plus 3.5.5
Nacos 2.0
(自行安装)Spring Boot 3.2.0
Spring Cloud 2023.0.0
Spring Cloud Alibaba 2023.0.0.0-RC1
使用Spring Initializr
创建一个多模块微服务项目:
各个模块介绍:
spring-cloud-demo
:父项目common
:公共模块business
:业务服务stock
:库存服务order
:订单服务account
:账户服务
3.1 引入依赖
spring-cloud-demo
作为父项目,引入依赖管理:
<?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>
<groupId>com.pearl</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-demo</name>
<description>spring-cloud-demo</description>
<packaging>pom</packaging>
<properties>
<java.version>17</java.version>
<skipTests>true</skipTests>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<modules>
<module>account</module>
<module>business</module>
<module>order</module>
<module>stock</module>
<module>common</module>
</modules>
<!--依赖版本管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2023.0.0.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Spring Boot
和Spring Cloud
的对应关系可以参考官网:
Spring Cloud Alibaba
最新的预发布版本2023.0.0.0-RC1
才开始支持Spring Boot 3.2
、Spring Cloud 2023.0.0
,因为是学习所以采用最新版本,其他版本兼容性可参考官网。
common
模块引入业务模块需要的公共依赖:
<!--公共依赖-->
<dependencies>
<!--Spring Boot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.21</version>
</dependency>
<!--Mybatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!--Spring Cloud-->
<!-- Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--Loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--Open Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
各个业务模块引入common
:
<dependency>
<groupId>com.pearl</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
3.2 配置
各个业务模块在application.yml
中添加端口号、服务名称、Nacos
注册地址、数据源:
server:
port: 8503
spring:
application:
name: stock
cloud:
nacos:
discovery:
namespace: public
server-addr: 127.0.0.1:8848
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata_stock?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true
username: root
password: 123456
3.3 业务代码
在项目中添加电商用户下单业务代码,由于这些代码很简单,直接参考案例源码即可。
3.4 测试
启动Nacos
、各个业务后台,查看Nacos
控制台:
访问下单接口http://localhost:8080/business/buy
,检查程序是否报错,各个业务表的数据是否正确。