IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

Erlang epmd的角色以及使用

Erlang非业余研究 2011-10-23 21:28:02 累计浏览 3,163 次
本机暂存
    很多同学误会了epmd的作用,认为epmd就是erlang集群的协议,我来澄清下:

    Epmd是Erlang Port Mapper Daemon的缩写,在Erlang集群中的作用相当于dns的作用,提供节点名称到端口的查询服务,epmd绑定在总所周知的4369端口上。

    epmd文档:这里

     epmd协议:这里

    Erlang的节点名称是类似这样的foo@ip的格式,当一个节点启动的时候,首先会在本机启动epmd,同时把自己的节点名称和节点监听的tcp端口登记在上面。

     看代码:

// erlexec.c
...
case 'n':
                    if (strcmp(argv[i], "-name") == 0) { /* -name NAME */
                        if (i+1 >= argc)
                            usage("-name");
                        /*
                         * Note: Cannot use add_args() here, due to non-defined
                         * evaluation order.
                         */
                        add_arg(argv[i]);
                        add_arg(argv[i+1]);
                        isdistributed = 1;
                        i++;
...
case 's':     /* -sname NAME */
                    if (strcmp(argv[i], "-sname") == 0) {
                        if (i+1 >= argc)
                            usage("-sname");
                        add_arg(argv[i]);
                        add_arg(argv[i+1]);
                        isdistributed = 1;
                        i++;
                    }
...
    if (isdistributed && !no_epmd)
        start_epmd(epmd_prog);
...

    我们可以透过erl -epmd EPMD_PROG来传入不同的参数。

    再看下实验:

$erl -sname a
$erl -sname b
$erl -sname c
$ ps -ef|grep epmd
membase   4592     1  0 Aug25 ?        00:00:39 /usr/local/bin/epmd -daemon
...
$ netstat -an|grep 4369
tcp        0      0 0.0.0.0:4369                0.0.0.0:*                   LISTEN
$ epmd -names
epmd: up and running on port 4369 with data:
name c at port 4096
name b at port 4097
name a at port 4098
...
$erl -sname x
Erlang R14B04 (erts-5.8.5) 1 [64-bit] [smp:16:16] [rq:16] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5  (abort with ^G)
(x@my031091)1> erl_epmd:port_please(x, "127.0.0.1").
{port,34625,5}
(x@my031091)2> erl_epmd:names().
{ok,[{"a",4096},{"b",4097},{"c",4098},{"x",34625}]}

    

    kernel的erl_epmd模块提供epmd协议的封装.

     那么如果要连接其他节点的时候,就取出节点名称的ip部分,透过erl_epmd建立连接到ip:4369,通过epmd协议来查询想要的foo的端口,然后再用ip:port去连接真正的服务。

    新版本的epmd提供了强行移除名称的功能,避免由于erlang虚拟机由于某种原因crash,没有注销名字,导致无法再使用这个名字。

     要使用stop功能,epmd必须以 -relaxed_command_check 启动,具体参考epmd -help

     演示下:

$ erl -sname x -epmd "epmd -relaxed_command_check -daemon"
Erlang R14B04 (erts-5.8.5) 1 [64-bit] [smp:16:16] [rq:16] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.8.5  (abort with ^G)
(x@my031089)1> 
$ epmd -names
epmd: up and running on port 4369 with data:
name x at port 58953
$ epmd -stop x
STOPPED
$ epmd -names
epmd: up and running on port 4369 with data:

    我们看到名称已经抢先移除成功。

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. gen_tcp发送进程被挂起起因分析及对策 (累计阅读 37,823)
  2. Twitter/微博客的学习摘要 (累计阅读 12,262)
  3. 面试题 – 为什么我的朋友圈不见了? (累计阅读 11,954)
  4. Zookeeper研究和应用 (累计阅读 9,485)
  5. 分布式哈希和一致性哈希 (累计阅读 8,815)
  6. 面试IT业界顶尖企业所应该知道的10道题(1) (累计阅读 8,527)
  7. 在百度的第一年 (累计阅读 6,922)
  8. Memcache分布式部署方案 (累计阅读 6,818)
  9. 大数据下的工行 (累计阅读 6,644)
  10. ZooKeeper管理员指南——部署与管理ZooKeeper (累计阅读 6,592)