Skip to content

Latest commit

 

History

History
131 lines (97 loc) · 4 KB

15.1.md

File metadata and controls

131 lines (97 loc) · 4 KB

ノード = Erlang VM = Beam

Node.self ノードの名前を確認する

> Node.self
:nonode@nohost

iex --name node_name node_nameという名前のノードを開始する 設定され名前は node_name を使った完全修飾名

goh@goh% iex --name wibble
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex([email protected])1>

iex --sname node_name node_nameという名前のノードを開始する

goh@goh% iex --sname wibble
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(wibble@goh)1>

Node.list ノードの一覧を表示する

Node.connect :node_name node_name に接続する

goh@goh% iex --sname node_one
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(node_one@goh)1>
goh@goh% iex --sname node_two
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(node_two@goh)1> Node.list
[]
iex(node_two@goh)2> Node.connect :node_one@goh
true
iex(node_two@goh)3> Node.list
[:node_one@goh]

ノードAからノードBに接続すると、ノードBもノードAの存在を知ることになる

Kernel.spawn(fun) プロセスを生成して fun を実行し、生成したプロセスのPIDを返す

node_one でプロセスを生成して、関数を実行

iex(node_one@goh)5> Node.spawn(:"node_one@goh", func)
warning: found quoted atom "node_one@goh" but the quotes are not required. Quotes should only be used to introduce atoms with foreign characters in them
  iex:5

:node_one@goh
#PID<0.117.0>

node_one から node_two でプロセスを生成して、関数を実行 プロセスを生成したノードと生成されたプロセスは紐付いているので、結果は node_one に表示される

iex(node_one@goh)6> Node.spawn(:"node_two@goh", func)
warning: found quoted atom "node_two@goh" but the quotes are not required. Quotes should only be used to introduce atoms with foreign characters in them
  iex:6

:node_two@goh
#PID<10630.116.0>

ノード、クッキー、セキュリティ ノード接続時にリモートノードのクッキーと自分のクッキーを比較する クッキーは iex elixir コマンド実行時に --cookie で指定できる

Node.get_cookie 自ノードのクッキーを返す

goh@goh% iex --sname one --cookie chocolate-chip
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(one@goh)1> Node.get_cookie
:"chocolate-chip"

接続しようとしたノード: false 接続されようとしたノード: エラーメッセージ

goh@goh% iex --sname node_one --cookie cookie-one
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(node_one@goh)1> Node.connect :"node_two@goh"
warning: found quoted atom "node_two@goh" but the quotes are not required. Quotes should only be used to introduce atoms with foreign characters in them
  iex:1

false
goh@goh% iex --sname node_two --cookie cookie-two
Erlang/OTP 21 [erts-10.0.5] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(node_two@goh)1>
23:55:56.236 [error] ** Connection attempt from disallowed node :node_one@goh **

クッキーを指定しない場合は、以下のファイルにクッキーを保存して、使用する ~/.erlang.cookie