我是微服务分布式事务处理的新手。 我正在构建一个基于微服务的应用程序,其中包含两个通过 RabbitMQ 代理进行通信的微服务。 我正在尝试创建一个示例来演示如何使用 MassTransit 和 RabbitMq 处理事务。 我面临的问题是 MassTransitStateMachine 中的 During block 没有被执行。 请帮助找出为什么 During 语句没有被执行。
我有两项服务,一项是客户服务,当创建新客户时发布事件并将其插入数据库 MassTransitStateMachine 在 Initially 块中被调用并将 CustomerCreated 状态插入数据库并发布到事件 SmartCardUpdateEvent由第二个微服务更新智能卡表中的 customerID 使用。这个案例工作得很好。
然后如果 SmartCardConsumer 发生任何错误,它会向 CustomerCancelEvent 发布一个事件,CustomerCancelConsumer 使用该事件并删除在第一个微服务中创建的 CustomerId。这也很完美。
问题出现在MassTransitStateMachine。 在 The MassTransitStateMachine 中,我有以下代码应该在数据库中插入 CustomerCancelled 状态。这没有发生。 如您所见 **During(CustomerCreated, ** 这行代码没有执行,我也看不到控制台中的任何内容。 公共类 CustomerStateMachine :MassTransitStateMachine {
public CustomerStateMachine()
{
Event(() => CustomerCreatedEvent, x => x.CorrelateById(c => c.Message.CustomerID));
Event(() => CustomerCancelledEvent, x => x.CorrelateById(c => c.Message.CustomerID));
InstanceState(x => x.CurrentState);
Initially(
When(CustomerCreatedEvent)
.Then(context =>
{
Console.WriteLine("Customer Created Event...");
Console.WriteLine("CustomerID: " + context.Message.CustomerID);
Console.WriteLine("CustomerName: " + context.Message.CustomerName);
context.Saga.CustomerCreationDateTime = DateTime.Now;
context.Saga.CustomerId = context.Message.CustomerID;
context.Saga.CustomerName = context.Message.CustomerName;
// context.Saga.CustomerCancelDateTime = null;
})
.TransitionTo(CustomerCreated)
.Publish(context => new SmartCardUpdateEvent(context.Saga))
);
During(CustomerCreated,
When(CustomerCancelledEvent)
.Then(context =>
{
Console.WriteLine("CustomerCancelled" + context.Message.CustomerID);
context.Saga.CustomerCancelDateTime = DateTime.Now;
//context.Saga.CustomerId = context.Message.CustomerID;
//context.Saga.CustomerName = context.Message.CustomerName;
})
.TransitionTo(CustomerCancelled)
//.Publish(context => new CustomerCancelEvent(context.Saga))
);
}
public State CustomerCreated { get; }
public State CustomerCancelled { get; }
public Event<ICustomerCreatedEvent> CustomerCreatedEvent { get; }
public Event<ICustomerCancelledEvent> CustomerCancelledEvent { get; }
}
}