首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在erlang应用程序中启动多个webmachine实例?

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

Stack Overflow用户
提问于 2014-03-12 00:23:52
回答 1查看 376关注 0票数 0

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

代码语言:javascript
复制
 {
    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)运行应用程序之后,我的应用程序启动良好。

以下是具体的错误:

代码语言:javascript
复制
=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应用程序中有不同的配置。

谢谢你对这个问题的任何帮助/讨论!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 13:56:09

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

代码语言:javascript
复制
{Name,
 {webmachine_mochiweb, start, [WebConfig]},
  permanent, 5000, worker, [mochiweb_socket_server]}

其中,NameWebConfig必须是您的and机器实例所特有的。WebConfig部件应该包括namedispatch_group属性。例如:

代码语言:javascript
复制
WebConfig = [{name,instance1},
             {dispatch_group,instance1},
             {ip, Ip},
             {port, Port},
             {log_dir, "priv/log"},
             {dispatch, Dispatch}],

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

代码语言:javascript
复制
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模块的名称,您的代码将与它发生冲突并导致各种问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22339530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档