5.27 分布式事务

This commit is contained in:
zhangsan 2025-05-27 20:43:25 +08:00
parent 08e52ae9c8
commit 8fe67b5ebd
13 changed files with 31 additions and 11 deletions

View File

@ -70,6 +70,11 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>

View File

@ -18,6 +18,7 @@ import com.hmall.common.utils.CollUtils;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.List;
@ -105,6 +106,7 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
}
@Override
@Transactional
public void removeByItemIds(Collection<Long> itemIds) {
// 1.构建删除条件userId和itemId
QueryWrapper<Cart> queryWrapper = new QueryWrapper<Cart>();

View File

@ -1,10 +1,6 @@
server:
port: 8082
tomcat:
threads:
max: 50
accept-count: 50
max-connections: 100
feign:
httpclient:
enabled: true # 使用 Apache HttpClient默认关闭

View File

@ -13,3 +13,4 @@ spring:
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- dataId: shared_sentinel.yaml #共享sentinel控制台
- dataId: shared_seeta.yaml

View File

@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import java.util.Collection;
import java.util.List;
@FeignClient(value = "item-service",fallbackFactory = ItemClientFallbackFactory.class)
@FeignClient(value = "item-service",fallbackFactory = ItemClientFallbackFactory.class)
public interface ItemClient {
@GetMapping("/items")

View File

@ -26,6 +26,7 @@ public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
@Override
public void deductStock(List<OrderDetailDTO> items) {
// 库存扣减业务需要触发事务回滚查询失败抛出异常
log.error("[ITEM-FALLBACK] deductStock 降级,原因:", cause);
throw new BizIllegalException(cause);
}
};

View File

@ -68,6 +68,11 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>

View File

@ -36,7 +36,7 @@ public class ItemController {
@GetMapping
public List<ItemDTO> queryItemByIds(@RequestParam("ids") List<Long> ids){
//模拟线程延迟
ThreadUtil.sleep(500);
// ThreadUtil.sleep(500);
return itemService.queryItemByIds(ids);
}

View File

@ -11,6 +11,7 @@ import com.hmall.item.domain.po.Item;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.Collections;
@ -27,6 +28,7 @@ import java.util.List;
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {
@Override
@Transactional
public void deductStock(List<OrderDetailDTO> items) {
String sqlStatement = "com.hmall.item.mapper.ItemMapper.updateStock";
boolean r = false;

View File

@ -13,3 +13,4 @@ spring:
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- dataId: shared_sentinel.yaml #共享sentinel控制台
- dataId: shared_seeta.yaml

View File

@ -68,6 +68,11 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>

View File

@ -15,6 +15,7 @@ import com.hmall.trade.domain.po.OrderDetail;
import com.hmall.trade.mapper.OrderMapper;
import com.hmall.trade.service.IOrderDetailService;
import com.hmall.trade.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -43,7 +44,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
private final CartClient cartClient;
@Override
@Transactional
@GlobalTransactional
public Long createOrder(OrderFormDTO orderFormDTO) {
// 1.订单数据
Order order = new Order();

View File

@ -13,3 +13,4 @@ spring:
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- dataId: shared_sentinel.yaml #共享sentinel控制台
- dataId: shared_seeta.yaml