【问题标题】:Masstransit How to make Advanced request / reply patternMasstransit 如何进行高级请求/回复模式
【发布时间】:2021-08-25 07:54:18
【问题描述】:

我有 3 个微服务如下通过masstransit/rabbitmq 进行通信

我希望ApiTransactionService 发出请求,但得到了PspService 的响应。

namespace API
{
    //I wish to have something like this
    PaymentForm form = await requestClient.GetResponse<PaymentForm>(new CreatePayment())
}

namespace TransactionService
{
    public class CreatePaymentConsumer : IConsumer<CreatePayment>
    {
        public async Task Consume(ConsumeContext<CreatePayment> context)
        {
            context.Send<BuildPaymentForm>()
        }
    }
}

namespace PspService
{
    public class BuildPaymentFormConsumer : IConsumer<BuildPaymentForm>
    {
        public async Task Consume(ConsumeContext<BuildPaymentForm> context)
        {
            context.Response<PaymentForm>() //how to make sure that the response will be sent to the API, but not to the caller (TransactionService)?
        }
    }
}

请指出制作这种通信模式或类似示例的正确方法,或文档中的正确部分。

【问题讨论】:

    标签: c# rabbitmq masstransit


    【解决方案1】:

    您可以使用CreateCopyContextPipe 方法将CreatePayment 消息中的RequestIdResponseAddress 复制到第一个消费者生成的消息中。还有一种内置方法可以将所有标头复制到传出消息(我在下面使用过)。

    namespace TransactionService
    {
        public class CreatePaymentConsumer : IConsumer<CreatePayment>
        {
            public async Task Consume(ConsumeContext<CreatePayment> context)
            {
                var pipe = context.CreateCopyContextPipe();
    
                await _otherHost.Publish(new BuildPaymentForm(...), pipe);
            }
        }
    }
    
    namespace PspService
    {
        public class BuildPaymentFormConsumer : IConsumer<BuildPaymentForm>
        {
            public async Task Consume(ConsumeContext<BuildPaymentForm> context)
            {
                await context.RespondAsync(new PaymentForm(...));
            }
        }
    }
    

    第一个消费者将命令发布给第二个消费者,将所有标头复制到出站消息。然后第二个消费者将响应请求发起者。

    【讨论】:

      猜你喜欢
      • 2013-04-22
      • 1970-01-01
      • 2012-08-11
      • 2021-02-09
      • 2018-04-02
      • 2021-11-19
      • 2011-05-21
      • 1970-01-01
      • 2021-09-30
      相关资源
      最近更新 更多