两台服务器监听同一个地址

发布于 2024-10-18 08:26:53 字数 220 浏览 5 评论 0原文

我有一个可能运行多个实例的应用程序。我有一个同时在所有实例上执行的任务。 我想安排一个操作系统任务来运行一个通过命名管道发送消息的小应用程序,并且我想让我的所有实例都侦听该管道并执行其操作。 我尝试了 WCF 命名管道,并在尝试运行第二个实例时收到了 AddressAlreadyInUseException。

可行吗?有道理吗?我的目标是找到正确的解决方案吗? (并不是说我不需要从应用程序向调用者发送回复)

I have an appliction that might be running multiple instances. I have a task that executes on all instances at the same time.
I want to schedule an OS task to run a small app that sends message trough a named pipe, and I want to have all my instnces listenning on that pipe and doing their stuff.
I tried WCF named pipes and got AddressAlreadyInUseException when I tried to run the second instnce.

Is it doable? Does it make sense? Am I aiming for the right solution? (Not that I don't need to send a reply from the appliction to the caller)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-10-25 08:26:53

典型的方法是这样的:

  • 单个“负载平衡器”进程具有众所周知的地址并持续监听它。
  • “工作”进程启动并联系“负载均衡器”进程;这在它们之间建立了一条管道。
  • 然后,“负载均衡器”在“工作人员”和外部世界之间路由请求和响应,通常每次都会选择最不繁忙的“工作人员”。

“工人”不需要监听管道或套接字;您必须对它们进行不同的配置,并将此配置传达给“负载均衡器”。通过侦听“负载均衡器”,每个“工作人员”都可以进行相同的配置,并且只需要知道如何联系“负载均衡器”。

A typical approach is like this:

  • A single 'load balancer' process has a well-known address and keeps listening on it.
  • A 'worker' process starts and contacts the 'load balancer' process; this establishes a pipe between them.
  • The 'load balancer' then routes requests and responses between 'workers' and the outside world, usually choosing the least busy 'worker' every time.

'Workers' need not listen on a pipe or a socket; you'd have to configure each of them differently and communicate this config to the 'load balancer'. With a listening 'load balancer', each 'worker' can be configured identically and only needs to know how to contact the 'load balancer'.

缺⑴份安定 2024-10-25 08:26:53

Windows 管道(就像输送油、水或其他物质的真实管道)只有两端,一端是服务器,另一端是单个客户端。

您不能让多个客户端在单个管道上“侦听”,并且沿着管道发送的每条消息都将由单个接收器接收。

您可以让多个服务器全部侦听同一管道名称上的管道连接,但管道名称只是一个集合点,而不是共享管道实例。连接到命名管道的每个客户端都将通过单个管道实例连接到单个服务器(客户端无法选择)。没有可靠的方法来枚举所有侦听服务器。 (如果您想使用 WCF,命名管道绑定创建和发布实际的管道名称 意味着,正如您所发现的,一次只有一个服务进程可以侦听特定的服务 url)。

两种可能的方法:

  1. Windows Mailslots 支持按照您需要的方式进行广播。您的工作进程将全部使用相同的名称创建一个邮槽,并监视它是否有消息到达。您的触发器应用程序将向邮槽发送一条消息。缺点是没有包装此功能的 .NET 库类,因此您需要通过 P/Invoke 直接使用 Win32 API。
  2. 扭转您的 WCF 服务并使用 NetNamedPipeBinding 提供的双工消息传递支持。您的工作进程将是 WCF 服务的客户端,该服务由触发器应用程序(如果工作进程一直在运行)或充当代理的单独服务公开(在这种情况下,它将公开第二个服务,该服务由触发器应用程序公开)。触发器应用程序将用于启动通知)。工作进程使用的 WCF 服务将具有注册和取消注册操作,工作进程将使用这些操作来通知其启动和停止。它还会有一个回调合约,触发器(或代理)应用程序将使用该回调合约将任务消息发送给工作人员。

我建议第二种方法不太脆弱(邮槽通信完全是一种方法,因此有更大的范围来难以诊断错误)。

Windows Pipes (like real pipes which carry oil, water, or whatever) have just two ends, one end the server, the other a single client.

You can't have multiple clients "listening" on a single pipe, and each message sent down a pipe will be received by a single receiver.

You can have multiple servers all listening for pipe connections on the same pipe name, but the pipe name is just a rendezvous point, not a shared pipe instance. Each client connecting to the named pipe will be connected to a single server (which the client can't choose) by a single pipe instance. There is no reliable way to enumerate all listening servers. (And if you want to use WCF, the way the named pipe binding creates and publishes the actual pipe name means that, as you have discovered, only one service process at a time can listen on a particular service url).

Two possible approaches:

  1. Windows Mailslots support broadcasting in the way you require. Your worker processes would all create a mailslot using the same name, and monitor it for messages arriving. Your trigger app would send a message to the mailslot. The downside is that there is no .NET library class wrapping this functionality, so you would need to work directly with the Win32 API via P/Invoke.
  2. Turn your WCF service around and use the support for duplex messaging which the NetNamedPipeBinding provides. Your worker processes would be clients of a WCF service exposed either by the trigger application (if it is running all the time the worker processes are) or by a separate service acting as a broker (in this case it would expose a second service which the trigger app would use to initiate the notifications). The WCF service used by the worker processes would have Register and UnRegister operations which the worker processes would use to notify their starting and stopping. It would also have a callback contract which the trigger (or broker) app would use to send the task messages to the workers.

I would recommend the second approach as being less brittle (mailslot communication is entirely one way, so there is more scope for difficult to diagnose errors).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文