Skip to content

Commit 05b9b5e

Browse files
Merge pull request #9556 from zolay-poi/main
fix(seata): 修复seata示例项目余额不足时没有正确回滚库存的问题 fix #9287
2 parents cf7eeac + 03ee061 commit 05b9b5e

File tree

12 files changed

+55
-25
lines changed

12 files changed

+55
-25
lines changed

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-account/src/main/java/org/jeecg/modules/test/seata/account/controller/SeataAccountController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jeecg.modules.test.seata.account.controller;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.jeecg.common.api.vo.Result;
45
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.web.bind.annotation.PostMapping;
@@ -20,7 +21,7 @@ public class SeataAccountController {
2021
private SeataAccountService accountService;
2122

2223
@PostMapping("/reduceBalance")
23-
public void reduceBalance(Long userId, BigDecimal amount) {
24-
accountService.reduceBalance(userId, amount);
24+
public Result<?> reduceBalance(Long userId, BigDecimal amount) {
25+
return accountService.reduceBalance(userId, amount);
2526
}
2627
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-account/src/main/java/org/jeecg/modules/test/seata/account/service/SeataAccountService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jeecg.modules.test.seata.account.service;
22

3+
import org.jeecg.common.api.vo.Result;
4+
35
import java.math.BigDecimal;
46

57
/**
@@ -14,5 +16,5 @@ public interface SeataAccountService {
1416
* @param userId 用户 ID
1517
* @param amount 扣减金额
1618
*/
17-
void reduceBalance(Long userId, BigDecimal amount);
19+
Result<?> reduceBalance(Long userId, BigDecimal amount);
1820
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-account/src/main/java/org/jeecg/modules/test/seata/account/service/impl/SeataAccountServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.seata.core.context.RootContext;
66
import lombok.extern.slf4j.Slf4j;
77

8+
import org.jeecg.common.api.vo.Result;
89
import org.jeecg.modules.test.seata.account.entity.SeataAccount;
910
import org.jeecg.modules.test.seata.account.mapper.SeataAccountMapper;
1011
import org.jeecg.modules.test.seata.account.service.SeataAccountService;
@@ -34,7 +35,7 @@ public class SeataAccountServiceImpl implements SeataAccountService {
3435
@DS("account")
3536
@Override
3637
@Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
37-
public void reduceBalance(Long userId, BigDecimal amount) {
38+
public Result<?> reduceBalance(Long userId, BigDecimal amount) {
3839
log.info("xid:"+ RootContext.getXID());
3940
log.info("=============ACCOUNT START=================");
4041
SeataAccount account = accountMapper.selectById(userId);
@@ -44,13 +45,14 @@ public void reduceBalance(Long userId, BigDecimal amount) {
4445

4546
if (balance.compareTo(amount)==-1) {
4647
log.warn("用户 {} 余额不足,当前余额:{}", userId, balance);
47-
throw new RuntimeException("余额不足");
48+
return Result.error("余额不足");
4849
}
4950
log.info("开始扣减用户 {} 余额", userId);
5051
BigDecimal currentBalance = account.getBalance().subtract(amount);
5152
account.setBalance(currentBalance);
5253
accountMapper.updateById(account);
5354
log.info("扣减用户 {} 余额成功,扣减后用户账户余额为{}", userId, currentBalance);
5455
log.info("=============ACCOUNT END=================");
56+
return Result.OK();
5557
}
5658
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-account/src/main/resources/application.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ spring:
44
data:
55
redis:
66
##redis 单机环境配置
7-
host: localhost
7+
host: jeecg-boot-redis
88
port: 6379
99
database: 0
1010
password:
@@ -22,15 +22,15 @@ spring:
2222
autoconfigure:
2323
exclude: com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration
2424
datasource:
25-
url: jdbc:mysql://127.0.0.1:3306/jeecg_account?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
25+
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg_account?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
2626
username: root
2727
password: root
2828
driver-class-name: com.mysql.cj.jdbc.Driver
2929
sql:
3030
init:
3131
schema-locations: classpath:sql/schema-account.sql
3232
seata:
33-
enable-auto-data-source-proxy: false
33+
enable-auto-data-source-proxy: true
3434
service:
3535
grouplist:
3636
default: 127.0.0.1:8091

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-order/src/main/java/org/jeecg/modules/test/seata/order/feign/AccountClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jeecg.modules.test.seata.order.feign;
22

3+
import org.jeecg.common.api.vo.Result;
34
import org.springframework.cloud.openfeign.FeignClient;
45
import org.springframework.web.bind.annotation.PostMapping;
56
import org.springframework.web.bind.annotation.RequestParam;
@@ -19,5 +20,5 @@ public interface AccountClient {
1920
* @return
2021
*/
2122
@PostMapping("/test/seata/account/reduceBalance")
22-
String reduceBalance(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount);
23+
Result<?> reduceBalance(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount);
2324
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-order/src/main/java/org/jeecg/modules/test/seata/order/feign/ProductClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jeecg.modules.test.seata.order.feign;
22

3+
import org.jeecg.common.api.vo.Result;
34
import org.springframework.cloud.openfeign.FeignClient;
45
import org.springframework.web.bind.annotation.PostMapping;
56
import org.springframework.web.bind.annotation.RequestParam;
@@ -21,5 +22,5 @@ public interface ProductClient {
2122
* @return
2223
*/
2324
@PostMapping("/test/seata/product/reduceStock")
24-
BigDecimal reduceStock(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
25+
Result<BigDecimal> reduceStock(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
2526
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-order/src/main/java/org/jeecg/modules/test/seata/order/service/impl/SeataOrderServiceImpl.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import io.seata.core.context.RootContext;
66
import io.seata.spring.annotation.GlobalTransactional;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.jeecg.common.api.vo.Result;
9+
import org.jeecg.common.exception.JeecgBootBizTipException;
10+
import org.jeecg.common.util.oConvertUtils;
811
import org.jeecg.modules.test.seata.order.dto.PlaceOrderRequest;
912
import org.jeecg.modules.test.seata.order.entity.SeataOrder;
1013
import org.jeecg.modules.test.seata.order.enums.OrderStatus;
@@ -59,13 +62,20 @@ public void placeOrder(PlaceOrderRequest request) {
5962
orderMapper.insert(order);
6063
log.info("订单一阶段生成,等待扣库存付款中");
6164
// 扣减库存并计算总价
62-
BigDecimal amount = productClient.reduceStock(productId, count);
65+
Result<BigDecimal> productRes = productClient.reduceStock(productId, count);
66+
if (!productRes.isSuccess()) {
67+
String message = productRes.getMessage();
68+
message = oConvertUtils.isEmpty(message) ? "操作失败" : message;
69+
throw new JeecgBootBizTipException(message);
70+
}
71+
BigDecimal amount = productRes.getResult();
6372
// 扣减余额
64-
String str = accountClient.reduceBalance(userId, amount);
73+
Result<?> accountRes = accountClient.reduceBalance(userId, amount);
6574
// feign响应被二次封装,判断使主事务回滚
66-
JSONObject jsonObject = JSONObject.parseObject(str);
67-
if (jsonObject.getInteger("code") != 200) {
68-
throw new RuntimeException();
75+
if (!accountRes.isSuccess()) {
76+
String message = accountRes.getMessage();
77+
message = oConvertUtils.isEmpty(message) ? "操作失败" : message;
78+
throw new JeecgBootBizTipException(message);
6979
}
7080

7181
order.setStatus(OrderStatus.SUCCESS);

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-order/src/main/resources/application.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ spring:
44
data:
55
redis:
66
##redis 单机环境配置
7-
host: localhost
7+
host: jeecg-boot-redis
88
port: 6379
99
database: 0
1010
password:
@@ -23,14 +23,14 @@ spring:
2323
exclude: com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration
2424
datasource:
2525
driver-class-name: com.mysql.cj.jdbc.Driver
26-
url: jdbc:mysql://127.0.0.1:3306/jeecg_order?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false
26+
url: jdbc:mysql://jeecg-boot-mysql:3306/jeecg_order?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
2727
username: root
2828
password: root
2929
sql:
3030
init:
3131
schema-locations: classpath:sql/schema-order.sql
3232
seata:
33-
enable-auto-data-source-proxy: false
33+
enable-auto-data-source-proxy: true
3434
service:
3535
grouplist:
3636
default: 127.0.0.1:8091

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-product/src/main/java/org/jeecg/modules/test/seata/product/controller/SeataProductController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jeecg.modules.test.seata.product.controller;
22

3+
import org.jeecg.common.api.vo.Result;
34
import org.jeecg.modules.test.seata.product.service.SeataProductService;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.web.bind.annotation.PostMapping;
@@ -20,7 +21,7 @@ public class SeataProductController {
2021
private SeataProductService seataProductService;
2122

2223
@PostMapping("/reduceStock")
23-
public BigDecimal reduceStock(Long productId, Integer count, HttpServletRequest request) {
24+
public Result<BigDecimal> reduceStock(Long productId, Integer count, HttpServletRequest request) {
2425
return seataProductService.reduceStock(productId, count);
2526
}
2627
}

jeecg-boot/jeecg-server-cloud/jeecg-visual/jeecg-cloud-test/jeecg-cloud-test-seata/jeecg-cloud-test-seata-product/src/main/java/org/jeecg/modules/test/seata/product/service/SeataProductService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jeecg.modules.test.seata.product.service;
22

3+
import org.jeecg.common.api.vo.Result;
4+
35
import java.math.BigDecimal;
46

57
/**
@@ -16,5 +18,5 @@ public interface SeataProductService {
1618
* @param count 扣减数量
1719
* @return 商品总价
1820
*/
19-
BigDecimal reduceStock(Long productId, Integer count);
21+
Result<BigDecimal> reduceStock(Long productId, Integer count);
2022
}

0 commit comments

Comments
 (0)