无法连接到服务器上的 RabbitMQ

Unable to connect to RabbitMQ on server

我有以下代码连接到我本地机器上的 RabbitMQ,但是当我将主机名从 localhost 更改为我的服务器名时,它失败并且 returns 错误

var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "localhost";

当我如下更改 HostName 后,returns 错误

factory.HostName = "myserver";

Exception: None of the specified endpoints were reachable

The AMQP operation was interrupted: AMQP close-reason, initiated by Library, code=0, text=\"End of stream\", classId=0, methodId=0, cause=System.IO.EndOfStreamException: Peer missed 2 heartbeats with heartbeat timeout set to 60 seconds

在服务器上,主机名似乎配置不同。我的管理员查看了日志并查看了配置,并为我提供了服务器上的主机名。

var factory = new ConnectionFactory();
factory.UserName = "myuser";
factory.Password = "mypassword";
factory.VirtualHost = "/filestream";
factory.Port = AmqpTcpEndpoint.UseDefaultPort;
factory.HostName = "myserver";

与使用这种方式连接相比,使用连接字符串连接更容易,就像使用 sql.

一样

示例 C#:

var factory = new ConnectionFactory
{
    Uri = ConfigurationManager.ConnectionStrings["RabbitMQ"].ConnectionString,
    RequestedHeartbeat = 15,
    //every N seconds the server will send a heartbeat.  If the connection does not receive a heartbeat within
    //N*2 then the connection is considered dead.
    //suggested from http://public.hudl.com/bits/archives/2013/11/11/c-rabbitmq-happy-servers/
    AutomaticRecoveryEnabled = true
};
return factory.CreateConnection();

web.config 或 app.config

  <connectionStrings>
    <add name="RabbitMQ" connectionString="amqp://{username}:{password}@{servername}/{vhost}" />
  </connectionStrings>