原标题:MassTransit 知多少 | 基于StateMachine实现Saga编排式分布式事务
状态机作为一种程序开发范例,在实际的应用开发中有很多的应用场景,其中.NET 中的async/await 的核心底层实现就是基于状态机机制。状态机分为两种:有限状态机和无限状态机,本文介绍的就是有限状态机,有限状态机在任何时候都可以准确地处于有限状态中的一种,其可以根据一些输入从一个状态转换到另一个状态。一个有限状态机是由其状态列表、初始状态和触发每个转换的输入来定义的。如下图展示的就是一个闸机的状态机示意图:
从上图可以看出,状态机主要有以下核心概念:
在.NET中,dotnet-state-machine/stateless
和MassTransit
都提供了开箱即用的状态机实现。本文将重点介绍MassTransit
中的状态机在Saga 模式中的应用。
在MassTransit 中MassTransitStateMachine
就是状态机的具体抽象,可以用其编排一系列事件来实现状态的流转,也可以用来实现Saga模式的分布式事务。并支持与EF Core和Dapper集成将状态持久化到关系型数据库,也支持将状态持久化到MongoDB、Redis等数据库。是以简单的下单流程:创建订单->扣减库存->支付订单举例而言,其示意图如下所示。
那具体如何使用MassTransitStateMachine
来应用编排式Saga 模式呢,接下来就来创建解决方案来实现以上下单流程示例。依次创建以下项目,除共享类库项目外,均安装MassTransit
和MassTransit.RabbitMQ
NuGet包。
项目 | 项目名 | 项目类型 |
---|---|---|
订单服务 | MassTransit.SmDemo.OrderService | ASP.NET Core Web API |
库存服务 | MassTransit.SmDemo.InventoryService | Worker Service |
支付服务 | MassTransit.SmDemo.PaymentService | Worker Service |
共享类库 | MassTransit.SmDemo.Shared | Class Library |
三个服务都添加扩展类MassTransitServiceExtensions
,并在Program.cs
类中调用services.AddMassTransitWithRabbitMq();
注册服务。
using System.Reflection;
using MassTransit.CourierDemo.Shared.Models;
namespace MassTransit.CourierDemo.InventoryService;
public static class MassTransitServiceExtensions
{
public static IServiceCollection AddMassTransitWithRabbitMq(this IServiceCollection services)
{
return services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
// By default, sagas are in-memory, but should be changed to a durable
// saga repository.
x.SetInMemorySagaRepositoryProvider();
var entryAssembly = Assembly.GetEntryAssembly();
x.AddConsumers(entryAssembly);
x.AddSagaStateMachines(entryAssembly);
x.AddSagas(entryAssembly);
x.AddActivities(entryAssembly);
x.UsingRabbitMq((context, busConfig) =>
{
busConfig.Host(
host: "localhost",
port: 5672,
virtualHost: "masstransit",
configure: hostCOnfig=>
{
hostConfig.Username("guest");
hostConfig.Password("guest");
});
busConfig.ConfigureEndpoints(context);
});
});
}
}
订单服务作为下单流程中的核心服务,主要职责包含接收创建订单请求和订单状态机的实现。先来定义OrderController
如下:
namespace MassTransit.SmDemo.OrderService.Controllers;
[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
private readonly IBus _bus;
public OrderController(IBus bus)
{
_bus = bus;
}
[HttpPost]
public async Task
{
await _bus.Publish
{
createOrderDto.CustomerId,
createOrderDto.ShoppingCartItems
});
return Ok();
}
}
紧接着,订阅ICreateOrderCommand
,执行订单创建逻辑,订单创建完毕后会发布ICreateOrderSucceed
事件。
public class CreateOrderConsumer : IConsumer
{
private readonly ILogger
public CreateOrderConsumer(ILogger
{
_logger = logger;
}
public async Task Consume(ConsumeContext
{
var shoppingItems =
context.Message.ShoppingCartItems.Select(item => new ShoppingCartItem(item.SkuId, item.Price, item.Qty));
var order = new Order(context.Message.CustomerId).NewOrder(shoppingItems.ToArray());
await OrderRepository.Insert(order);
_logger.LogInformation($"Order {owww.yii666.comrder.OrderId} created successfully");
await context.Publish
{
order.OrderId,
order.OrderItems
});
}
}
最后来实现订单状态机,主要包含以下几步:
OrderState
以保存状态机实例状态数据:using MassTransit.SmDemo.OrderService.Domains;
namespace MassTransit.SmDemo.OrderService;
public class OrderState : SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public string CurrentState { get; set; }
public Guid OrderId { get; set; }
public decimal Amount { get; set; }
public List
}
MassTransitStateMachine
并同时指定状态实例即可:namespace MassTransit.SmDemo.OrderService;
public class OrderStateMachine : MassTransitStateMachine
{
}
return services.AddMassTransit(x =>
{
//...
x.AddSagaStateMachine
.InMemoryRepository();
}
State
类型定义,本例中为:public State Created { get; private set; }
public State InventoryDeducted { get; private set; }
public State Paid { get; private set; }
public State Canceled { get; private set; }
Event
类型定义,本例涉及有:public Event OrderCreated {get; private set;}
public Event DeduceInventorySucceed {get; private set;}
public Event DeduceInventoryFailed {get; private set;}
public Event PayOrderSucceed {get; private set;}
public Event PayOrderFailed {get; private set;}
public Event ReturnInventorySucceed { get; private set; }
public Event OrderCanceled { get; private set; }
关联Id
。以下就是将事件消息中的传递的OrderId
作为关联ID。Event(() => OrderCreated, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => DeduceInventorySucceed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => DeduceInventoryFailed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => PayOrderSucceed, x => x.CorrelateById(m => m.Message.OrderId));
(1) 初始状态->已创建:触发条件为OrderCreated
事件,同时要发送IDeduceInventoryCommand
推动库存服务执行库存扣减。
Initially(
When(OrderCreated)
.Then(cOntext=>
{
context.Saga.OrderId = context.Message.OrderId;
context.Saga.OrderItems = context.Message.OrderItems;
context.Saga.Amount = context.Message.OrderItems.Sum(x => x.Price * x.Qty);
})
.PublishAsync(cOntext=> context.Init
{
context.Saga.OrderId,
DeduceInventoryItems =
context.Saga.OrderItems.Select(x => new DeduceInventoryItem(x.SkuId, x.Qty)).ToList()
}))
.TransitionTo(Created));
(2) 已创建-> 库存已扣减:触发条件为DeduceInventorySucceed
事件,同时要发送IPayOrderCommand
推动支付服务执行订单支付。
During(Created,
When(DeduceInventorySucceed)
.Then(cOntext=>
{
context.Publish
{
context.Saga.OrderId,
context.Saga.Amount
});
}).TransitionTo(InventoryDeducted),
When(DeduceInventoryFailed).Then(cOntext=>
{
context.Publish
{
context.Saga.OrderId
});
})
);
(3) 库存已扣减->已支付:触发条件为PayOrderSucceed
事件,转移到已支付后,流程结束。
During(InventoryDeducted,
When(PayOrderFailed).Then(cOntext=>
{
context.Publish
{
context.Message.OrderId,
ReturnInventoryItems =
context.Saga.OrderItems.Select(x => new ReturnInventoryItem(x.SkuId, x.Qty)).ToList()
});
}),
When(PayOrderSucceed).TransitionTo(Paid).Then(cOntext=> context.SetCompleted()));
最终完整版的OrderStateMachine
如下所示:
using MassTransit.SmDemo.OrderService.Events;
using MassTransit.SmDemo.Shared.Contracts;
namespace MassTransit.SmDemo.OrderService;
public class OrderStateMachine : MassTransitStateMachine
{
public State Created { get; private set; }
public State InventoryDeducted { get; private set; }
public State Paid { get; private set; }
public State Canceled { get; private set; }
public Event
public Event
public Event
public Event
public Event
public Event
public Event
public Event
public OrderStateMachine()
{
Event(() => OrderCreated, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => DeduceInventorySucceed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => DeduceInventoryFailed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => ReturnInventorySucceed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => PayOrderSucceed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => PayOrderFailed, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => OrderCanceled, x => x.CorrelateById(m => m.Message.OrderId));
Event(() => OrderStateRequested, x =>
{
x.CorrelateById(m => m.Message.OrderId);
x.OnM文章来源地址19328.htmlissingInstance(m =>
{
return m.ExecuteAsync(x => x.RespondAsync
});
});
InstanceState(x => x.CurrentState);
Initially(
When(OrderCreated)
.Then(cOntext=>
{
context.Saga.OrderId = context.Message.OrderId;
context.Saga.OrderItems = context.Message.OrderItems;
var amount = context.Message.OrderItems.Sum(x => x.Price * x.Qty);
context.Saga.Amount = amount;
})
.PublishAsync(cOntext=> context.Init
{
context.Saga.OrderId,
DeduceInventoryItems =
context.Saga.OrderItems.Select(x => new DeduceInventoryItem(x.SkuId, x.Qty)).ToList()
}))
.TransitionTo(Created));
During(Created,
When(DeduceInventorySucceed)
.Then(cOntext=>
{
context.Publish
{
contex文章来源地址19328.htmlt.Saga.OrderId,
context.Saga.Amount
});
}).TransitionTo(InventoryDeducted),
When(DeduceInventoryFailed).Then(cOntext=>
{
context.Publish
{
context.Saga.OrderId
www.yii666.com });
})
);
During(InventoryDeducted,
When(PayOrderFailed).Then(cOntext=>
{
context.Publish
{
context.Message.OrderId,
ReturnInventoryItems =
context.Saga.OrderItems.Select(x => new ReturnInventoryItem(x.SkuId, x.Qty)).ToList()
});
}),
When(PayOrderSucceed).TransitionTo(Paid).Then(cOntext=> context.SetCompleted()),
When(ReturnInventorySucceed)
.ThenAsync(cOntext=> context.Publish
{
context.Saga.OrderId
})).TransitionTo(Created));
DuringAny(When(OrderCanceled).TransitionTo(Canceled).ThenAsync(async cOntext=>
{
await Task.Delay(TimeSpan.FromSeconds(10));
await context.SetCompleted();
}));
DuringAny(
When(OrderStateRequested)
.RespondAsync(x => x.Init
{
x.Saga.OrderId,
State = x.Saga.CurrentState
}))
);
}
}
库存服务在整个下单流程的职责主要是库存的扣减和返还,其仅需要订阅IDeduceInventoryCommand
和IReturnInventoryCommand
两个命令并实现即可。代码如下所示:
using MassTransit.SmDemo.InventoryService.Repositories;
using MassTransit.SmDemo.Shared.Contracts;
namespace MassTransit.SmDemo.InventoryService.Consumers;
public class DeduceInventoryConsumer : IConsumer
{
private readonly ILogger
public DeduceInventoryConsumer(ILogger
{
_logger = logger;
}
public async Task Consume(ConsumeContext
{
if (!CheckStock(context.Message.DeduceInventoryItems))
{
_logger.LogWarning($"Insufficient stock for order [{context.Message.OrderId}]!");
await context.Publish
new { context.Message.OrderId, Reason = "insufficient stock" });
}
else
{
_logger.LogInformation($"Inventory has been deducted for order [{context.Message.OrderId}]!");
DeduceStocks(context.Message.DeduceInventoryItems);
await context.Publish
}
}
private bool CheckStock(List
{
foreach (var stockItem in deduceItems)
{
if (InventoryRepository.GetStock(stockItem.SkuId)
return true;
}
private void DeduceStocks(List
{
foreach (var stockItem in deduceItems)
文章来源站点https://www.yii666.com/ {
InventoryRepository.TryDeduceStock(stockItem.SkuId, stockItem.Qty);
}
}
}
namespace MassTransit.SmDemo.InventoryService.Consumers;
public class ReturnInventoryConsumer : IConsumer
{
private readonly ILogger
public ReturnInventoryConsumer(ILogger
{
_logger = logger;
}
public async Task Consume(ConsumeContext
{
foreach (var returnInventoryItem in context.Message.ReturnInventoryItems)
{
InventoryRepository.ReturnStock(returnInventoryItem.SkuId, returnInventoryItem.Qty);
}
_logger.LogInformation($"Inventory has been returned for order [{context.Message.OrderId}]!");
await context.Publish
}
}
对于下单流程的支付用例来说,要么成功要么失败,因此仅需要订阅IPayOrderCommand
命令即可,具体PayOrderConsumer
实现如下:
using MassTransit.SmDemo.Shared.Contracts;
namespace MassTransit.SmDemo.PaymentService.Consumers;
public class PayOrderConsumer : IConsumer
{
private readonly ILogger
public PayOrderConsumer(ILogger
{
_logger = logger;
}
public async Task Consume(ConsumeContext
{
await Task.Delay(TimeSpan.FromSeconds(10));
if (context.Message.Amount % 2 == 0)
{_logger.LogInformation($"Order [{context.Message.OrderId}] paid successfully!");
await context.Publish
}
else
{
_logger.LogWarning($"Order [{context.Message.OrderId}] payment failed!");
await context.Publish
{
context.Message.OrderId,
Reason = "Insufficient account balance"
});
}
}
}
启动三个项目,并在Swagger中发起订单创建请求,如下图所示:
由于订单总额为奇数,因此支付会失败,最终控制台输出如下图所示:
打开RabbitMQ后台,可以看见MassTransit按照约定创建了以下队列用于服务间的消息传递:
其中order-state
队列绑定到类型为fanout
的同名order-state
Exchange,其绑定关系如下图所示,该Exchange负责从其他同名事件的Exchange转发事件。
通过以上示例的讲解,相信了解到MassTransit StateMachine的强大之处。StateMachine充当着事务编排器的角色,通过集中定义状态、转移条件和状态转移的执行顺序,实现高内聚的事务流转控制,也确保了其他伴生服务仅需关注自己的业务逻辑,而无需关心事务的流转,真正实现了关注点分离。
来源于:MassTransit 知多少 | 基于StateMachine实现Saga编排式分布式事务