远程连接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,<<"guest">>},
  {default_user_tags,[administrator]},
  {default_vhost,<<"/">>},
  {loopback_users,[<<"guest">>]},
  {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();
    }
}

参考文档

  • http://www.rabbitmq.com/access-control.html


==============================================================================
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员

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


你可能感兴趣的:(远程连接RabbitMQ失败)