Skip to content

Commit

Permalink
refactor: Seata实验室案例调整为Seata官网购买商品示例
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxianrui committed Nov 29, 2022
1 parent fdfcd93 commit 43ee28a
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.youlai.common.result.Result;
import com.youlai.laboratory.seata.pojo.form.SeataForm;
import com.youlai.laboratory.seata.pojo.vo.SeataDataVO;
import com.youlai.laboratory.seata.service.ISeataService;
import com.youlai.laboratory.seata.pojo.vo.SeataVO;
import com.youlai.laboratory.seata.service.SeataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,26 +22,12 @@
@Slf4j
public class SeataController {

private final ISeataService seataService;

@ApiOperation("订单支付")
@PostMapping("/order/_pay")
public Result payOrder(@RequestBody SeataForm seataForm) {

boolean openTx = seataForm.isOpenTx();
boolean result;
if (openTx) {
result = seataService.payOrderWithGlobalTx(seataForm);
} else {
result = seataService.payOrder(seataForm);
}
return Result.success(result);
}
private final SeataService seataService;

@ApiOperation("获取模拟数据")
@GetMapping("/data")
public Result getData() {
SeataDataVO result = seataService.getData();
SeataVO result = seataService.getData();
return Result.success(result);
}

Expand All @@ -52,4 +38,19 @@ public Result resetData() {
return Result.success(result);
}

@ApiOperation("购买商品")
@PostMapping("/purchaseGoods")
public Result purchaseGoods(@RequestBody SeataForm seataForm) {
boolean openTx = seataForm.isOpenTx();

String orderSn = null;
if (openTx) {
// 开启全局事务
orderSn = seataService.purchaseGoodsWithGlobalTx(seataForm);
} else {
orderSn = seataService.purchaseGoods(seataForm);
}

return Result.success(orderSn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
import lombok.Data;

/**
*
*
* @author haoxr
* @date 2022/4/21 23:16
*/
@Data
public class SeataForm {

/**
* 会员ID
*/
private Long memberId;

/**
* 商品ID
*/
private Long skuId;

/**
* 订单金额
*/
private Long amount;

/**
* 是否开启事务
*/
private boolean openTx;

/**
* 订单异常
* 是否开启异常
*/
private boolean orderEx;
private boolean openEx;


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
@ApiModel("Seata模拟数据视图对象")
@Data
public class SeataDataVO {
public class SeataVO {

@ApiModelProperty("商品库存信息")
private StockInfo stockInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
package com.youlai.laboratory.seata.service;

import com.youlai.laboratory.seata.pojo.form.SeataForm;
import com.youlai.laboratory.seata.pojo.vo.SeataDataVO;
import com.youlai.laboratory.seata.pojo.vo.SeataVO;

/**
* @author haoxr
* @date 2022/4/16 20:49
*/
public interface ISeataService {
public interface SeataService {

/**
* 模拟订单支付
* 获取模拟数据
*
* @return
*/
boolean payOrder(SeataForm seataForm);
SeataVO getData();

/**
* 模拟订单支付(分布式事务)
* 重置模拟数据
*
* @param seataForm
* @return
*/
boolean payOrderWithGlobalTx(SeataForm seataForm);
boolean resetData();

/**
* 获取模拟数据
* 购买商品
*
* @return
* @return 订单号
*/
SeataDataVO getData();
String purchaseGoods(SeataForm seataForm);

/**
* 重置模拟数据
* 购买商品(全局事务)
*
* @return
* @return 订单号
*/
boolean resetData();
String purchaseGoodsWithGlobalTx(SeataForm seataForm);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.youlai.laboratory.seata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.youlai.common.result.Result;
import com.youlai.laboratory.seata.pojo.form.SeataForm;
import com.youlai.laboratory.seata.pojo.vo.SeataDataVO;
import com.youlai.laboratory.seata.service.ISeataService;
import com.youlai.laboratory.seata.pojo.vo.SeataVO;
import com.youlai.laboratory.seata.service.SeataService;
import com.youlai.mall.oms.api.OrderFeignClient;
import com.youlai.mall.oms.dto.OrderInfoDTO;
import com.youlai.mall.oms.dto.SeataOrderDTO;
import com.youlai.mall.pms.api.SkuFeignClient;
import com.youlai.mall.pms.pojo.dto.SkuInfoDTO;
import com.youlai.mall.ums.api.MemberFeignClient;
Expand All @@ -24,7 +26,7 @@
@Service
@Slf4j
@RequiredArgsConstructor
public class SeataServiceImpl implements ISeataService {
public class SeataServiceImpl implements SeataService {

private final SkuFeignClient skuFeignClient;
private final OrderFeignClient orderFeignClient;
Expand Down Expand Up @@ -66,12 +68,14 @@ public boolean payOrderWithGlobalTx(SeataForm seataForm) {
log.info("========扣减商品库存(Seata)========");
skuFeignClient.deductStock(skuId, 1); // 扣减库存

log.info("========扣减账户余额(Seata)========");
memberFeignClient.deductBalance(memberId, 1000 * 100l); // 扣款1000
log.info("========修改订单状态(Seata)========");
orderFeignClient.createOrder(orderId, 201, seataForm.isOrderEx()); // 已支付

log.info("========修改订单状态(Seata)========");
orderFeignClient.updateOrderStatus(orderId, 201, seataForm.isOrderEx()); // 已支付

log.info("========扣减账户余额(Seata)========");
memberFeignClient.deductBalance(memberId, 1000 * 100l); // 扣款1000
return true;
}

Expand All @@ -81,26 +85,26 @@ public boolean payOrderWithGlobalTx(SeataForm seataForm) {
* @return
*/
@Override
public SeataDataVO getData() {
SeataDataVO seataDataVO = new SeataDataVO();
public SeataVO getData() {
SeataVO seataVO = new SeataVO();

SkuInfoDTO skuInfoDTO = skuFeignClient.getSkuInfo(skuId).getData();
SeataDataVO.StockInfo stockInfo = new SeataDataVO.StockInfo();
SeataVO.StockInfo stockInfo = new SeataVO.StockInfo();
BeanUtil.copyProperties(skuInfoDTO, stockInfo);
stockInfo.setName(skuInfoDTO.getSkuName());
seataDataVO.setStockInfo(stockInfo);
seataVO.setStockInfo(stockInfo);

MemberInfoDTO memberInfoDTO = memberFeignClient.getMemberInfo(memberId).getData();
SeataDataVO.AccountInfo accountInfo = new SeataDataVO.AccountInfo();
SeataVO.AccountInfo accountInfo = new SeataVO.AccountInfo();
BeanUtil.copyProperties(memberInfoDTO, accountInfo);
seataDataVO.setAccountInfo(accountInfo);
seataVO.setAccountInfo(accountInfo);

OrderInfoDTO orderInfoDTO = orderFeignClient.getOrderInfo(orderId).getData();
SeataDataVO.OrderInfo orderInfo = new SeataDataVO.OrderInfo();
SeataVO.OrderInfo orderInfo = new SeataVO.OrderInfo();
BeanUtil.copyProperties(orderInfoDTO, orderInfo);
seataDataVO.setOrderInfo(orderInfo);
seataVO.setOrderInfo(orderInfo);

return seataDataVO;
return seataVO;
}

/**
Expand All @@ -116,4 +120,52 @@ public boolean resetData() {
return true;

}

/**
* 购买商品
*
* @return 订单号
*/
@Override
public String purchaseGoods(SeataForm seataForm) {
log.info("========扣减商品库存(全局事务)========");
skuFeignClient.deductStock(seataForm.getSkuId(), 1); // 扣减库存

log.info("========创建订单(全局事务)========");
SeataOrderDTO seataOrderDTO = new SeataOrderDTO(
seataForm.getMemberId(),
seataForm.getSkuId(),
seataForm.getAmount()
);
boolean openEx = seataForm.isOpenEx(); // 是否开启异常
Result<String> result = orderFeignClient.createOrder(seataOrderDTO, openEx);
String orderSn = result.getData();

return orderSn;
}

/**
* 购买商品(全局事务)
*
* @return 订单号
*/
@GlobalTransactional
@Override
public String purchaseGoodsWithGlobalTx(SeataForm seataForm) {

log.info("========扣减商品库存(全局事务)========");
skuFeignClient.deductStock(seataForm.getSkuId(), 1); // 扣减库存

log.info("========创建订单(全局事务)========");
SeataOrderDTO seataOrderDTO = new SeataOrderDTO(
seataForm.getMemberId(),
seataForm.getSkuId(),
seataForm.getAmount()
);
boolean openEx = seataForm.isOpenEx(); // 是否开启异常
Result<String> result = orderFeignClient.createOrder(seataOrderDTO, openEx);
String orderSn = result.getData();

return orderSn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.youlai.common.result.Result;
import com.youlai.mall.oms.dto.OrderInfoDTO;
import com.youlai.mall.oms.dto.SeataOrderDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;

/**
* 订单Feign客户端
Expand Down Expand Up @@ -38,4 +36,13 @@ public interface OrderFeignClient {
@GetMapping("/api/v1/orders/{orderId}/info")
Result<OrderInfoDTO> getOrderInfo(@PathVariable Long orderId);

/**
* 「实验室」创建订单
*
* @param orderDTO
* @param openEx 是否出现异常
* @return
*/
@PostMapping("/api/v1/orders")
Result<String> createOrder(SeataOrderDTO orderDTO, boolean openEx);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import lombok.Data;

/**
* 订单信息传输层对象
* 订单传输层对象
*
* @author <a href="mailto:[email protected]">haoxr</a>
* @date 2022/4/17 21:12
* @author haoxr
* @date 2022/4/17
*/
@Data
public class OrderInfoDTO {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.youlai.mall.oms.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SeataOrderDTO {

/**
* 会员ID
*/
private Long memberId;

/**
* 商品ID
*/
private Long skuId;

/**
* 订单金额
*/
private Long amount;


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
*/

@AllArgsConstructor
public enum OrderTypeEnum {
public enum OrderSourceTypeEnum {

WEB(0), // PC订单
PC(0), // PC订单
APP(1), // APP订单
;

@Getter
private Integer code;

public static OrderTypeEnum getValue(Integer code){
for (OrderTypeEnum value : values()) {
public static OrderSourceTypeEnum getValue(Integer code){
for (OrderSourceTypeEnum value : values()) {
if (value.getCode().equals(code)) {
return value;
}
Expand Down
Loading

0 comments on commit 43ee28a

Please sign in to comment.