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

How to specify custom host for connection in RabbitMQ?

如果我的应用有 receive.exs,我如何在 send.exs 文件中指定我的自定义主机在某处举办?我有一个带有 send.exs 的长生不老药应用程序和另一个带有 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"
...

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

The first,您正在使用,不带任何参数,并使用一些默认设置(本地主机、"guest" 用户名、"guest" 密码等)。

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

The third form takes a well-formed RabbitMQ URI 作为字符串,由主机、端口、用户名、密码和虚拟主机组合而成。请注意,其中一些值在 URI 中是可选的。