erlang gproc 全局属性的测试:{p,g, name}

  1. gproc 获取地址
    `https://github.com/esl/gproc
  2. 为测试配置主机名
    geidt /etc/hosts
127.0.1.1	example1.com
127.0.1.1	example2.com
127.0.1.1       example3.com
  1. 测试说明:
    此次测试启动了三个节点,分别为 'a@example1.com', 'b@example2.com' 和 'c@example3.com',首先在 'a@example1.com'通过ping 联通三个节点,
    然后三个节点分别注册名称为 {p, g, hehe}, 接下来'a@example1.com' 给 注册进程发送消息, 结果为三个节点上的shell进程都接收到消息
    测试代码如下:
    节点a@example1.com:
dev@debian:~/Work/ShankYan/gproc$ erl -pa gproc/ebin  -pa gen_leader_revival/ebin    -name a@ample1.com -setcookie 123
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]
Eshell V5.10.4  (abort with ^G)
(a@example1.com)1> net_adm:ping('b@example2.com').
pong
(a@example1.com)2> net_adm:ping('c@example3.com').
pong
(a@example1.com)3> gproc:start_link().
{ok,<0.52.0>}
(a@example1.com)4> gproc_dist:start_link().
{ok,<0.54.0>}
(a@example1.com)5> nodes().
['c@example3.com','b@example2.com']
(a@example1.com)6> gproc:reg({p,g,hehe}).
true
(a@example1.com)9> gproc:send({p,g,hehe},hehe).
hehe
(a@example1.com)10> flush().
Shell got hehe
ok
(a@example1.com)11> gproc:send({p,g,hehe},{form,a@example1.com}).
 1: syntax error before: '.'
(a@example1.com)11> gproc:send({p,g,hehe},{form,'a@example1.com'}).
{form,'a@example1.com'}
(a@example1.com)12> flush().
Shell got {form,'a@example1.com'}
ok
(a@example1.com)13> 

节点b@example2.com:

dev@debian:~/Work/ShankYan/gproc$ erl -pa gproc/ebin  -pa gen_leader_revival/ebin  -name b@example2.com -setcookie 123
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]
Eshell V5.10.4  (abort with ^G)
(b@example2.com)1> application:start(gproc).
ok
(b@example2.com)2> gproc_dist:start_link().
{ok,<0.58.0>}
(b@example2.com)3> gproc:reg({p,g,hehe}).
true
(b@example2.com)4> flush().
Shell got hehe
ok
(b@example2.com)5> flush().
Shell got {form,'a@example1.com'}
ok
(b@example2.com)6> 

节点c@example3.com:

dev@debian:~/Work/ShankYan/gproc$ erl -pa gproc/ebin  -pa gen_leader_revival/ebin  -name c@example3.com -setcookie 123
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]
Eshell V5.10.4  (abort with ^G)
(c@example3.com)1> net_adm:ping('a@example1.com').
pong
(c@example3.com)2> net_adm:ping('b@example2.com').
pong
(c@example3.com)3> nodes().
['a@example1.com','b@example2.com']
(c@example3.com)4> gproc:start_link().
{ok,<0.52.0>}
(c@example3.com)5> gproc_dist:start_link().
{ok,<0.54.0>}
(c@example3.com)6> gproc:reg({p,g,hehe}).
true
(c@example3.com)7> flush().
Shell got hehe
ok
(c@example3.com)8> flush().
Shell got {form,'a@example1.com'}
ok
(c@example3.com)9> 

相关资料:

https://github.com/uwiger/gproc/issues/50 ----how to run
https://github.com/esl/gproc/blob/master/doc/gproc.md ----API
http://blog.rusty.io/2009/09/16/g-proc-erlang-global-process-registry/ how to run g

  GProc automatically detects whether gen_leader is available on the system. If it is,
  then gproc will run in “global mode” meaning that all of the functions above work across
  your entire Erlang cluster rather than on a single node, which is also pretty friggin’
  sweet. Unfortunately, gproc currently 
  uses an obsolete version of gen_leader, so running gproc in global mode is not recommended.

http://christophermeiklejohn.com/erlang/2013/06/05/erlang-gproc-failure-semantics.html
这里有详细的检测g 过程,在不同的机器上,后面直接跟IP地址,不要跟127.0.0.1
We also need to add gen_leader as a dependency to support the global distribution of the registry
We’ll use the current recommended version of gen_leader by gproc, which is known to have multiple problems during netsplits.
{gproc, ".",
{git, "git://github.com/uwiger/gproc.git", "master"}},
{gen_leader, ".
",
{git, "https://github.com/garret-smith/gen_leader_revival.git", "master"}}
We’ll also modify the vm.args file to enable global distribution.

-gproc gproc_dist all


(riak_pg1@127.0.0.1)1> gproc:reg({p, g, group}).
true
(riak_pg1@127.0.0.1)2> gproc_dist:get_leader().
'riak_pg1@127.0.0.1'
(riak_pg1@127.0.0.1)4> nodes().
['riak_pg2@127.0.0.1']
(riak_pg1@127.0.0.1)5> gproc:lookup_pids({p, g, group}).
[<0.489.0>]


需要看的

-kernel dist_auto_connect 查一下
http://blog.yufeng.info/archives/2169#more-2169 节点之间链接失败解决方案
http://www.ejabberd.im/interconnect-erl-nodes 很不错的文档,查看

Erlang节点的连接 相关微博
net_kernel与节点互连,断开,监控
我理解的是 当前全局属性还是不稳定的(当然有参考的测试文档,提交在下面),还必须要保证节点之间是
相互联通的,这个除了ping,我还不知道其他的办法, 这个还需要在以后理解下;
Erlang集群节点集合的启动,这个还没有使用过
0. 关于gproc 的本地属性 {p, l, name} 看 坚强2002博客的标题名为
"[Erlang 0101] Gproc:扩展进程注册机制"

原文地址:https://www.cnblogs.com/ShankYan/p/4133696.html