遠程連接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();
}
}