php-amqp中的“套接字错误:无法连接到主机”异常

时间:2014-03-07 10:14:53

标签: php rabbitmq php-amqp

我写了一个函数来检查rabbitmq是否正在运行。

function getBrokerStatus()
{
    log_message("info", "Checking if broker is running....");
    try {
        $amqpConnection = new AMQPConnection();
        $amqpConnection->setLogin("guest");
        $amqpConnection->setPassword("guest");
        $amqpConnection->setVhost("/");
        $amqpConnection->connect();
    } catch (Exception $e) {
        log_message("info", "Exception: " . $e->getMessage());
        return false;
    }

    if (!$amqpConnection->isConnected()) {
        log_message("info", "Cannot connect to the broker! It might not be running");
        return false;
    }
    $amqpConnection->disconnect();
    return true;
}

我的代码捕获了此异常。我在下面的日志中看到了 -

Exception: Socket error: could not connect to host.

但是我的rabbitmq服务器正在运行那么为什么我得到这个例外?我正在使用rabbitmq-server的v3.1.1

1 个答案:

答案 0 :(得分:0)

如果您无法在计算机上ping本地主机,则必须是您未设置主机文件。 在/ etc / hosts

中编辑它