【问题标题】:NServiceBus Distributor in only 2 steps?NServiceBus Distributor 只需 2 步?
【发布时间】:2013-03-03 16:11:02
【问题描述】:

我在弄清楚如何正确使用分销商执行以下操作时遇到了一些麻烦:

  1. 创建一个发送命令的服务(分发者),这些命令分发给工作人员。如果我使用 IWantToRunAtStartup 实现启动 Distributor,我可以实现这种行为。见下文。
  2. 创建处理这些命令的服务(工作者)。然后,我将启动 X 个实例来横向扩展该工作人员。
  3. 到目前为止,这一切都在同一台机器上。

NSB 中包含的示例有点难以理解,或者可能只是我自己 :)。

例如,我有一个经销商和一个工人:

经销商:

class MessageCreator: IWantToRunAtStartup
{
    public IBus Bus { get; set; }

    public void Run()
    {
        Thread.Sleep(5000); //Allow workers to checkin
        for (int i = 0; i < 1000; i++ )
        {
            Bus.Send(new DoWorkForCustomerCommand { CustomerID = i });
        }

    }

    public void Stop() { }
}

...

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
    public void Init()
    {
        Configure.Instance.RunDistributor();
    }
}

app.config

<configSections>
    <section name="Logging" type="NServiceBus.Config.Logging, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" /> 
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
</configSections>

<MessageForwardingInCaseOfFaultConfig ErrorQueue="error"/>
<Logging Threshold="INFO" />
<UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="Messages" Endpoint="Worker" />
    </MessageEndpointMappings>
 </UnicastBusConfig>

工人:

public class MessageHandler: IHandleMessages<DoWorkForCustomerCommand >
{
    public void Handle(DoWorkForCustomerCommand message)
    {
        Console.WriteLine("Handled customer with Id: " + message.CustomerID );
    }
}

...

public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher
{
    public void Init()
    {
        Configure.Instance.EnlistWithDistributor();
        // For some reason this: Configure.Instance.RunDistributor(); achieves the same thing.
    }
}

app.config

<configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
</configSections>
<MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />

这适用于我的机器,并且可以很好地分配给我开始的任意数量的工人,但我没有错过什么吗,ScaleOut 示例似乎更复杂?

为什么我可以将工人作为分销商开始,然后看到工人表现得好像是工人,而实际上是作为分销商开始的?

如果我只是在工作程序 app.config 中添加队列名称/端点,这是否不能跨机器工作?

【问题讨论】:

    标签: c# nservicebus nservicebus-distributor


    【解决方案1】:

    默认情况下,如果你 RunDistributor(),那么 NSB 会在同一个进程中运行一个带有 Worker 节点的 Distributor。这就是为什么尽管有 RunDistributor() 配置,您仍会看到 Worker。要禁用此功能,请改用 RunDistributorWithNoWorkerOnItsEndpoint()。通过更改配置,所有这些都将在机器上运行。

    我可能会建议改用配置文件,因为这会稍微简化配置。您可以使用 NServiceBus.Distributor 和 NServicerBus.Worker 配置文件。如果您没有正确配置,配置文件将为您提供更多诊断信息。希望这会有所帮助。

    【讨论】:

    • 感谢您的回答。自从我发布了我必须在自己的流程中托管 NSB 的问题后,我实际上已经意识到,这已经大大改变了这个领域。前任。我不能再使用 IWantToRunAtStartup,但必须使用完全不同的解决方案,请参阅我的另一篇帖子 stackoverflow.com/questions/15255880/…。我仍然发现 ScaleOut 示例非常误导,因为有更多方法可以实现分发器。我认为他们会做出更多示例来说明如何实施分发器:)。
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多