如何在erlang应用程序中启动多个webmachine实例?

时间:2014-03-12 00:23:52

标签: erlang webmachine

我正在尝试在一个erlang应用程序中创建两个webmachine实例。每个实例都在不同的端口上运行,并具有自己的特定配置。在webmachine doc here之后,我在我的supervisor规范(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 doc,我们应该能够启动同一个应用程序的两个实例,但在erlang应用程序中使用不同的配置。

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

1 个答案:

答案 0 :(得分:0)

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

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

其中NameWebConfig必须对您的webmachine实例唯一。 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判断,您可能已经运行了webmachine ./scripts/new_webmachine.sh并将应用程序名称指定为application。如果是这样,请不要这样做,因为application是密钥Erlang OTP模块的名称,并且您的代码将与其发生冲突并导致各种问题。