Seata服务的安装和配置请点击这里
yml配置(需要参与分布式事务的微服务)
seata:
enabled: true
application-id: ${spring.application.name}
tx-service-group: ${spring.application.name}
enable-auto-data-source-proxy: true
data-source-proxy-mode: AT
registry:
type: nacos
nacos:
application: serviceSeata
server-addr: 1.116.1.203:8840
namespace: d50a1a79-cf7c-4b95-ae2e-d0b4096a06dc
group: SpringCloudAlibaba
cluster: default
userName: nacos账号
password: nacos密码
config:
type: nacos
nacos:
server-addr: 1.116.1.203:8840
namespace: d50a1a79-cf7c-4b95-ae2e-d0b4096a06dc
group: SpringCloudAlibaba
dataId: seata.properties
userName: nacos账号
password: nacos密码
Java代码(@GlobalTransactional全局事务注解)
package com.springcloudalibaba.serviceUser.controller.buyer;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.jdbc.log.LogUtils;
import com.springcloudalibaba.basefeign.service.order.api.OrderFeign;
import com.springcloudalibaba.basefeign.service.order.vo.BuyerOrderVo;
import com.springcloudalibaba.basefeign.service.product.api.ProductFeign;
import com.springcloudalibaba.basefeign.service.product.vo.SellerProductVo;
import com.springcloudalibaba.bclass.base.BaseException;
import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.bclass.enums.SysStateEnum;
import com.springcloudalibaba.bclass.util.LoggerUtil;
import com.springcloudalibaba.serviceUser.entity.buyer.BuyerEntity;
import com.springcloudalibaba.serviceUser.entity.seller.SellerEntity;
import com.springcloudalibaba.serviceUser.service.buyer.BuyerService;
import com.springcloudalibaba.serviceUser.service.seller.SellerService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
@RestController
@RequestMapping("/buyer")
public class BuyerController {
@Resource
private BuyerService buyerService;
@Resource
private SellerService sellerService;
@Resource
private ProductFeign productFeign;
@Resource
private OrderFeign orderFeign;
@PostMapping("createOrder")
@GlobalTransactional
public BaseResult<String> createOrder() {
System.out.println("xid========================" + RootContext.getXID());
SellerProductVo sellerProductVo = new SellerProductVo();
sellerProductVo.setId(1000000L);
sellerProductVo.setStock(1);
BaseResult<String> baseResultForChangeStock = productFeign.changeStock(sellerProductVo);
if (!baseResultForChangeStock.isSuccess()) {
throw new BaseException(baseResultForChangeStock.getMsg());
}
BuyerOrderVo buyerOrderVo = new BuyerOrderVo();
buyerOrderVo.setCreateTime(new Date());
buyerOrderVo.setCode("order1000000");
buyerOrderVo.setProductId(1000000L);
buyerOrderVo.setPayState(SysStateEnum.ORDER_STATE_ING.getValue());
BaseResult<String> baseResultForCreateOrder = orderFeign.createOrder(buyerOrderVo);
if (!baseResultForCreateOrder.isSuccess()) {
throw new BaseException(baseResultForChangeStock.getMsg());
}
boolean updateBuyerFlag = buyerService.update(new UpdateWrapper<BuyerEntity>().set("grade", 0).eq("account", "31011225"));
if (!updateBuyerFlag) {
throw new BaseException("扣减账户金额失败");
}
System.out.println(0/0);
return new BaseResult<String>().success(buyerOrderVo.getCode());
}
}