在多个端点注册消费者

huangapple go评论46阅读模式
英文:

MassTransit - Register Consumer to multiple endpoints

问题

public class Message
{
public object Payload { get; set; }
}

public class Consumer1 : IConsumer<Message>
{
//... handler code
}

public class Consumer2 : IConsumer<Message>
{
//... handler code
}

我想将Consumer1注册到endpoint_1、endpoint_2和endpoint_3,而Consumer2注册到endpoint_1和endpoint_2。

英文:

In MassTransit how can a Consumer register to multiple endpoints?

public class Message
{
   public object Payload { get; set; }
}
 
public class Consumer1 : IConsumer&lt;Message&gt;
{
   //... handler code
}
 
public class Consumer2 : IConsumer&lt;Message&gt;
{
   //... handler code
}

And I'd like to register Consumer1 to endpoint_1, endpoint_2 and endpoint_3, while Consumer2 is registered to endpoint_1 and endpoint_2

答案1

得分: 1

你需要手动配置这些接收端点以及它们上面的消费者,按照文档中的指导进行操作。

services.AddMassTransit(x =>
{
    x.AddConsumer<Consumer1>();
    x.AddConsumer<Consumer2>();
    
    x.UsingRabbitMq((context, cfg) =>
    {
        cfg.ReceiveEndpoint("endpoint_1", e =>
        {
            e.ConfigureConsumer<Consumer1>(context);
            e.ConfigureConsumer<Consumer2>(context);
        });
        cfg.ReceiveEndpoint("endpoint_2", e =>
        {
            e.ConfigureConsumer<Consumer1>(context);
            e.ConfigureConsumer<Consumer2>(context);
        });
        cfg.ReceiveEndpoint("endpoint_3", e =>
        {
            e.ConfigureConsumer<Consumer1>(context);
        });
    });
});
英文:

You'd need to manually configure those receive endpoints, and the consumer(s) on each of them, following the guidance in the documentation.

services.AddMassTransit(x =&gt;
{
    x.AddConsumer&lt;Consumer1&gt;();
    x.AddConsumer&lt;Consumer2&gt;();
    
    x.UsingRabbitMq((context, cfg) =&gt;
    {
        cfg.ReceiveEndpoint(&quot;endpoint_1&quot;, e =&gt;
        {
            e.ConfigureConsumer&lt;Consumer1&gt;(context);
            e.ConfigureConsumer&lt;Consumer2&gt;(context);
        });
        cfg.ReceiveEndpoint(&quot;endpoint_2&quot;, e =&gt;
        {
            e.ConfigureConsumer&lt;Consumer1&gt;(context);
            e.ConfigureConsumer&lt;Consumer2&gt;(context);
        });
        cfg.ReceiveEndpoint(&quot;endpoint_3&quot;, e =&gt;
        {
            e.ConfigureConsumer&lt;Consumer1&gt;(context);
        });
    });
});

huangapple
  • 本文由 发表于 2023年3月31日 02:44:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定