简体   繁体   English

通过 IP 地址而不是 localhost 连接到 RabbitMQ 时连接被拒绝

[英]Connection refused when connecting to RabbitMQ via IP address instead of localhost

I'm trying to set up a RabbitMQ server (version 3.7.4, installed via homebrew on macOS 10.13.4) such that my colleagues can work with it via the internal network.我正在尝试设置 RabbitMQ 服务器(版本 3.7.4,通过 macOS 10.13.4 上的自制软件安装),以便我的同事可以通过内部网络使用它。 We are using the official Java client ( com.rabbitmq:amqp-client:5.2.0 ).我们正在使用官方 Java 客户端 ( com.rabbitmq:amqp-client:5.2.0 )。 Unfortunately, they only get a ConnectException with the message Connection refused :不幸的是,他们只收到一个ConnectException消息, Connection refused

java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:134)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:997)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:956)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:914)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1068)
    at my.own.private.RabbitMqConnector.connect(RabbitMqConnector.java:29)

We already set up another user account (not guest ) which has admin privileges.我们已经设置了另一个具有管理员权限的用户帐户(不是guest )。 With almost the same Java code I can connect locally (setting the host to localhost ) but not if I use my IP address.使用几乎相同的 Java 代码,我可以在本地连接(将主机设置为localhost ),但如果我使用我的 IP 地址则不行。 According to the RabbitMQ docs, the RabbitMQ server binds to all network interfaces by default (otherwise I would have said that it's a problem with address binding).根据 RabbitMQ 文档,RabbitMQ 服务器默认绑定到所有网络接口(否则我会说这是地址绑定的问题)。 I disabled my firewall so that shouldn't be the problem.我禁用了我的防火墙,所以这应该不是问题。

Here's the respective code:这是相应的代码:

final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername("username");
connectionFactory.setPassword("password");
connectionFactory.setHost("10.10.33.12");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");

try (final Connection connection = connectionFactory.newConnection()) {
    // doo stuff with the connection
}

I finally found the actual problem and it's not related to Java or how I configured the ConnectionFactory .我终于找到了实际问题,它与 Java 或我如何配置ConnectionFactory无关。 It was indeed a problem how the RabbitMQ package for homebrew is configured.如何配置自制软件的RabbitMQ包确实是一个问题。 In the rabbitmq-env.conf file, there was the following settingrabbitmq-env.conf文件中,有以下设置

NODE_IP_ADDRESS=127.0.0.1

This setting caused RabbitMQ to only listen on 127.0.0.1 for incoming connections.此设置导致 RabbitMQ 仅在 127.0.0.1 上侦听传入连接。 Once I removed the setting and restarted the service, I could connect from other machines as well.删除设置并重新启动服务后,我也可以从其他机器连接。

Thank you all for your help!谢谢大家的帮助!

The issue is not with your code.问题不在于您的代码。 I would suggest you to check such things:我建议您检查以下内容:

  • RabbitMQ Server is up and running RabbitMQ 服务器已启动并正在运行
  • Your custom user can access virtual host "/"您的自定义用户可以访问虚拟主机“/”
  • Check firewall settings if any, probably it's refusing connection with remote host检查防火墙设置(如果有),可能是拒绝与远程主机连接

One of these should be the cause, hope it helps!其中之一应该是原因,希望它有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM