启动远程 Erlang 节点
我想用 Erlang 编写一个主从应用程序。我正在考虑架构中需要的以下内容:
奴隶不应该在主人死亡时死亡,而是在主人死亡时尝试重新连接到它
如果远程节点没有自动连接或关闭,主节点应该自动启动远程节点(可能是 OTP)
是否有面向 OTP 的行为来执行此操作?我知道我可以使用 slave:start_link()
启动远程节点,并且可以使用 erlang:monitor()
监视节点,但我不知道如何将其合并在 gen_server
行为中。
I want to write a master-slave application in Erlang. I am thinking at the following things I need from the architecture:
the slaves shouldn't die when the master dies, but rather try to reconnect to it while the master is down
the master should automatically start the remote nodes if they don't connect automatically or they are down (probably the supervisor behaviour in OTP)
Is there a OTP oriented behaviour to do this? I know I can start remote nodes with slave:start_link()
and I can monitor nodes with erlang:monitor()
, but I don't know how this can be incorporated in a gen_server
behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意关于使用 erlang:monitor_node 和使用分布式应用程序的评论。
您不能仅使用从属模块来完成此任务,它明确指出“当主控终止时,由主控启动的所有从属节点将自动终止”。
目前也没有 OTP 行为可以做到这一点。监督树是分层的;似乎您正在寻找在应用程序逻辑方面存在层次结构的东西,但生成是在对等基础上完成的(或在个人基础上完成,具体取决于您的观点)。
如果您要使用多个 Erlang VM,那么您应该仔细考虑运行的数量,因为大量 VM 可能会因操作系统交换操作系统进程而导致性能问题。获得最佳性能的一条经验法则是每个 CPU 核心的操作系统进程不超过一个(即一个 Erlang VM)。
I agree with the comments about using erlang:monitor_node and the use of distributed applications.
You cannot just use the slave module to accomplish that, it clearly states "All slave nodes which are started by a master will terminate automatically when the master terminates".
There is currently no OTP behaviour to do it either. Supervision trees are hierarchical ; it seems like you are looking for something where there is a hierarchy in terms of application logic, but spawning is done an a peer-to-peer basis (or an individual basis, depending upon your point of view).
If you were to use multiple Erlang VMs then you should carefully consider how many you run, as a large number of them may cause performance issues due to the OS swapping OS processes in and out. A rule of thumb for best performance is to aim for having no more than one OS process (i.e. one Erlang VM) per CPU core.
如果您有兴趣研究其他实现,Basho 的 riak_core 框架对去中心化分布式应用程序有很好的了解。
riak_core_node_watcher.erl 中有大部分有趣的节点观察代码。
搜索一下,你会发现有不少有关该框架的讨论和演示。
If you're interested in studying other implementations, Basho's riak_core framework has a pretty good take on decentralized distributed applications.
riak_core_node_watcher.erl has most of the interesting node observation code in it.
Search and you'll find there are quite a few talks and presentations about the framework.