高度细化的SAGA模式实现:基于Spring Boot与RabbitMQ的跨服务事务

场景与技术栈

场景:电商系统中的订单创建流程,涉及订单服务(Order Service)、库存服务(Inventory Service)、支付服务(Payment Service)。

技术栈:

  • Java 11

  • Spring Boot 2.7.0

  • MySQL 8.0

  • RabbitMQ

项目结构与组件
  • order-service

  • inventory-service

  • payment-service

  • saga-coordinator

代码实现

1. Saga Coordinator

java 复制代码
@SpringBootApplicationpublic class SagaCoordinatorApplication {    public static void main(String[] args) {        SpringApplication.run(SagaCoordinatorApplication.class, args);    }}@Service@RequiredArgsConstructorpublic class SagaService {    private final SagaOrderService sagaOrderService;    private final SagaInventoryService sagaInventoryService;    private final SagaPaymentService sagaPaymentService;    private final SagaTransactionRepository sagaTransactionRepository;    public void placeOrder(Long orderId, Long productId, int quantity, double amount) throws ExecutionException, InterruptedException {        SagaTransaction transaction = new SagaTransaction();        transaction.setOrderId(orderId);        transaction.setProductId(productId);        transaction.setQuantity(quantity);        transaction.setAmount(amount);        transaction.setStatus(SagaTransaction.Status.INITIATED);        sagaTransactionRepository.save(transaction);        CompletableFuture.runAsync(() -> sagaOrderService.createOrder(transaction))            .thenRunAsync(() -> sagaInventoryService.decreaseInventory(transaction))            .thenRunAsync(() -> sagaPaymentService.charge(transaction))            .exceptionally(ex -> {                rollback(transaction);                throw new RuntimeException("Saga failed", ex);            });    }    private void rollback(SagaTransaction transaction) {        CompletableFuture.runAsync(() -> sagaPaymentService.refund(transaction))            .thenRunAsync(() -> sagaInventoryService.increaseInventory(transaction))            .thenRunAsync(() -> sagaOrderService.cancelOrder(transaction))            .exceptionally(ex -> {                transaction.setStatus(SagaTransaction.Status.FAILED);                sagaTransactionRepository.save(transaction);                throw new RuntimeException("Rollback failed", ex);            });    }}

2. SagaOrderService

java 复制代码
@Servicepublic class SagaOrderService {    private final OrderService orderService;    private final RabbitTemplate rabbitTemplate;    @Autowired    public SagaOrderService(OrderService orderService, RabbitTemplate rabbitTemplate) {        this.orderService = orderService;        this.rabbitTemplate = rabbitTemplate;    }    public void createOrder(SagaTransaction transaction) {        // 创建订单逻辑        // ...        // 发送创建订单完成的消息        rabbitTemplate.convertAndSend("order.create", transaction);    }    public void cancelOrder(SagaTransaction transaction) {        // 取消订单逻辑        // ...        // 发送取消订单完成的消息        rabbitTemplate.convertAndSend("order.cancel", transaction);    }}

3. SagaInventoryService

java 复制代码
@Servicepublic class SagaInventoryService {    private final InventoryService inventoryService;    private final RabbitTemplate rabbitTemplate;    @Autowired    public SagaInventoryService(InventoryService inventoryService, RabbitTemplate rabbitTemplate) {        this.inventoryService = inventoryService;        this.rabbitTemplate = rabbitTemplate;    }    public void decreaseInventory(SagaTransaction transaction) {        // 扣减库存逻辑        // ...        // 发送扣减库存完成的消息        rabbitTemplate.convertAndSend("inventory.decrease", transaction);    }    public void increaseInventory(SagaTransaction transaction) {        // 增加库存逻辑        // ...        // 发送增加库存完成的消息        rabbitTemplate.convertAndSend("inventory.increase", transaction);    }}

4. SagaPaymentService

java 复制代码
@Servicepublic class SagaPaymentService {    private final PaymentService paymentService;    private final RabbitTemplate rabbitTemplate;    @Autowired    public SagaPaymentService(PaymentService paymentService, RabbitTemplate rabbitTemplate) {        this.paymentService = paymentService;        this.rabbitTemplate = rabbitTemplate;    }    public void charge(SagaTransaction transaction) {        // 收款逻辑        // ...        // 发送收款完成的消息        rabbitTemplate.convertAndSend("payment.charge", transaction);    }    public void refund(SagaTransaction transaction) {        // 退款逻辑        // ...        // 发送退款完成的消息        rabbitTemplate.convertAndSend("payment.refund", transaction);    }}
SagaTransaction Entity
java 复制代码
@Entitypublic class SagaTransaction {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private Long orderId;    private Long productId;    private int quantity;    private double amount;    private Status status; // INITIATED, COMPLETED, ROLLING_BACK, ROLLED_BACK, FAILED    // Getters & Setters}
消息队列监听

在每个微服务中,需要添加消息队列的监听器,以便在接收到消息时执行相应的操作。例如,在order-service中:

java 复制代码
@Componentpublic class OrderSagaListener {    private final SagaOrderService sagaOrderService;    @Autowired    public OrderSagaListener(SagaOrderService sagaOrderService) {        this.sagaOrderService = sagaOrderService;    }    @RabbitListener(queues = "order.create")    public void handleOrderCreate(SagaTransaction transaction) {        sagaOrderService.createOrder(transaction);    }    @RabbitListener(queues = "order.cancel")    public void handleOrderCancel(SagaTransaction transaction) {        sagaOrderService.cancelOrder(transaction);    }}
额外细节
  • 为确保事务的一致性,可以使用RabbitMQ的发布确认(Publisher Confirms)机制。

  • 每个微服务的数据库事务应该使用@Transactional注解来保证ACID属性。

  • 需要设计失败重试和事务状态检查机制,确保在故障恢复时能够正确地执行补偿操作。

通过上述设计,SAGA模式与RabbitMQ的结合,不仅能够处理跨服务的事务,还能够通过消息队列实现服务解耦和消息的异步处理,提高系统的稳定性和可扩展性。

相关推荐
学长毕设8 小时前
基于SpringBoot的游戏攻略社区交流系统-附源码
spring boot·后端·游戏
叼烟扛炮9 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
文丑颜不良啊9 小时前
LeetCode3401~3420题目解析
java·leetcode
文丑颜不良啊11 小时前
LeetCode3421~3440题目解析
java·leetcode
code2cat11 小时前
【随笔】Java客户端选了HTTP/3,怎样知道请求实际走了哪个版本
java·开发语言·http
码云社区13 小时前
Java在线租房系统源码:VR看房素材、房源图片与内容维护方案
java·vue.js·vr·租房小程序·在线租房·看房租房
码云社区13 小时前
Spring Boot智慧物业系统源码:合同到期提醒、续签资料与资产协作
java·spring boot·后端·物业管理·物业系统·智慧物业
AI开源软件14 小时前
Vue3+SpringBoot3 业务配置统一管理:接管、保存与回退怎样保留有效值
java·前端·vue.js·spring boot·vue·ruoyi office
陈如水14 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
杨运交14 小时前
091-功能模块-API Key 管理功能模块解析:feature-sa-token 架构设计与数据库持久化
spring boot