From 8fe67b5ebd803b23a05e3d67830eec6b07bec5fe Mon Sep 17 00:00:00 2001
From: zhangsan <646228430@qq.com>
Date: Tue, 27 May 2025 20:43:25 +0800
Subject: [PATCH] =?UTF-8?q?5.27=20=E5=88=86=E5=B8=83=E5=BC=8F=E4=BA=8B?=
=?UTF-8?q?=E5=8A=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cart-service/pom.xml | 5 +++++
.../java/com/hmall/cart/service/impl/CartServiceImpl.java | 2 ++
cart-service/src/main/resources/application.yaml | 6 +-----
cart-service/src/main/resources/bootstrap.yaml | 3 ++-
hm-api/src/main/java/com/hmall/api/client/ItemClient.java | 2 +-
.../api/client/fallback/ItemClientFallbackFactory.java | 1 +
item-service/pom.xml | 5 +++++
.../main/java/com/hmall/item/controller/ItemController.java | 2 +-
.../java/com/hmall/item/service/impl/ItemServiceImpl.java | 2 ++
item-service/src/main/resources/bootstrap.yaml | 3 ++-
trade-service/pom.xml | 5 +++++
.../java/com/hmall/trade/service/impl/OrderServiceImpl.java | 3 ++-
trade-service/src/main/resources/bootstrap.yaml | 3 ++-
13 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/cart-service/pom.xml b/cart-service/pom.xml
index 0e412d0..8c0011c 100644
--- a/cart-service/pom.xml
+++ b/cart-service/pom.xml
@@ -70,6 +70,11 @@
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-seata
+
${project.artifactId}
diff --git a/cart-service/src/main/java/com/hmall/cart/service/impl/CartServiceImpl.java b/cart-service/src/main/java/com/hmall/cart/service/impl/CartServiceImpl.java
index 8db022f..55b7b8a 100644
--- a/cart-service/src/main/java/com/hmall/cart/service/impl/CartServiceImpl.java
+++ b/cart-service/src/main/java/com/hmall/cart/service/impl/CartServiceImpl.java
@@ -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 implements IC
}
@Override
+ @Transactional
public void removeByItemIds(Collection itemIds) {
// 1.构建删除条件,userId和itemId
QueryWrapper queryWrapper = new QueryWrapper();
diff --git a/cart-service/src/main/resources/application.yaml b/cart-service/src/main/resources/application.yaml
index 67934e9..8eb085a 100644
--- a/cart-service/src/main/resources/application.yaml
+++ b/cart-service/src/main/resources/application.yaml
@@ -1,10 +1,6 @@
server:
port: 8082
- tomcat:
- threads:
- max: 50
- accept-count: 50
- max-connections: 100
+
feign:
httpclient:
enabled: true # 使用 Apache HttpClient(默认关闭)
diff --git a/cart-service/src/main/resources/bootstrap.yaml b/cart-service/src/main/resources/bootstrap.yaml
index 8cddd17..ec2c458 100644
--- a/cart-service/src/main/resources/bootstrap.yaml
+++ b/cart-service/src/main/resources/bootstrap.yaml
@@ -12,4 +12,5 @@ spring:
- dataId: shared_jdbc.yaml # 共享mybatis配置
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- - dataId: shared_sentinel.yaml #共享sentinel控制台
\ No newline at end of file
+ - dataId: shared_sentinel.yaml #共享sentinel控制台
+ - dataId: shared_seeta.yaml
\ No newline at end of file
diff --git a/hm-api/src/main/java/com/hmall/api/client/ItemClient.java b/hm-api/src/main/java/com/hmall/api/client/ItemClient.java
index d111d11..e3372c2 100644
--- a/hm-api/src/main/java/com/hmall/api/client/ItemClient.java
+++ b/hm-api/src/main/java/com/hmall/api/client/ItemClient.java
@@ -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")
diff --git a/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java b/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java
index e5cf301..a29daa2 100644
--- a/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java
+++ b/hm-api/src/main/java/com/hmall/api/client/fallback/ItemClientFallbackFactory.java
@@ -26,6 +26,7 @@ public class ItemClientFallbackFactory implements FallbackFactory {
@Override
public void deductStock(List items) {
// 库存扣减业务需要触发事务回滚,查询失败,抛出异常
+ log.error("[ITEM-FALLBACK] deductStock 降级,原因:", cause);
throw new BizIllegalException(cause);
}
};
diff --git a/item-service/pom.xml b/item-service/pom.xml
index 8e56a93..952b8c9 100644
--- a/item-service/pom.xml
+++ b/item-service/pom.xml
@@ -68,6 +68,11 @@
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-seata
+
${project.artifactId}
diff --git a/item-service/src/main/java/com/hmall/item/controller/ItemController.java b/item-service/src/main/java/com/hmall/item/controller/ItemController.java
index 01d02cc..5461997 100644
--- a/item-service/src/main/java/com/hmall/item/controller/ItemController.java
+++ b/item-service/src/main/java/com/hmall/item/controller/ItemController.java
@@ -36,7 +36,7 @@ public class ItemController {
@GetMapping
public List queryItemByIds(@RequestParam("ids") List ids){
//模拟线程延迟
- ThreadUtil.sleep(500);
+// ThreadUtil.sleep(500);
return itemService.queryItemByIds(ids);
}
diff --git a/item-service/src/main/java/com/hmall/item/service/impl/ItemServiceImpl.java b/item-service/src/main/java/com/hmall/item/service/impl/ItemServiceImpl.java
index e261e18..2d54716 100644
--- a/item-service/src/main/java/com/hmall/item/service/impl/ItemServiceImpl.java
+++ b/item-service/src/main/java/com/hmall/item/service/impl/ItemServiceImpl.java
@@ -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 implements IItemService {
@Override
+ @Transactional
public void deductStock(List items) {
String sqlStatement = "com.hmall.item.mapper.ItemMapper.updateStock";
boolean r = false;
diff --git a/item-service/src/main/resources/bootstrap.yaml b/item-service/src/main/resources/bootstrap.yaml
index f066f4e..2388c66 100644
--- a/item-service/src/main/resources/bootstrap.yaml
+++ b/item-service/src/main/resources/bootstrap.yaml
@@ -12,4 +12,5 @@ spring:
- dataId: shared_jdbc.yaml # 共享mybatis配置
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- - dataId: shared_sentinel.yaml #共享sentinel控制台
\ No newline at end of file
+ - dataId: shared_sentinel.yaml #共享sentinel控制台
+ - dataId: shared_seeta.yaml
\ No newline at end of file
diff --git a/trade-service/pom.xml b/trade-service/pom.xml
index 400ef45..8d96119 100644
--- a/trade-service/pom.xml
+++ b/trade-service/pom.xml
@@ -68,6 +68,11 @@
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-seata
+
${project.artifactId}
diff --git a/trade-service/src/main/java/com/hmall/trade/service/impl/OrderServiceImpl.java b/trade-service/src/main/java/com/hmall/trade/service/impl/OrderServiceImpl.java
index e9136ac..cabb846 100644
--- a/trade-service/src/main/java/com/hmall/trade/service/impl/OrderServiceImpl.java
+++ b/trade-service/src/main/java/com/hmall/trade/service/impl/OrderServiceImpl.java
@@ -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 implements
private final CartClient cartClient;
@Override
- @Transactional
+ @GlobalTransactional
public Long createOrder(OrderFormDTO orderFormDTO) {
// 1.订单数据
Order order = new Order();
diff --git a/trade-service/src/main/resources/bootstrap.yaml b/trade-service/src/main/resources/bootstrap.yaml
index 50a81df..39833b5 100644
--- a/trade-service/src/main/resources/bootstrap.yaml
+++ b/trade-service/src/main/resources/bootstrap.yaml
@@ -12,4 +12,5 @@ spring:
- dataId: shared_jdbc.yaml # 共享mybatis配置
- dataId: shared_log.yaml # 共享日志配置
- dataId: shared_swagger.yaml # 共享日志配置
- - dataId: shared_sentinel.yaml #共享sentinel控制台
\ No newline at end of file
+ - dataId: shared_sentinel.yaml #共享sentinel控制台
+ - dataId: shared_seeta.yaml
\ No newline at end of file