分布式环境中的观察者

发布于 2024-08-04 11:48:58 字数 489 浏览 10 评论 0原文

机器 A 需要向机器 B 发送消息。机器 A 有静态 IP,但机器 B 没有。

我能想到的解决这个问题的一个选择是,机器 B 打开到机器 A 的 TCP 连接,然后机器 A 将数据/消息发送到机器 B。但是,此解决方案具有以下限制:

a) 如果满足以下条件,则该解决方案不可扩展:有许多诸如机器 B 之类的机器,数据已被发送到这些机器。这可能会占用机器 A 的资源。

b) 机器 A 需要在需要时发送数据。机器 B 不知道何时会有它的数据。在当前的设计中,机器 B 必须通过 TCP 连接反复轮询机器 A,询问它是否有任何数据。如果有很多机器 B,这可能会变得昂贵。

有没有更便宜的方法来解决这个问题?我想到了观察者设计模式。机器 B 可以订阅来自机器 A 的通知,以便在数据可用时通知它。然而,当机器 B 没有静态 IP 时,如何在分布式环境中实现该模式呢?

抛开观察者不谈,除了使用机器 A 的原始套接字将数据发送到机器 B 之外,还有其他方法可以更便宜吗?

Machine A needs to send a message to machine B. Machine A has a static IP but machine B does not.

One option I could think of to solve this problem is that machine B opens a TCP connection to machine A and then machine A sends the data/message to machine B. However, this solution has the following limitations:

a) It is not scalable if there are many such machines as machine B to whom/which the data has be sent. It might be a kill on the resources of machine A.

b) It is machine A that needs to send the data when it wants. Machine B does not know when there will be data for it. In the current design, machine B will have to keep polling machine A repeatedly with a TCP connection asking if it has any data for it or not. This can get expensive if there are many machine B's.

Is there a less expensive way to solve this problem? The Observer design pattern comes to mind. Machine B could subscribe to a notification from machine A to inform it when data becomes available. However, how does one implement the pattern in a distributed environment when machine B does not have a static IP?

Observer aside, is there a way other than using raw sockets for machine A to send that data to machine B, that would be less expensive?

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

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

发布评论

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

评论(4

夢归不見 2024-08-11 11:48:58

如果机器 B 调用机器 A 来注册其 IP 地址以进行更新会怎样?这将是一个快速的消息交换。每当机器 A 有数据时,它就可以创建一个到所有已注册的 IP 的新连接并向它们发送数据。

What if machine B makes a call to machine A to register its IP address for updates? That would be a quick message exchange. Whenever machine A has data it could create a new connection to all of the IPs that have registered themselves and send them the data.

旧竹 2024-08-11 11:48:58

请查看 IP 多播,尽管您可能可以通过简单的 UDP 广播来实现。

Look at IP Multicast, though you may be able to get by with simple UDP broadcast.

巾帼英雄 2024-08-11 11:48:58

没有静态IP意味着可以从外部访问它,但是它的地址会改变吗?

如果是,那么您可以让机器 B 调用 A.detach(old_ip); A.attach(new_ip) 每次地址改变时。

Not having a static IP means that it is accessible from outside, but it's address changes?

If it does, then you can have the machine B call A.detach(old_ip); A.attach(new_ip) every time the address is changed.

一身软味 2024-08-11 11:48:58

我同意你最初的想法,除了不需要轮询 - B 可以始终保持与 A 的空闲 TCP 连接,然后当 A 想要发送消息时,它只需将其发送给它拥有的所有客户端那时连接的。开销不会成为问题——即使是相当旧的机器也可以处理数千个同时大部分空闲的 TCP 连接。

(如果真实消息之间的间隔超过几分钟,您还需要实现某种保持活动的回显/回显回复类型消息,以便 B 可以快速检测到死连接并重新连接,并避免连接 -跟踪防火墙或路由器中超时路径中的信息)。

I'd go with your original idea, except that there's no need to poll - B can just maintain an idle TCP connection to A at all times, then when A wants to send a message it just sends it out to all the clients it has connected at that time. Overhead won't be a problem - even a fairly old machine can handle thousands of simultaneous mostly-idle TCP connections.

(You'll also want to implement some kind of keep-alive echo / echo reply type messages if the gaps between real messages are longer than a few minutes, so that B can quickly detect a dead connection and reconnect, and to avoid connection-tracking information in firewalls or routers in path from timing-out).

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