节点1:
F:WorkSpaceServersrc>erl -name hw@192.168.10.142 -setcookie 4213 consulting .erlang in "F:/WorkSpace/Server/src" Now in:"F:/WorkSpace/Server/src" Eshell V5.9.3.1 (abort with ^G) (hw@192.168.10.142)1> auth:get_cookie(). '4213' (hw@192.168.10.142)2> hw:start(). true (hw@192.168.10.142)3>
步骤
启动节点: erl -name hw@192.168.10.142 -setcookie 4213
启动进程: hw:start().
注意: 本地机器不同节点可以用都短名 -sname, 跨网络涉及到DNS要用-name
节点2:
F:WorkSpaceServersrc>erl -name bibo@192.168.10.142 -setcookie 4213 consulting .erlang in "F:/WorkSpace/Server/src" Now in:"F:/WorkSpace/Server/src" Eshell V5.9.3.1 (abort with ^G) (bibo@192.168.10.142)1> net_adm:ping(hw@192.168.10.142). * 2: syntax error before: 168.1 (bibo@192.168.10.142)1> net_adm:ping(hw@192.168.10.142). * 1: syntax error before: 168.1 (bibo@192.168.10.142)1> net_adm:ping('hw@192.168.10.142'). pong (bibo@192.168.10.142)2> node(). 'bibo@192.168.10.142' (bibo@192.168.10.142)3> auth_getcookie(). ** exception error: undefined shell command auth_getcookie/0 (bibo@192.168.10.142)4> auth:get_cookie(). '4213' (bibo@192.168.10.142)5> net_adm:ping('hw@192.168.10.142'). pong (bibo@192.168.10.142)6> node(). 'bibo@192.168.10.142' (bibo@192.168.10.142)7> rpc:call(hw@192.168.10.142,hw,store,[weather,cold]). * 1: syntax error before: 168.1 (bibo@192.168.10.142)7> rpc:call('hw@192.168.10.142',hw,store,[weather,cold]). cold (bibo@192.168.10.142)8> rpc:call('hw@192.168.10.142',hw,lookup,[weather]). {ok,cold} (bibo@192.168.10.142)9>
步骤
启动节点: erl -name bibo@192.168.10.142 -setcookie 4213
启动进程: rpc:call('hw@192.168.10.142',hw,store,[weather,cold]).
rpc:call('hw@192.168.10.142',hw,lookup,[weather]).
注意: 'hw@192.168.10.142' 不能少了单引号
两个节点用的代码是同样的, 如下:
%% @author Administrator %% @doc @todo Add description to bibo. -module(bibo). %% ==================================================================== %% API functions %% ==================================================================== -export([]). %% ==================================================================== %% Internal functions %% ==================================================================== %% ==================================================================== %% API functions %% ==================================================================== %%-export([start/2, stop/1]). -compile(export_all). %% ==================================================================== %% Internal functions %% ==================================================================== start()-> register(kvs, spawn(fun()-> loop() end) ). loop()-> receive {From, {store, Key, Value}}-> put(Key, {ok, Value}), From ! {kvs, Value}, loop(); {From, {lookup, Key}} -> From ! {kvs, get(Key)}, loop() end. store(Key, Value)-> rpc({store, Key, Value}). lookup(Key)-> rpc({lookup, Key}). rpc(Q)-> kvs ! {self(), Q}, receive {kvs, Reply}-> Reply end.