分布式Elixir示例不起作用

时间:2015-08-22 03:50:01

标签: 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

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

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

我使用node1iex --sname node1 --cookie secretnode2 iex --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)

1 个答案:

答案 0 :(得分:5)

我认为你需要在节点名称中加上@符号才能将它们解释为单独的联网机器

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

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

Node.connect :"node@machinename2"

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