rabbitmq链接超时_远程连接RabbitMQ失败

为了避免污染宿主系统环境,于是在虚拟机中搭建了一个linux环境并且按照了rabbitmq-server。然后在远程连接的时候一直连接失败。

官网上面给的例子都是在本地使用系统默认的guest用户连接的。没有给出远程连接的例子,于是阅读文档发现:

When the server first starts running, and detects that its database is uninitialised or has been deleted, it initialises a fresh database with the following resources:

a virtual host named /

a user named guest with a default password of guest, granted full access to the / virtual host.

也就是刚刚安装好rabbitmq-server,系统会自动创建一个名为“/”的virtual host,同时也会创建一个用户名和密码都是guest的用户,并且应用"/ virtual host"的所有访问权限。

因此在rabbitmq安装的机器上使用官网给出的例子:

import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Connection;import com.rabbitmq.client.Channel;public class Send {

private final static String QUEUE_NAME = "hello";  public static void main(String[] argv) throws Exception {

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("localhost");

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

String message = "Hello World!";

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

System.out.println(" [x] Sent '" + message + "'");

channel.close();

connection.close();

}

}

运行是没问题的。如果要切换到远程机器访问的话,单纯的修改

factory.setHost("localhost");

是不行的。

因为guest用户只是被容许从localhost访问。官网文档描述如下:

"guest" user can only connect via localhost

By default, the guest user is prohibited from connecting to the broker remotely; it can only connect over a > loopback interface (i.e. localhost). This applies both to AMQP and to any other protocols enabled via plugins. Any > other users you create will not (by default) be restricted in this way.

This is configured via the loopback_users item in the configuration file.

If you wish to allow the guest user to connect from a remote host, you should set the loopback_users configuration item to []. A complete rabbitmq.config which does this would look like:

[{rabbit, [{loopback_users, []}]}].

默认情况下,使用下面的命令:

sudo rabbitmqctl environment

会发现:

{default_permissions,[<>,<>,<>]},

{default_user,<>},

{default_user_tags,[administrator]},

{default_vhost,<>},

{loopback_users,[<>]},

{tcp_listeners,[5672]},

我这快不想使用默认的guest用户,我新建立了一个用户rollen,然后授予所有权限,使用下面的命令:

rabbitmqctl add_user rollen root

rabbitmqctl set_user_tags rollen administrator

rabbitmqctl set_permissions -p / rollen ".*" ".*" ".*"

然后使用下面的代码远程访问

package com.rollenholt.rabbitmq.example1;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;public class Send {    private final static String QUEUE_NAME = "hello";    public static void main(String[] argv) throws Exception {

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("192.168.126.131");

factory.setUsername("rollen");

factory.setPassword("root");

factory.setPort(5672);

Connection connection = factory.newConnection();

Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

String message = "Hello World!";

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

System.out.println(" [x] Sent '" + message + "'");

channel.close();

connection.close();

}

}

参考文档

==============================================================================

我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他

们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其

实我是一个程序员

==============================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值