3.2.0-beta,重构很大:升级springboot2.6.6、spring-cloud-alibaba 2021.1、mybatisplus3.5.1、代码规范部分重构

This commit is contained in:
zhangdaiscott
2022-04-18 09:37:28 +08:00
parent d0b68919b3
commit 69ecb39c9e
487 changed files with 9754 additions and 2928 deletions

View File

@ -0,0 +1,14 @@
<?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>jeecg-cloud-test-seata</artifactId>
<groupId>org.jeecgframework.boot</groupId>
<version>3.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>分布式事务测试模块</description>
<artifactId>jeecg-cloud-test-seata-account</artifactId>
</project>

View File

@ -0,0 +1,17 @@
package org.jeecg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 分布式事务-账户服务
* @author zyf
*/
@SpringBootApplication
public class SeataAccountApplication {
public static void main(String[] args) {
SpringApplication.run(SeataAccountApplication.class, args);
}
}

View File

@ -0,0 +1,26 @@
package org.jeecg.modules.test.seata.account.controller;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
/**
* @author zyf
*/
@RestController
@RequestMapping("/test/seata/account")
public class SeataAccountController {
@Autowired
private SeataAccountService accountService;
@PostMapping("/reduceBalance")
public void reduceBalance(Long userId, BigDecimal amount) {
accountService.reduceBalance(userId, amount);
}
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.test.seata.entity;
package org.jeecg.modules.test.seata.account.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
@ -24,7 +25,7 @@ public class SeataAccount {
/**
* 余额
*/
private Double balance;
private BigDecimal balance;
private Date lastUpdateTime;
}

View File

@ -1,8 +1,9 @@
package org.jeecg.modules.test.seata.mapper;
package org.jeecg.modules.test.seata.account.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.jeecg.modules.test.seata.entity.SeataAccount;
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
/**
* @Description: TODO

View File

@ -1,4 +1,6 @@
package org.jeecg.modules.test.seata.service;
package org.jeecg.modules.test.seata.account.service;
import java.math.BigDecimal;
/**
* @Description: 账户接口
@ -9,7 +11,7 @@ package org.jeecg.modules.test.seata.service;
public interface SeataAccountService {
/**
* @param userId 用户 ID
* @param price 扣减金额
* @param amount 扣减金额
*/
void reduceBalance(Long userId, Double price);
void reduceBalance(Long userId, BigDecimal amount);
}

View File

@ -1,17 +1,19 @@
package org.jeecg.modules.test.seata.service.impl;
package org.jeecg.modules.test.seata.account.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.entity.SeataAccount;
import org.jeecg.modules.test.seata.mapper.SeataAccountMapper;
import org.jeecg.modules.test.seata.service.SeataAccountService;
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
import org.jeecg.modules.test.seata.account.mapper.SeataAccountMapper;
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.math.BigDecimal;
/**
* @Description: TODO
@ -31,19 +33,19 @@ public class SeataAccountServiceImpl implements SeataAccountService {
@DS("account")
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void reduceBalance(Long userId, Double price) {
public void reduceBalance(Long userId, BigDecimal amount) {
log.info("=============ACCOUNT START=================");
SeataAccount account = accountMapper.selectById(userId);
Assert.notNull(account, "用户不存在");
Double balance = account.getBalance();
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, price);
BigDecimal balance = account.getBalance();
log.info("下单用户{}余额为 {},商品总价为{}", userId, balance, amount);
if (balance < price) {
if (balance.compareTo(amount)==-1) {
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
throw new RuntimeException("余额不足");
}
log.info("开始扣减用户 {} 余额", userId);
double currentBalance = account.getBalance() - price;
BigDecimal currentBalance = account.getBalance().subtract(amount);
account.setBalance(currentBalance);
accountMapper.updateById(account);
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);

View File

@ -0,0 +1,26 @@
server:
port: 5002
spring:
application:
name: seata-account
datasource:
dynamic:
seata: true # 开启对 seata的支持
seata-mode: AT #支持XA及AT模式,默认AT
datasource:
# 设置 账号数据源配置
account:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3300/jeecg-account?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-account.sql
seata:
enable-auto-data-source-proxy: false
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
springboot-seata-group: default
# seata 事务组编号 用于TC集群名
tx-service-group: springboot-seata-group

View File

@ -0,0 +1,37 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`balance` decimal(10, 2) NULL DEFAULT NULL,
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES (1, 50.00, '2022-03-16 17:02:53');
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -0,0 +1,14 @@
<?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>jeecg-cloud-test-seata</artifactId>
<groupId>org.jeecgframework.boot</groupId>
<version>3.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>分布式事务测试模块</description>
<artifactId>jeecg-cloud-test-seata-order</artifactId>
</project>

View File

@ -0,0 +1,18 @@
package org.jeecg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zyf
*/
@SpringBootApplication
@EnableFeignClients
public class SeataOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SeataOrderApplication.class, args);
}
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.test.seata.controller;
package org.jeecg.modules.test.seata.order.controller;
/**
* @Description: TODO
@ -8,8 +8,9 @@ package org.jeecg.modules.test.seata.controller;
*/
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.service.SeataOrderService;
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
@ -18,7 +19,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
@RequestMapping("/test/seata/order")
@Api(tags = "seata测试")
public class SeataOrderController {

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.test.seata.dto;
package org.jeecg.modules.test.seata.order.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@ -24,5 +24,5 @@ public class PlaceOrderRequest {
private Long productId;
@NotNull
private Integer amount;
private Integer count;
}

View File

@ -1,11 +1,13 @@
package org.jeecg.modules.test.seata.entity;
package org.jeecg.modules.test.seata.order.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import org.jeecg.modules.test.seata.enums.OrderStatus;
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
import java.math.BigDecimal;
/**
* @Description: 订单
@ -36,9 +38,9 @@ public class SeataOrder {
/**
* 数量
*/
private Integer amount;
private Integer count;
/**
* 总金额
*/
private Double totalPrice;
private BigDecimal totalPrice;
}

