标签:
为了避免污染宿主系统环境,于是在虚拟机中搭建了一个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.cnblogs.com/jeryl/p/5606383.html