net_kernel:monitor_nodes 订阅node连接\断开消息
Erlang中节点之间建立链接我们可以使用net_adm:ping(),连接成功返回pong,失败返回pang;实践中我们不仅仅是要建立连接,比如我们需要在与其它节点建立连接或者其它节点断开的时候做一些事情,比如在节点当掉的时候记录日志,这就需要在对应的时机取得相应的信息;Erlang在net_kernel库中提供了这样的方法:net_kernel:monitor_nodes(Flag);调用这个方法来订阅节点状态变动的消息.一个节点加入的时候会向所有的订阅进程发送一个nodeup消息,一个节点断开的时候会发送nodedown消息.
看规格说明中,我们可以对订阅的消息进行定制:
doc地址:http://www.erlang.org/doc/man/net_kernel.html#monitor_nodes-1
monitor_nodes(Flag) -> ok | Error monitor_nodes(Flag, Options) -> ok | Error Types: Flag = boolean() Options = [Option] Option = {node_type, NodeType} | nodedown_reason NodeType = visible | hidden | all Error = error | {error, term()}
一个典型的应用就是rabbitmq项目中rabbit_node_monitor的代码,关于rabbitmq项目请点击这里,解析:
在下面这段代码里面rabbit_node_monitor是一个gen_server,在它启动的回调函数中,调用了 net_kernel:monitor_nodes(true)方法,同时在handle_info中添加了对nodeup和nodedown消息的处理,这段代码里面在和其它节点建立链接的时候(nodeup)记录了日志,在节点当掉的时候(nodedown)做了数据库和网络的清理
工作,相关的代码文件位置: rabbit_networking rabbit_amqqueue
-module(rabbit_node_monitor). -behaviour(gen_server). -export([start_link/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -define(SERVER, ?MODULE). %%-------------------------------------------------------------------- start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). %%-------------------------------------------------------------------- init([]) -> ok = net_kernel:monitor_nodes(true), {ok, no_state}. handle_call(_Request, _From, State) -> {noreply, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info({nodeup, Node}, State) -> rabbit_log:info("node ~p up", [Node]), {noreply, State}; handle_info({nodedown, Node}, State) -> rabbit_log:info("node ~p down", [Node]), %% TODO: This may turn out to be a performance hog when there are %% lots of nodes. We really only need to execute this code on %%*one* node, rather than all of them. ok = rabbit_networking:on_node_down(Node), ok = rabbit_amqqueue:on_node_down(Node), {noreply, State}; handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. %%--------------------------------------------------------------------
成功开源项目中可挖掘的东西真是多,特别是学习一个新东西的时候,需要从广泛涉猎一些开源项目,开阔眼界,积累知识,继续!
2012-11-4 17:57:57更新 主动断开节点
Erlang : Disconnect Unwanted Nodes
(mathu@mathu)> (mathu@mathu)> nodes(). [mathu9@mathu,mathu1@mathu,mathu2@mathu,mathu3@mathu, mathu4@mathu,mathu5@mathu,mathu6@mathu,mathu7@mathu, mathu15@mathu,mathu12@mathu,mathu14@mathu,mathu11@mathu, mathu10@mathu,mathu13@mathu,mathu8@mathu,mathu9@mathu] (mathu@mathu)>
Having unwanted nodes connected is overhead for the system. Therefore the bellow one-liner will help to make it clean.
[net_kernel:disconnect(X) || X <- nodes() -- [List_of_wanted_nodes]].
(mathu@mathu)> (mathu@mathu)> nodes(). [mathu9@mathu,mathu1@mathu,mathu2@mathu,mathu3@mathu, mathu4@mathu,mathu5@mathu,mathu6@mathu,mathu7@mathu, mathu15@mathu,mathu12@mathu,mathu14@mathu,mathu11@mathu, mathu10@mathu,mathu13@mathu,mathu8@mathu,mathu9@mathu] (mathu@mathu)>
与其后面踢掉不如限制可以链接的节点:
allow(Nodes) -> ok | error Types: Nodes = [node()] Limits access to the specified set of nodes. Any access attempts made from (or to) nodes not in Nodes will be rejected. Returns error if any element in Nodes is not an atom.
余锋有一篇文章专门介绍: Erlang如何限制节点对集群的访问之net_kernel:allow http://blog.yufeng.info/archives/1752
本文转自:http://www.cnblogs.com/me-sa/archive/2011/07/12/erlang0005.html