分布式 Elixir 示例不起作用

Distributed Elixir example doesn't work

我有一个非常简单的 Elixir 代码示例,我想 运行 在不同的节点上。

第一个节点在我的笔记本电脑上,第二个节点是 Raspberry Pi,通过 SSH 访问。

代码很简单:

# node1@my-computer
defmodule Hello do
    def world, do: IO.puts "hello world"
end

# node2@raspberrypi
iex> Node.spawn_link :"node1@my-computer", fn -> Hello.world end

我预计 Node.spawn_link 会在 Raspberry Pi 上打印 "hello world",但它却显示错误 ** (EXIT from #PID<0.66.0>) no connect

在具有两个不同 iex 实例的同一台机器上,这段代码工作正常。

我将在 node1iex --sname node1 --cookie secret 以及 node2iex --sname node2 --cookie secret 开始 IEx 会话。

还值得注意的是,在我的 Raspberry Pi iex 中以这个警告开头:

warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell) Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)

我认为您需要在节点名称中加入 @ 符号,以便将它们解释为单独的联网机器

iex --name node@machinename1 --cookie mycookie
iex --name node@machinename2 --cookie mycookie

然后在第一个 iex shell 中连接节点:

Node.connect :"node@machinename2"

注意使节点名称成为 elixir 原子的冒号语法。由于 @ 符号,需要引号。 如果遇到问题,您可以先尝试使用原始 ip 地址的机器名,然后再尝试 dns 名称 注意:我使用 --name 而不是你的 --sname