使用rebar3时,如何设置httpc的配置文件的配置选项?
这是唯一通过erl -config inets.config出现的example,如下所示:

[{inets,
[{services,[{httpc,[{profile, server1}]},
            {httpc, [{profile, server2}]}]}]
}].
我尝试将其应用于rebar3项目结构。

该项目是使用rebar3创建的,具有标准的OTP布局:
rebar3 new release myapp
这是我的myapp/config/sys.config:
[
  { myapp, []},
  {inets, [{services, [{httpc, [{profile, myapp}]}]}]}
].
rebar.config:
{erl_opts, [debug_info]}.
{deps, []}.

{relx, [{release, { myapp, "0.1.0" },
         [myapp,
          sasl]},

        {sys_config, "./config/sys.config"},
        {vm_args, "./config/vm.args"},

        {dev_mode, true},
        {include_erts, false},

        {extended_start_script, true}]
}.

{profiles, [{prod, [{relx, [{dev_mode, false},
                            {include_erts, true}]}]
            }]
}.
为了完整性,这是我的myapp.app.src文件:
{application, myapp,
 [{description, "An OTP application"},
  {vsn, "0.1.0"},
  {registered, []},
  {mod, { myapp_app, []}},
  {applications,
   [kernel,
    stdlib
   ]},
  {env,[]},
  {modules, []},

  {maintainers, []},
  {licenses, []},
  {links, []}
 ]}.
要求
这是我要从rebar`s shell发出的请求:
$ ./rebar3 shell
1> ===> Booted myapp
1> ===> Booted sasl
...
1> httpc:request( "http://reddit.com", myapp).
** exception exit: {noproc,
                    {gen_server,call,
                     [httpc_myapp,
                      {request,
                       {request,undefined,<0.88.0>,0,http,
                        {"reddit.com",80},
                        "/",[],get,
                        {http_request_h,undefined,"keep-alive",undefined,
                         undefined,undefined,undefined,undefined,undefined,
                         undefined,...},
                        {[],[]},
                        {http_options,"HTTP/1.1",infinity,true,
                         {essl,[]},
                         undefined,false,infinity,...},
                        "http://reddit.com",[],none,[],1478280329839,
                        undefined,undefined,false}},
                      infinity]}}
     in function  gen_server:call/3 (gen_server.erl, line 212)
     in call from httpc:handle_request/9 (httpc.erl, line 574)
这是没有配置文件的请求,用于检查inets是否确实有效:
2> httpc:request( "http://reddit.com").

=PROGRESS REPORT==== 4-Nov-2016::13:25:51 ===
          supervisor: {local,inet_gethost_native_sup}
             started: [{pid,<0.107.0>},{mfa,{inet_gethost_native,init,[[]]}}]

=PROGRESS REPORT==== 4-Nov-2016::13:25:51 ===
          supervisor: {local,kernel_safe_sup}
             started: [{pid,<0.106.0>},
                       {id,inet_gethost_native_sup},
                       {mfargs,{inet_gethost_native,start_link,[]}},
                       {restart_type,temporary},
                       {shutdown,1000},
                       {child_type,worker}]
{ok,{{"HTTP/1.1",200,"OK"},...

最佳答案

rebar3本身使用inets http客户端,因此在Shell中启动应用程序时,inets已启动并配置。一种解决方法是在应用程序启动之前停止inets,即as It's suggested by rebar3 developer(复制如下)。另一个是在控制台模式下引导您的发行版:

./_build/default/rel/myapp/bin/myapp console

除此之外,您的项目还有另一个问题。您尚未告诉您要为您启动inets。您应该在myapp.src中包含以下行:
{applications, [kernel, stdlib, inets]}

或者,您可以在rebar.config发布部分中列出inets,以告诉relx该应用程序应包含在发布中并在启动时启动。
{relx, [{release, { myapp, "0.1.0" }, [inets, myapp, sasl]} ]}

在rebar3 shell启动时阻止加载Inets

这是Fred barbert从rebar3邮件列表中获得的full answer的副本:

我们确实需要inets来获取软件包,并且可能不会将其关闭
自动针对所有用例,因为这可能会损害一般用法
如果用户应用程序不使用rebar3代理,
inets,但仍要求在后续运行中获取软件包。的
解决方法,我建议使用钩子脚本。钩
脚本在引导用户应用程序之前运行,并且是常规脚本:
#!/usr/bin/env escript
main(_) -> application:stop(inets).

然后,您可以使用以下命令将脚本挂钩:
{shell, [{script_file, "path/to/file"}]}

在rebar3.config中,或
rebar3 shell --script_file test/check_env.escript

关于erlang - 如何使用rebar3配置httpc配置文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40291335/

10-12 23:07