我正在使用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>
...
Run Code Online (Sandbox Code Playgroud)
由于Rebus app.config XML针对单进程实例每进程场景进行了优化,因此无法在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>
Run Code Online (Sandbox Code Playgroud)
从而允许我一劳永逸地配置每个总线的默认工作数和端点映射.然后,当我的应用程序启动时,它将在应用程序生命周期内为每个总线保留一个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
Run Code Online (Sandbox Code Playgroud)
其中RebusInstaller(这是一个Windsor机制)基本上只是将具有正确队列名称的总线放入容器中,例如:
Configure.With(new WindsorContainerAdapter(container))
.Transport(t => t.UseMsmq(_inputQueueName, _errorQueueName))
.(...) etc
.CreateBus().Start();
Run Code Online (Sandbox Code Playgroud)
我喜欢这样的想法,即每个IoC容器实例本身都是一个逻辑上独立的应用程序.这样,如果您希望能够独立部署端点,将来很容易将事情分开.
我希望这为你提供了一些灵感:)请不要犹豫,问你是否需要更多指针.