爲了不污染宿主系統環境,因而在虛擬機中搭建了一個linux環境而且按照了rabbitmq-server
。而後在遠程鏈接的時候一直鏈接失敗。html
官網上面給的例子都是在本地使用系統默認的guest
用戶鏈接的。沒有給出遠程鏈接的例子,因而閱讀文檔發現:linux
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:app
a virtual host named /
a user named guest with a default password of guest, granted full access to the / virtual host.tcp
也就是剛剛安裝好rabbitmq-server
,系統會自動建立一個名爲「/」的virtual host,同時也會建立一個用戶名和密碼都是guest
的用戶,而且應用"/ virtual host"的全部訪問權限。oop
所以在rabbitmq安裝的機器上使用官網給出的例子:this
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(); } }
運行是沒問題的。若是要切換到遠程機器訪問的話,單純的修改rest
factory.setHost("localhost");
是不行的。code
由於guest
用戶只是被允許從localhost
訪問。官網文檔描述以下:server
"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.htm
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(); } }