简体   繁体   English

使用msmq队列对服务进行负载平衡?

[英]Loadbalancing a service with an msmq queue?

Is it possible to use an MSMQ queue to load-balance requests? 是否可以使用MSMQ队列来对请求进行负载平衡? If I have clients 1-n send in requests to a WCF service, which then enqueues each request, and have 1-n servers in the background each grabbing the next available request as they become available, I would assume this effectively load-balances.. however, how would I then get the response back? 如果我有客户端1-n向WCF服务发送请求,然后将每个请求排入队列,并且后台有1-n个服务器,每个服务器在可用时抓取下一个可用请求,我会假设这有效地进行负载平衡。但是,我怎么才能得到回复? I realize I could subscribe to 'complete' events in my WCF service, but then i have to parse all events when i only care about one.. How could I accomplish a callback mechanism of some sort with this architecture? 我意识到我可以在我的WCF服务中订阅“完整”事件,但是当我只关心一个事件时我必须解析所有事件。我怎么能用这种架构完成某种回调机制?

If you are using the MSMQ to send events, then there is no inbuilt concept of "getting the response back". 如果您使用MSMQ发送事件,那么就没有“获得响应”的内置概念。

Here's a solution: the clients can each have their own message queue which they listen on. 这是一个解决方案:客户端每个人都可以拥有自己的消息队列。 When the workers have completed their tasks they can send a "completed" message back to the client. 当工人完成任务后,他们可以将“已完成”的消息发送回客户端。

But how would the server know the address of the client's message queue? 但是服务器如何知道客户端消息队列的地址?

One approach would be to supply the client's message queue address as a parameter in each function. 一种方法是将客户端的消息队列地址作为每个函数中的参数提供。

A better approach would be to include this address as a custom header value. 更好的方法是将此地址包含为自定义标头值。 The custom header solution is a better approach for two reasons. 自定义标头解决方案是一种更好的方法,原因有两个。 Firstly, this can be configured via the client's configuration file, allowing the queue details to be changed after deployment. 首先,可以通过客户端的配置文件进行配置,允许在部署后更改队列详细信息。 Secondly, it's never desirable for the details of the communication "plumbing" to intrude into the function call abstraction layer, and the custom header solution allows you to avoid this. 其次,通信“管道”的细节进入函数调用抽象层是不可取的,并且自定义头解决方案允许您避免这种情况。

See this question for an example of custom headers being used. 有关正在使用的自定义标头的示例,请参阅此问题

You can have the workers put the replies in a separate reply queue. 您可以让工作人员将回复放在单独的回复队列中。 Then just use some unique id for each request-reply pair to identify the correct reply. 然后,只需为每个请求 - 回复对使用一些唯一ID即可识别正确的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM