---
title: "erlang使用长名启动的分析与解决"
author: "lanyuflying"
date: "2014-06-12T12:50:06Z"
tags: []
categories: ["编程语言"]
slug: "erlang"
draft: false
lastmod: "2014-06-12T12:50:06Z"
id: 2078725
url: "https://lanyuflying.iteye.com/blog/2078725"
---

erlang作为一种函数式编程语言，得到了越来越多的应用。在学习分布式编程的过程中，在本机使用长名启动报错，信息如下：

       {error\_logger,{{2014,6,12},{10,56,2}},"Can't set long node name!\\nPlease check your configuration\\n",\[]}  
{error\_logger,{{2014,6,12},{10,56,2}},crash\_report,\[\[{initial\_call,{net\_kernel,init,\['Argument\_\_1']}},{pid,&lt;0.20.0&gt;},{registered\_name,\[]},{error\_info,{exit,{error,badarg},\[{gen\_server,init\_it,6,\[{file,"gen\_server.erl"},{line,322}]},{proc\_li  
b,init\_p\_do\_apply,3,\[{file,"proc\_lib.erl"},{line,239}]}]}},{ancestors,\[net\_sup,kernel\_sup,&lt;0.10.0&gt;]},{messages,\[]},{links,\[&lt;0.17.0&gt;]},{dictionary,\[{longnames,true}]},{trap\_exit,true},{status,running},{heap\_size,610},{stack\_size,27},{reducti  
ons,784}],\[]]}  
{error\_logger,{{2014,6,12},{10,56,2}},supervisor\_report,\[{supervisor,{local,net\_sup}},{errorContext,start\_error},{reason,{'EXIT',nodistribution}},{offender,\[{pid,undefined},{name,net\_kernel},{mfargs,{net\_kernel,start\_link,\[\[cat,longnames]]}  
},{restart\_type,permanent},{shutdown,2000},{child\_type,worker}]}]}  
{error\_logger,{{2014,6,12},{10,56,2}},supervisor\_report,[{supervisor,{local,kernel\_sup}},{errorContext,start\_error},{reason,{shutdown,{failed\_to\_start\_child,net\_kernel,{'EXIT',nodistribution}}}},{offender,[{pid,undefined},{name,net\_sup},{mf  
args,{erl\_distribution,start\_link,\[]}},{restart\_type,permanent},{shutdown,infinity},{child\_type,supervisor}]}]}  
{error\_logger,{{2014,6,12},{10,56,2}},crash\_report,\[\[{initial\_call,{application\_master,init,\['Argument\_\_1','Argument\_\_2','Argument\_\_3','Argument\_\_4']}},{pid,&lt;0.9.0&gt;},{registered\_name,\[]},{error\_info,{exit,{{shutdown,{failed\_to\_start\_child,n  
et\_sup,{shutdown,{failed\_to\_start\_child,net\_kernel,{'EXIT',nodistribution}}}}},{kernel,start,\[normal,\[]]}},\[{application\_master,init,4,\[{file,"application\_master.erl"},{line,133}]},{proc\_lib,init\_p\_do\_apply,3,[{file,"proc\_lib.erl"},{line,23  
9}]}]}},{ancestors,\[&lt;0.8.0&gt;]},{messages,\[{'EXIT',&lt;0.10.0&gt;,normal}]},{links,\[&lt;0.8.0&gt;,&lt;0.7.0&gt;]},{dictionary,\[]},{trap\_exit,true},{status,running},{heap\_size,376},{stack\_size,27},{reductions,117}],\[]]}  
{error\_logger,{{2014,6,12},{10,56,2}},std\_info,\[{application,kernel},{exited,{{shutdown,{failed\_to\_start\_child,net\_sup,{shutdown,{failed\_to\_start\_child,net\_kernel,{'EXIT',nodistribution}}}}},{kernel,start,\[normal,\[]]}}},{type,permanent}]}  
{"Kernel pid terminated",application\_controller,"{application\_start\_failure,kernel,{{shutdown,{failed\_to\_start\_child,net\_sup,{shutdown,{failed\_to\_start\_child,net\_kernel,{'EXIT',nodistribution}}}}},{kernel,start,\[normal,\[]]}}}"}

Crash dump was written to: erl\_crash.dump  
Kernel pid terminated (application\_controller) ({application\_start\_failure,kernel,{{shutdown,{failed\_to\_start\_child,net\_sup,{shutdown,{failed\_to\_start\_child,net\_kernel,{'EXIT',nodistribution}}}}},{k

 

 

  其中的提示信息  Can't set long node name!\\nPlease check your configuration没有明确说是什么原因。结合搜索到的一些信息，和群友提供的答案，结合书本，基本上确定此问题和域名解析有关，情况大致如下：

     1.使用-sname时，默认是本地节点，所以，不需要考虑网络交互，也就不存在域名解析问题。

     2.使用-name启动时，系统认为需要通过网络交互，那么，在这个时候，不管是局域网还是广域网，都会涉及到域名解析的问题。

          使用 erl  -name  nodename 启动时，默认会使用机器名作域名进行解析请求，此时肯定解析是无法成功的，所以报错。知道了问题原因，那么解决起来就会有途径可以遵循了。

 

        解决方法有多个，分别如下：

          （1）使用erl   -name [nodename@127.0.0.1启动，](mailto:nodename@127.0.0.1%E5%90%AF%E5%8A%A8%EF%BC%8C%E6%AD%A4%E6%97%B6%EF%BC%8C)

                   这种方法，借用了127.0.0.1是指向本机的约定来解决此问题的。因为已经提供了具体的ip指向，所以这个时候，不再需要解析了。所以会成功。这个方法是erlang群的群友提供的，在此表示感谢。

          （2）和方法1类似，但是用本机的真实ip来处理：erl -name nodename@本机ip地址。

                   该方法和方法一的原理是一样的，当然，这个地址是真实的，不是模拟的，这个很重要。所以就逻辑性而言，这个方法要更好一些。

          （3） 直接使用机器名：erl -name [nodename@hostname](mailto:nodename@hostname)

                    由于在这里直接指明了机器名，也就是本机的机器名，所以在这里也不存在解析问题了。问题得到了解决。在这里有个问题：也就是完整长名时的处理流程和只有nodename的长名时的区别在哪里？这个貌似只有看源代码才能回答

           （4）使用host文件解析：这个方法是最好的方法，是适合用在部署环境中，因为此时，机器名也并无必要通过dns系统解析。

            ok，问题解决了。那么问题是在最常规的-name nodename 时，erlang使用什么来请求dns但是没有结果呢？  在我的出问题环境中，机器名是 hostname,但是 host文件中只有localhost的配置。所以，此时应当是使用了机器名，去请求dns解析，没有解析成功而导致的。而在使用[nodename@hostname](mailto:nodename@hostname)时，应该在发现hostname是本机后，就没有再去解析了。