[Erlang 0005] 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

 
When you use same cookie for all nodes in a distributed Erlang system, you might have probably seen that all the nodes are interconnected with each other.

(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)3> [net_kernel:disconnect(X) || X <- nodes() -- [mathu3@mathu, mathu4@mathu]].
[true,true,true,true,true,true,true,true,true,true,true,
 true,true]
(mathu@mathu)4> nodes().
[mathu3@mathu,mathu4@mathu]
(mathu@mathu)5> 

- Mathuvathanan Mounasmay

 

与其后面踢掉不如限制可以链接的节点:

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

目录
相关文章
|
4月前
|
NoSQL JavaScript 前端开发
如何使用 Node.js 连接和操作 MongoDB 数据库?
如何使用 Node.js 连接和操作 MongoDB 数据库?
466 2
|
9天前
|
SQL JavaScript 数据库
sqlite在Windows环境下安装、使用、node.js连接
sqlite在Windows环境下安装、使用、node.js连接
|
26天前
|
JavaScript 关系型数据库 MySQL
node连接mysql,并实现增删改查功能
【8月更文挑战第26天】node连接mysql,并实现增删改查功能
42 3
|
25天前
|
JavaScript NoSQL 前端开发
|
27天前
|
缓存 NoSQL 网络协议
【Azure Redis 缓存】Redisson 连接 Azure Redis出现间歇性 java.net.UnknownHostException 异常
【Azure Redis 缓存】Redisson 连接 Azure Redis出现间歇性 java.net.UnknownHostException 异常
|
1月前
|
Oracle 关系型数据库
Navicat 连接Oracle ORA-28547: connection to server failed, probable Oracle Net admin error
Navicat 连接Oracle ORA-28547: connection to server failed, probable Oracle Net admin error
64 0
|
3月前
|
JavaScript 前端开发 关系型数据库
Node.js 连接 MySQL
Node.js 连接 MySQL
39 5
|
3月前
|
JavaScript NoSQL 数据库连接
使用Nodejs + express连接数据库mongoose
【6月更文挑战第3天】这篇文章介绍了如何在Express应用中使用Mongoose连接MongoDB数据库。首先,需要创建一个`db.js`文件,然后通过`npm install mongoose`安装Mongoose驱动。接着,在应用中引入MongoDB模块,建立到数据库的连接。创建一个Mongoose schema定义数据模型,如用户信息表。最后,执行数据库操作,包括查询、插入、更新和删除文档,并在完成后关闭数据库连接。文中还提供了相关代码示例。
175 1
|
3月前
|
NoSQL JavaScript MongoDB
Node.js 连接 MongoDB
Node.js 连接 MongoDB
33 0
|
4月前
|
Linux Windows
FinalShell连接Linux虚拟机报错java.net.ConnectException: Connection timed out: connect(亲测有效)
FinalShell连接Linux虚拟机报错java.net.ConnectException: Connection timed out: connect(亲测有效)
739 0