《轻松实现可伸缩性,容错性,和负载平衡的大规模多人在线系统》一文里面对“Erlang的分布式进程组(Distributed Named Process Groups)”大吹特吹,就是说的pg2这个module。文档那里虽然写了支持分布式节点,但是并没有说如何如何,只提供了一个join(Name, Pid)。
看了一下openpoker的源码,原来很简单,对于连接上的node,用 which_groups() 多调用几次就可以同步过来了:
主节点先创建一个Group:
[quote](foo@localhost)1> pg2:create(group).
ok
(foo@localhost)2> pg2:get_members(group).
[]
(foo@localhost)3> pg2:join(group, self()).
ok
(foo@localhost)4> pg2:get_members(group).
[<0.35.0>][/quote]
其他节点先连上主节点,然后调用pg2:which_groups().
[quote](bar@localhost)1> net_adm:ping('foo@localhost').
pong
(bar@localhost)2> pg2:which_groups().
[]
(bar@localhost)3> pg2:which_groups().
[group]
(bar@localhost)4> pg2:join(group, self()).
ok[/quote]
已经加入了:
[quote](foo@localhost)5> pg2:get_members(group).
[<4835.35.0>,<0.35.0>][/quote]
使用就是那么简单。但是在pg2的底层实现上,使用了ets,global等模块,下次继续研究。
看了一下openpoker的源码,原来很简单,对于连接上的node,用 which_groups() 多调用几次就可以同步过来了:
主节点先创建一个Group:
[quote](foo@localhost)1> pg2:create(group).
ok
(foo@localhost)2> pg2:get_members(group).
[]
(foo@localhost)3> pg2:join(group, self()).
ok
(foo@localhost)4> pg2:get_members(group).
[<0.35.0>][/quote]
其他节点先连上主节点,然后调用pg2:which_groups().
[quote](bar@localhost)1> net_adm:ping('foo@localhost').
pong
(bar@localhost)2> pg2:which_groups().
[]
(bar@localhost)3> pg2:which_groups().
[group]
(bar@localhost)4> pg2:join(group, self()).
ok[/quote]
已经加入了:
[quote](foo@localhost)5> pg2:get_members(group).
[<4835.35.0>,<0.35.0>][/quote]
使用就是那么简单。但是在pg2的底层实现上,使用了ets,global等模块,下次继续研究。