如何在RabbitMQ中指定自定义主机进行连接?

时间:2016-03-16 15:03:44

标签: rabbitmq elixir phoenix-framework

如果我的应用 receive.exs 托管在某个地方,如何在 send.exs 文件中指定自定义主机?我有一个带有 send.exs 的elixir应用程序和另一个带有 receive.exs 的应用程序,它是Phoenix应用程序并且是托管的。

send.exs

{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.publish(channel, "", "hello", msg)
IO.puts " [x] Sent everything"
AMQP.Connection.close(connection)

receive.exs

...
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.consume(channel, "hello", nil, no_ack: true)
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"
...

1 个答案:

答案 0 :(得分:0)

Connection.open有几种不同的形式。

你正在使用的

The first不带参数,并使用一些默认设置(localhost," guest" username," guest" password等)。

The second采用关键字选项列表,包括host,port,virtual_host等。您可以使用Connection.open(host: your_host)。您未提供的任何设置都将使用默认设置。

The third表格采用格式正确的RabbitMQ URI作为字符串,它是使用主机,端口,用户名,密码和虚拟主机的组合形成的。请注意,其中一些值在URI中是可选的。