一个 rebus 进程中的多个输入队列

2024-01-04

我正在使用 top-shelf 和 rebus 编写一个“多工作人员”应用程序。

我的想法是使用MyWorker1Namespace - MyWorker1Namespace.Messages, MyWorker2Namespace - MyWorker2Namespace.Messages图案。

我想在不跨越多个进程的情况下运行应用程序,而是想使用多个输入队列配置应用程序,以便在必要时准备将其拆分为多个进程。

有没有办法使用 Rebus 在一个应用程序中声明多个输入队列和多个工作线程?

我想配置应该是这样的:

<rebus inputQueue="MyWorker1Namespace.input" errorQueue="MyWorker1Namespace.error" workers="1" maxRetries="5"> </rebus>
<rebus inputQueue="MyWorker2Namespace.input" errorQueue="MyWorker2Namespace.error" workers="1" maxRetries="5"> </rebus>
...

由于 Rebus app.config XML 针对每进程一个总线实例的场景进行了优化,因此您无法配置多个总线fully在 XML 中,但是没有什么可以阻止您在同一进程内启动多个总线实例。

我经常这样做,例如在 Azure 辅助角色中,我希望托管多个逻辑端点,而不会产生物理上独立部署的成本,有时我也使用 Topshelf 托管的 Windows 服务来完成此任务。

通常,我的 app.config 最终会包含以下 XML:

<rebus workers="1">
    <add messages="SomeAssembly.Messages" endpoint="someEndpoint.input"/>
    <add messages="AnotherAssembly.Messages" endpoint="anotherEndpoint.input"/>
</rebus>

因此,我可以一劳永逸地配置每条总线的默认工作人员数量和端点映射。然后,当我的应用程序启动时,它将在应用程序生命周期内为每个总线保留一个 IoC 容器 - 使用 Windsor,我通常会得到一个通用总线安装程序,其中包含队列名称的参数,这允许我配置 Windsor像这样:

var containers = new List<IWindsorContainer> {
    new WindsorContainer()
        // always handlers first
        .Install(FromAssembly.Containing<SomeEndpoint.SomeHandler>())

        // and then the bus, which gets started at this point
        .Install(new RebusInstaller("someEndpoint.input", "error"))

        // and then e.g. background timers and other "living things"
        .Install(new PeriodicTimersInstannce()),

    new WindsorContainer()
        .Install(FromAssembly.Containing<AnotherEndpoint.AnotherHandler>())
        .Install(new RebusInstaller("anotherEndpoint.input", "error"))
};

// and then remember to dispose each container when shutting down the process

哪里的RebusInstaller(这是一种 Windsor 机制)基本上只是将具有正确队列名称的总线放入容器中,例如像这样:

Configure.With(new WindsorContainerAdapter(container))
    .Transport(t => t.UseMsmq(_inputQueueName, _errorQueueName))
    .(...) etc
    .CreateBus().Start();

我喜欢这样的想法:每个 IoC 容器实例都可以作为逻辑上独立的应用程序运行。这样,如果您想在将来的某个时候将事情分开,例如能够独立部署您的端点。

我希望这能为您提供一些灵感:)如果您需要更多指导,请随时询问。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

一个 rebus 进程中的多个输入队列 的相关文章

随机推荐