分布式Elixir示例不起作用

the*_*uto 6 elixir

我有一个非常简单的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
Run Code Online (Sandbox Code Playgroud)

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

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

我开始对IEx标志会node1iex --sname node1 --cookie secretnode2iex --sname node2 --cookie secret.

值得注意的是,在我的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)

Gav*_*aff 5

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

iex --name node@machinename1 --cookie mycookie
iex --name node@machinename2 --cookie mycookie
Run Code Online (Sandbox Code Playgroud)

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

Node.connect :"node@machinename2"
Run Code Online (Sandbox Code Playgroud)

请注意使节点名称成为elixir原子的冒号语法.由于@符号,需要引号.在尝试使用dns名称之前,你可以尝试使用原始IP地址的机器名,如果你遇到问题Nb:我使用--name而不是你的--sname