一个正在运行的 Erlang VM 实例被称为一个节点。如果你启动一个 Erlang shell,你最终会得到一个禁用分发的节点。您可以通过调用is_alive/0 函数来检查这一点。
$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
1> is_alive().
false
所有节点都有一个名称——即使是那些没有启用分发的节点:
2> node().
nonode@nohost
...由于分发被禁用,集群中没有节点(目前):
3> nodes().
[]
4> q().
ok
启用分发后,有两种通信方式:我们可以在 shell 中调用各种函数,让 Erlang VM
在启动时自动处理等等。
$ erl -sname earth
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
(earth@uplink)1> is_alive().
true
(earth@uplink)2> node().
earth@uplink
(earth@uplink)3> nodes().
[]
注意: uplink 是我机器的名称。全节点
名称始终为 name@host。您还可以看到提示与第一次/示例时的提示略有不同。
...但仍然没有人连接!在另一台机器上启动另一个节点,这次将其命名为 pluto。
$ erl -sname pluto
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3 (abort with ^G)
(pluto@uplink)1> is_alive().
true
(pluto@uplink)2> node().
pluto@uplink
(pluto@uplink)3> nodes().
[]
(pluto@uplink)4> net_adm:ping(earth@uplink).
pong
(pluto@uplink)5> nodes().
[earth@uplink]
你的 Erlang 集群现在已经启动并运行了。还有其他一些方法可以做到这一点。我建议你阅读Learn You Some Erlang for Great Good!...非常好的一本。
重要提示:如果您在两台机器上没有相同的“cookie”,您可能很难尝试与节点通信。相反,您可以同时启动节点并设置 cookie:$ erl -sname your_node_name -setcookie 'acookietorulethemall'。