0

我在传奇状态机中构建了一个路由器滑动:

var builder = new RoutingSlipBuilder(NewId.NextGuid());
            var submitOrderUrl = QueueNames.GetActivityUri(nameof(SubmitOrderActivity));
            builder.AddActivity("SubmitOrder", submitOrderUrl, new
            {
                context.Message.OrderId
            });;
            builder.AddActivity("Payment", QueueNames.GetActivityUri(nameof(PaymentActivity)), new {
                context.Message.OrderId,
                context.Message.CustomerId,
                context.Message.Credit
            });
           
            builder.AddActivity("TakeProduct", QueueNames.GetActivityUri(nameof(TakeProductActivity)), new
            {
                context.Message.OrderId,
                Baskets
            });
            builder.AddVariable("OrderId", context.Message.OrderId);
            var routingSlip = builder.Build();
            await context.Execute(routingSlip);

我有 TakeProductActivity 活动: public class TakeProductActivity : IActivity<TakeProductArgument, TakeProductLog>: ...

 public async Task<ExecutionResult> Execute(ExecuteContext<TakeProductArgument> context)
        {
            logger.LogInformation($"Take Product Courier called for order {context.Arguments.OrderId}");            
            var uri = QueueNames.GetMessageUri(nameof(TakeProductTransactionMessage));
            var sendEndpoint = await context.GetSendEndpoint(uri);
            await sendEndpoint.Send<TakeProductTransactionMessage>(new
            {
                ProductBaskets = context.Arguments.Baskets                
            });
             
            return context.Completed(new { Baskets = context.Arguments.Baskets, OrderId=context.Arguments.OrderId });
        }

当我使用 sendEndpoint.Send() 方法(触发并忘记)时,当服务中发生异常时,补偿方法不会自动激活,但是当我使用 requestClient.GetResponse (request/reply) 方法调用服务时,当异常自动发生 Compensate 方法被调用。并且在 PaymentConsumer 中,当抛出异常时,它必须被称为付款的补偿方法,但事实并非如此!

///this class has implemented in another micro-service hosted separate process:
public class TakeProductTransactionConsumer : IConsumer<TakeProductTransactionMessage>
....
 public async Task Consume(ConsumeContext<TakeProductTransactionMessage> context)
        {
            if(context.Message.ProductBaskets.Count>0)
             { 
                    throw new Exception("Process Failed!");
             }
            logger.LogInformation($"Take product called ");
         
            Dictionary<int, int> productCounts = new Dictionary<int, int>();
            foreach (var item in context.Message.ProductBaskets)
            {
                productCounts.Add(item.ProductId, item.Count);
            }
            var products = await productService.TakeProducts(productCounts);
            await publishEndpoint.Publish<ProductsUpdatedEvent>(new
            {
                ProductUpdatedEvents = products.Select(p =>new { ProductId = p.Id,p.Price,p.Count}).ToList()
            });
           
            
        }

问题是 MassTransit 无法从 rabbitMQ 获取异常并自动调用补偿方法。当路由器滑动活动中抛出异常时,我应该如何告诉 MassTransit 调用补偿

4

1 回答 1

1

如果您的 Take Product 活动使用 Send 对 Take Product 服务进行一劳永逸,并且该服务引发异常,则该活动将永远不会知道它,因为它已经完成。即发即弃就是这样,在目标服务中没有观察到异常。

如果您希望在 take 产品服务抛出异常时导致 take 产品活动失败,您需要使用 request/response 来观察服务中的异常。

于 2020-07-12T11:59:45.730 回答