View File

@ -0,0 +1,23 @@
package org.jeecg.modules.test.seata.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.math.BigDecimal;
/**
* @author zyf
*/
@FeignClient(value ="seata-account")
public interface AccountClient {
/**
* 扣减余额
* @param userId
* @param amount
* @return
*/
@PostMapping("/test/seata/account/reduceBalance")
String reduceBalance(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount);
}

View File

@ -0,0 +1,20 @@
package org.jeecg.modules.test.seata.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.math.BigDecimal;
@FeignClient(value ="seata-product")
public interface ProductClient {
/**
* 扣减库存
*
* @param productId
* @param count
* @return
*/
@PostMapping("/test/seata/product/reduceStock")
BigDecimal reduceStock(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.test.seata.mapper;
package org.jeecg.modules.test.seata.order.mapper;
/**
* @Description: TODO
@ -9,7 +9,7 @@ package org.jeecg.modules.test.seata.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.jeecg.modules.test.seata.entity.SeataOrder;
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
@Mapper
public interface SeataOrderMapper extends BaseMapper<SeataOrder> {

View File

@ -1,6 +1,7 @@
package org.jeecg.modules.test.seata.service;
package org.jeecg.modules.test.seata.order.service;
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
/**
* @Description: 订单接口

View File

@ -1,20 +1,22 @@
package org.jeecg.modules.test.seata.service.impl;
package org.jeecg.modules.test.seata.order.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.entity.SeataOrder;
import org.jeecg.modules.test.seata.enums.OrderStatus;
import org.jeecg.modules.test.seata.mapper.SeataOrderMapper;
import org.jeecg.modules.test.seata.service.SeataAccountService;
import org.jeecg.modules.test.seata.service.SeataOrderService;
import org.jeecg.modules.test.seata.service.SeataProductService;
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
import org.jeecg.modules.test.seata.order.feign.AccountClient;
import org.jeecg.modules.test.seata.order.feign.ProductClient;
import org.jeecg.modules.test.seata.order.mapper.SeataOrderMapper;
import org.jeecg.modules.test.seata.order.service.SeataOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
/**
* @Description: 订单服务类
* @author: zyf
@ -27,10 +29,10 @@ public class SeataOrderServiceImpl implements SeataOrderService {
@Resource
private SeataOrderMapper orderMapper;
@Autowired
private SeataAccountService accountService;
@Autowired
private SeataProductService productService;
@Resource
private AccountClient accountClient;
@Resource
private ProductClient productClient;
@DS("order")
@Override
@ -40,26 +42,26 @@ public class SeataOrderServiceImpl implements SeataOrderService {
log.info("=============ORDER START=================");
Long userId = request.getUserId();
Long productId = request.getProductId();
Integer amount = request.getAmount();
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, amount);
Integer count = request.getCount();
log.info("收到下单请求,用户:{}, 商品:{},数量:{}", userId, productId, count);
SeataOrder order = SeataOrder.builder()
.userId(userId)
.productId(productId)
.status(OrderStatus.INIT)
.amount(amount)
.count(count)
.build();
orderMapper.insert(order);
log.info("订单一阶段生成,等待扣库存付款中");
// 扣减库存并计算总价
Double totalPrice = productService.reduceStock(productId, amount);
BigDecimal amount = productClient.reduceStock(productId, count);
// 扣减余额
accountService.reduceBalance(userId, totalPrice);
accountClient.reduceBalance(userId, amount);
order.setStatus(OrderStatus.SUCCESS);
order.setTotalPrice(totalPrice);
order.setTotalPrice(amount);
orderMapper.updateById(order);
log.info("订单已成功下单");
log.info("=============ORDER END=================");

View File

@ -0,0 +1,26 @@
server:
port: 5001
spring:
application:
name: seata-order
datasource:
dynamic:
seata: true # 开启对 seata的支持
seata-mode: AT #支持XA及AT模式,默认AT
datasource:
# 设置 账号数据源配置
order:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3300/jeecg-order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-order.sql
seata:
enable-auto-data-source-proxy: false
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
springboot-seata-group: default
# seata 事务组编号 用于TC集群名
tx-service-group: springboot-seata-group

View File

@ -0,0 +1,37 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for p_order
-- ----------------------------
DROP TABLE IF EXISTS `p_order`;
CREATE TABLE `p_order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NULL DEFAULT NULL,
`product_id` int(11) NULL DEFAULT NULL,
`count` int(11) NULL DEFAULT NULL,
`total_price` decimal(10, 2) NULL DEFAULT NULL,
`status` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`add_time` timestamp NULL DEFAULT current_timestamp(),
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -0,0 +1,14 @@
<?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>jeecg-cloud-test-seata</artifactId>
<groupId>org.jeecgframework.boot</groupId>
<version>3.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>分布式事务测试模块</description>
<artifactId>jeecg-cloud-test-seata-product</artifactId>
</project>

View File

@ -0,0 +1,16 @@
package org.jeecg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zyf
*/
@SpringBootApplication
public class SeataProductApplication {
public static void main(String[] args) {
SpringApplication.run(SeataProductApplication.class, args);
}
}

View File

@ -0,0 +1,25 @@
package org.jeecg.modules.test.seata.product.controller;
import org.jeecg.modules.test.seata.product.service.SeataProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
/**
* @author zyf
*/
@RestController
@RequestMapping("/test/seata/product")
public class SeataProductController {
@Autowired
private SeataProductService seataProductService;
@PostMapping("/reduceStock")
public BigDecimal reduceStock(Long productId, Integer count) {
return seataProductService.reduceStock(productId, count);
}
}

View File

@ -1,4 +1,4 @@
package org.jeecg.modules.test.seata.entity;
package org.jeecg.modules.test.seata.product.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
/**
* @Description: 产品
@ -23,7 +24,7 @@ public class SeataProduct {
/**
* 价格
*/
private Double price;
private BigDecimal price;
/**
* 库存
*/

View File

@ -1,7 +1,8 @@
package org.jeecg.modules.test.seata.mapper;
package org.jeecg.modules.test.seata.product.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.jeecg.modules.test.seata.entity.SeataProduct;
import org.jeecg.modules.test.seata.product.entity.SeataProduct;
/**
* @Description: TODO

View File

@ -1,4 +1,6 @@
package org.jeecg.modules.test.seata.service;
package org.jeecg.modules.test.seata.product.service;
import java.math.BigDecimal;
/**
* @Description: 产品接口
@ -11,8 +13,8 @@ public interface SeataProductService {
* 扣减库存
*
* @param productId 商品 ID
* @param amount 扣减数量
* @param count 扣减数量
* @return 商品总价
*/
Double reduceStock(Long productId, Integer amount);
BigDecimal reduceStock(Long productId, Integer count);
}

View File

@ -1,17 +1,19 @@
package org.jeecg.modules.test.seata.service.impl;
package org.jeecg.modules.test.seata.product.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.test.seata.entity.SeataProduct;
import org.jeecg.modules.test.seata.mapper.SeataProductMapper;
import org.jeecg.modules.test.seata.service.SeataProductService;
import org.jeecg.modules.test.seata.product.entity.SeataProduct;
import org.jeecg.modules.test.seata.product.mapper.SeataProductMapper;
import org.jeecg.modules.test.seata.product.service.SeataProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.math.BigDecimal;
/**
* @Description: 产品服务类
@ -32,25 +34,25 @@ public class SeataProductServiceImpl implements SeataProductService {
@DS("product")
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Double reduceStock(Long productId, Integer amount) {
public BigDecimal reduceStock(Long productId, Integer count) {
log.info("=============PRODUCT START=================");
// 检查库存
SeataProduct product = productMapper.selectById(productId);
Assert.notNull(product, "商品不存在");
Integer stock = product.getStock();
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, amount);
log.info("商品编号为 {} 的库存为{},订单商品数量为{}", productId, stock, count);
if (stock < amount) {
if (stock < count) {
log.warn("商品编号为{} 库存不足,当前库存:{}", productId, stock);
throw new RuntimeException("库存不足");
}
log.info("开始扣减商品编号为 {} 库存,单价商品价格为{}", productId, product.getPrice());
// 扣减库存
int currentStock = stock - amount;
int currentStock = stock - count;
product.setStock(currentStock);
productMapper.updateById(product);
double totalPrice = product.getPrice() * amount;
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, amount, totalPrice);
BigDecimal totalPrice = product.getPrice().multiply(new BigDecimal(count));
log.info("扣减商品编号为 {} 库存成功,扣减后库存为{}, {} 件商品总价为 {} ", productId, currentStock, count, totalPrice);
log.info("=============PRODUCT END=================");
return totalPrice;
}

View File

@ -0,0 +1,26 @@
server:
port: 5003
spring:
application:
name: seata-product
datasource:
dynamic:
seata: true # 开启对 seata的支持
seata-mode: AT #支持XA及AT模式,默认AT
datasource:
# 设置 账号数据源配置
product:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3300/jeecg-product?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-product.sql
seata:
enable-auto-data-source-proxy: false
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
springboot-seata-group: default
# seata 事务组编号 用于TC集群名
tx-service-group: springboot-seata-group

View File

@ -0,0 +1,38 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`price` decimal(10, 2) NULL DEFAULT NULL,
`stock` int(11) NULL DEFAULT NULL,
`last_update_time` timestamp NULL DEFAULT current_timestamp() ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, 10.00, 20, '2022-01-13 09:52:50');
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime(0) NOT NULL,
`log_modified` datetime(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

View File

@ -5,18 +5,27 @@
<parent>
<artifactId>jeecg-cloud-test</artifactId>
<groupId>org.jeecgframework.boot</groupId>
<version>3.1.0</version>
<version>3.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>分布式事务测试模块</description>
<artifactId>jeecg-cloud-test-seata</artifactId>
<packaging>pom</packaging>
<modules>
<module>jeecg-cloud-test-seata-account</module>
<module>jeecg-cloud-test-seata-product</module>
<module>jeecg-cloud-test-seata-order</module>
</modules>
<dependencies>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-starter-cloud</artifactId>
<version>${jeecgboot.version}</version>
</dependency>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-starter-seata</artifactId>
<version>3.1.0</version>
<version>${jeecgboot.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,38 +0,0 @@
seata:
enable-auto-data-source-proxy: false
service:
grouplist:
default: 127.0.0.1:8091
vgroup-mapping:
springboot-seata-group: default
# seata 事务组编号 用于TC集群名
tx-service-group: springboot-seata-group
transport:
heartbeat: false
spring:
datasource:
dynamic:
seata: true # 开启对 seata的支持
seata-mode: AT #支持XA及AT模式,默认AT
datasource:
# 设置 账号数据源配置
account:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jeecg-account?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-account.sql
# 设置 订单数据源配置
order:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jeecg-order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-order.sql
# 设置商品 数据源配置
product:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jeecg-product?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
username: root
password: root
schema: classpath:sql/schema-product.sql

View File

@ -1,29 +0,0 @@
DROP TABLE IF EXISTS account;
CREATE TABLE account
(
id INT(11) NOT NULL AUTO_INCREMENT,
balance DOUBLE DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
DROP TABLE IF EXISTS undo_log;
CREATE TABLE undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
INSERT INTO account (id, balance)
VALUES (1, 50);

View File

@ -1,32 +0,0 @@
DROP TABLE IF EXISTS p_order;
CREATE TABLE p_order
(
id INT(11) NOT NULL AUTO_INCREMENT,
user_id INT(11) DEFAULT NULL,
product_id INT(11) DEFAULT NULL,
amount INT(11) DEFAULT NULL,
total_price DOUBLE DEFAULT NULL,
status VARCHAR(100) DEFAULT NULL,
add_time DATETIME DEFAULT CURRENT_TIMESTAMP,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
DROP TABLE IF EXISTS undo_log;
CREATE TABLE undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;

View File

@ -1,31 +0,0 @@
DROP TABLE IF EXISTS product;
CREATE TABLE product
(
id INT(11) NOT NULL AUTO_INCREMENT,
price DOUBLE DEFAULT NULL,
stock INT(11) DEFAULT NULL,
last_update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
DROP TABLE IF EXISTS undo_log;
CREATE TABLE undo_log
(
id BIGINT(20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT(20) NOT NULL,
xid VARCHAR(100) NOT NULL,
context VARCHAR(128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11) NOT NULL,
log_created DATETIME NOT NULL,
log_modified DATETIME NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid, branch_id)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8;
INSERT INTO product (id, price, stock)
VALUES (1, 10, 20);

View File

@ -1,12 +0,0 @@
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=137 DEFAULT CHARSET=utf8;