简体   繁体   English

分布式环境中的观察者

[英]Observer in a distributed environment

Machine A needs to send a message to machine B. Machine A has a static IP but machine B does not. 机器A需要向机器B发送消息。机器A具有静态IP,但机器B没有。

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: 我可以想到的一个解决方案是机器B打开到机器A的TCP连接,然后机器A将数据/消息发送到机器B。但是,此解决方案具有以下局限性:

a) It is not scalable if there are many such machines as machine B to whom/which the data has be sent. a)如果要向其发送数据的机器B之类的机器很多,则它是不可伸缩的。 It might be a kill on the resources of machine A. 这可能会杀死机器A的资源。

b) It is machine A that needs to send the data when it wants. b)是机器A需要在需要时发送数据。 Machine B does not know when there will be data for it. 机器B不知道何时会有数据。 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. 在当前设计中,机器B将不得不通过TCP连接反复轮询机器A,询问其是否有数据。 This can get expensive if there are many machine B's. 如果有很多机器B,这可能会变得很昂贵。

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. 机器B可以订阅来自机器A的通知,以在数据可用时通知它。 However, how does one implement the pattern in a distributed environment when machine B does not have a static IP? 但是,当机器B没有静态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? 旁观者说,除了将原始套接字用于机器A并将数据发送到机器B之外,还有其他方法会更便宜吗?

What if machine B makes a call to machine A to register its IP address for updates? 如果机器B呼叫机器A以注册其IP地址以进行更新怎么办? 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. 只要机器A有数据,它就可以与已注册自己的所有IP建立新的连接并将其发送给它们。

看一下IP多播 ,尽管您可以通过简单的UDP广播来解决。

Not having a static IP means that it is accessible from outside, but it's address changes? 没有静态IP意味着可以从外部访问它,但是它的地址发生了变化?

If it does, then you can have the machine B call A.detach(old_ip); A.attach(new_ip) 如果是这样,那么您可以让机器B调用A.detach(old_ip); A.attach(new_ip) A.detach(old_ip); A.attach(new_ip) every time the address is changed. 每次更改地址时都会A.detach(old_ip); A.attach(new_ip)

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. 我会遵循您最初的想法,只是不需要轮询-B可以一直保持与A的空闲TCP连接,然后当A要发送消息时,它只会将其发送给它拥有的所有客户端当时已连接。 Overhead won't be a problem - even a fairly old machine can handle thousands of simultaneous mostly-idle TCP connections. 开销不会有问题-即使是相当老的机器也可以处理成千上万个同时空闲的TCP连接。

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

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

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