0

我正在尝试在一个 erlang 应用程序中创建两个 webmachine 实例。每个实例都将在不同的端口上运行,并具有自己的特定配置。在此处的 webmachine 文档之后,我在主管规范 (application_sup.erl) 中添加了以下要启动的流程:

 {
    webmachine_instance_1,
    { webmachine_mochiweb, start, 
       [
          [
              { ip, "0.0.0.0"},
              { port, 8000},
              { dispatch, [ {["*"], file_resource, []} ] }
          ] 
        ]
    },
    permanent,
    5000,
    worker,
    dynamic
 },
 {
    webmachine_instance_2,
    { webmachine_mochiweb, start, 
       [
          [
              { ip, "0.0.0.0"},
              { port, 8080},
              { dispatch, [ {["*"], file_resource, []} ] }
          ] 
       ]
    },
    permanent,
    5000,
    worker,
    dynamic
 }

当我包含这两个实例时,我得到一个启动错误并且无法启动我的 erlang 应用程序。在尝试使用单个 webmachine 实例(webmachine_instance_1 或 webmachine_instance_2)运行应用程序后,我的应用程序启动正常。

这是具体的错误:

=PROGRESS REPORT==== 11-Mar-2014::17:00:31 ===
      supervisor: {local, application_sup}
         started: [{pid,<0.230.0>},
                   {name,webmachine_instance_1},
                   {mfargs,
                       {webmachine_mochiweb,start,
                           [[{ip,"0.0.0.0"},
                             {port,8000},
                             {dispatch, [{['*'],
                                   file_resource,
                                   []
                             }]}]
                            ]
                        }
                   },
                   {restart_type,permanent},
                   {shutdown,5000},
                   {child_type,worker}] 

 =SUPERVISOR REPORT==== 11-Mar-2014::17:00:31 ===
     Supervisor: {local, application_sup}
     Context:    start_error
     Reason:     {'EXIT',
                 {undef,
                     [{webmachine_mochiweb,start,
                          [{ip,"0.0.0.0"},
                           {port,8080},
                           {dispatch,[{["*"],file_resource,[]}]}],
                          []},
                      {supervisor,do_start_child,2,
                          [{file,"supervisor.erl"},{line,303}]},
                      {supervisor,start_children,3,
                          [{file,"supervisor.erl"},{line,287}]},
                      {supervisor,init_children,2,
                          [{file,"supervisor.erl"},{line,253}]},
                      {gen_server,init_it,6,
                          [{file,"gen_server.erl"},{line,304}]},
                      {proc_lib,init_p_do_apply,3,
                          [{file,"proc_lib.erl"},{line,227}]}]}}
 Offender:   [{pid,undefined},
              {name,webmachine_instance_2},
              {mfargs,
                  {webmachine_mochiweb,start,
                      [{ip,"0.0.0.0"},
                       {port,8080},
                       {dispatch,[{["*"],file_resource,[]}]}]}},
              {restart_type,permanent},
              {shutdown,5000},
              {child_type,worker}]

我对 erlang 相当陌生,可能不太了解这里的潜在问题 - 根据 webmachine 文档,我们应该能够启动同一个应用程序的两个实例,但在一个 erlang 应用程序中具有不同的配置。

感谢您对此问题的任何帮助/讨论!

4

1 回答 1

0

您的子进程配置应具有以下形式:

{Name,
 {webmachine_mochiweb, start, [WebConfig]},
  permanent, 5000, worker, [mochiweb_socket_server]}

您的网络机器实例在哪里Name并且WebConfig必须是唯一的。该WebConfig部分应包括namedispatch_group属性。例如:

WebConfig = [{name,instance1},
             {dispatch_group,instance1},
             {ip, Ip},
             {port, Port},
             {log_dir, "priv/log"},
             {dispatch, Dispatch}],

因此,对于多个实例,您的主管规范可能会有类似的内容:

WebConfig1 = [{name,instance1},
              {dispatch_group,instance1},
              {ip, Ip},
              {port, Port},
              {log_dir, "priv/log"},
              {dispatch, Dispatch}],
WebConfig2 = [{name,instance2},
              {dispatch_group,instance2},
              {ip, Ip},
              {port, Port+1},
              {log_dir, "priv/log"},
              {dispatch, Dispatch}],
Web1 = {instance1,
        {webmachine_mochiweb, start, [WebConfig1]},
        permanent, 5000, worker, [mochiweb_socket_server]},
Web2 = {instance2,
        {webmachine_mochiweb, start, [WebConfig2]},
        permanent, 5000, worker, [mochiweb_socket_server]},
Processes = [Web1, Web2],
{ok, { {one_for_one, 10, 10}, Processes} }.

另一件事:从application_sup您的问题中出现的名称来看,您可能已经运行了网络机器./scripts/new_webmachine.sh并将应用程序名称指定为application. 如果是这样,请不要这样做,因为application它是关键 Erlang OTP 模块的名称,您的代码会与它发生冲突并导致各种问题。

于 2014-03-12T13:56:09.433 回答