use of io.seata.spring.annotation.GlobalTransactional in project spring-cloud-alibaba by alibaba.
the class HomeController method rest.
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@GetMapping(value = "/seata/rest", produces = "application/json")
public String rest() {
String result = restTemplate.getForObject("http://127.0.0.1:18082/storage/" + COMMODITY_CODE + "/" + ORDER_COUNT, String.class);
if (!SUCCESS.equals(result)) {
throw new RuntimeException();
}
String url = "http://127.0.0.1:18083/order";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("userId", USER_ID);
map.add("commodityCode", COMMODITY_CODE);
map.add("orderCount", ORDER_COUNT + "");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> response;
try {
response = restTemplate.postForEntity(url, request, String.class);
} catch (Exception exx) {
throw new RuntimeException("mock error");
}
result = response.getBody();
if (!SUCCESS.equals(result)) {
throw new RuntimeException();
}
return SUCCESS;
}
use of io.seata.spring.annotation.GlobalTransactional in project hummer-framework by hummer-team.
the class OrderService method save.
@GlobalTransactional(timeoutMills = 30000, name = "save-order")
@TargetDataSourceTM(dbName = "hj_classs_courseware", transactionManager = "hj_classs_courseware_TM", timeout = 5)
public int save() {
OrderPo po = new OrderPo();
po.setOrderTitle("test order");
po.setUserId(45677);
int result = orderDaoMapper.save(po);
log.info("save order done,db response {}", result);
return po.getOrderId();
}
use of io.seata.spring.annotation.GlobalTransactional in project SpringBoot-Project by MartinDai.
the class TradeService method createOrder.
/**
* 创建订单
*/
@GlobalTransactional(timeoutMills = 30000)
public void createOrder(String goodsCode, int stockNum, boolean mockException) {
String xid = RootContext.getXID();
System.out.println("createOrder 当前正在执行的事务xid:" + xid);
String result = storageService.reduceStock(goodsCode, stockNum);
if (!SUCCESS_RESPONSE.equals(result)) {
throw new IllegalStateException("库存扣减失败! xid:" + xid);
}
Order order = new Order().setGoodsCode(goodsCode).setStockNum(stockNum).setMoney(stockNum * 100).setUserId(1);
result = orderService.createOrder(order);
if (!SUCCESS_RESPONSE.equals(result)) {
throw new IllegalStateException("创建订单失败! xid:" + xid);
}
if (mockException) {
throw new IllegalStateException("模拟用户业务异常! xid:" + xid);
}
}
use of io.seata.spring.annotation.GlobalTransactional in project JavaFamily by chenjiabing666.
the class OrderServiceImpl method create.
/**
* 下订单的业务接口,其中完成了扣减库存-》扣减余额-》创建订单的流程
* 使用@GlobalTransactional这个注解开启一个全局的分布式事务
* @param userId 用于唯一Id
* @param productId 商品Id
* @param num 购买数量
*/
@GlobalTransactional
@Override
public Order create(String userId, Long productId, Long num) {
// todo 模拟下业务逻辑,细致的逻辑自己完善
// 1. 扣减库存
log.info("扣减库存开始..............");
storageFeignService.deduct(productId, num);
log.info("扣减库存结束..............");
// 2. 扣余额
log.info("扣减余额开始..............");
Storage storage = storageFeignService.getById(productId).getData();
accountFeignService.deduct(userId, storage.getPrice() * num);
log.info("扣减余额结束..............");
// 3. 创建订单
log.info("创建订单开始..............");
Order order = Order.builder().userId(userId).productId(productId).num(num).status(2).createTime(new Date()).build();
Order save = orderRepository.save(order);
return save;
}
use of io.seata.spring.annotation.GlobalTransactional in project FCLProject by FlowingCloudL.
the class OrderServiceImpl method submitOrder.
@GlobalTransactional
@Override
public Long submitOrder(Long uid, OrderSubmitDTO orderSubmitDTO) {
// 扣减库存
List<StockModifyBO> bos = orderSubmitDTO.getSkus().stream().map(dto -> {
StockModifyBO bo = new StockModifyBO();
BeanUtils.copyProperties(dto, bo);
return bo;
}).collect(Collectors.toList());
remoteStockService.decrStock(bos);
// 创建订单基本信息
OrderInfoEntity orderInfoEntity = new OrderInfoEntity();
BeanUtils.copyProperties(orderSubmitDTO, orderInfoEntity);
orderInfoEntity.setUserId(uid);
orderInfoEntity.setCreateTime(LocalDateTime.now());
orderInfoEntity.setUpdateTime(LocalDateTime.now());
orderInfoMapper.insert(orderInfoEntity);
// 创建订单项
for (OrderSubmitSkuDTO submitSkuDTO : orderSubmitDTO.getSkus()) {
// 远程调用获得订单项所需的SKU和SPU信息
OrderSpuBO orderSpuBO = remoteSpuService.getSpuBySkuId(submitSkuDTO.getSkuId());
// 生成订单项Entity
OrderItemEntity orderItemEntity = new OrderItemEntity();
// 设置订单id
orderItemEntity.setOrderId(orderInfoEntity.getOrderId());
// 设置spu部分
BeanUtils.copyProperties(orderSpuBO, orderItemEntity);
// 设置sku部分
BeanUtils.copyProperties(orderSpuBO.getSkuBO(), orderItemEntity);
// 设置单价
orderItemEntity.setSkuPrice(orderSpuBO.getSkuBO().getPrice());
// 设置数量
orderItemEntity.setSkuQuantity(submitSkuDTO.getCount());
// 计算订单项价格
orderItemEntity.setRealAmount(orderItemEntity.getSkuPrice().multiply(new BigDecimal(orderItemEntity.getSkuQuantity())));
orderItemMapper.insert(orderItemEntity);
}
return null;
}
Aggregations