To avoid contaminating the host system environment, a Linux environment was built in the virtual machine and followed rabbitmq-server
. Then the connection fails at the remote connection.
The examples given on the official website are local users connected using the system default guest
. Without giving an example of a remote connection, read the document discovery:
When the server first starts running, and detects the IT database is uninitialised or have 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.
That is, the rabbitmq-server
system automatically creates a virtual host named "/" and creates a user with a user name and password guest
, and applies all access rights of "/virtual host".
So use the official website example on the RABBITMQ installed machine:
Import Com.rabbitmq.client.ConnectionFactory;Import com.rabbitmq.client.Connection;Import Com.rabbitmq.client.Channel;PublicClassSend {PrivateFinalstatic String queue_name ="Hello";PublicStaticvoid 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 (null, Message.getbytes ()); System.out.println ( "[x] Sent '" + message + "'"); Channel.close (); Connection.close (); }}
The operation is no problem. If you want to switch to remote machine access, simply modify the
factory.setHost("localhost");
It's not going to work.
Because guest
the user is only allowed to localhost
access from. The official documents are described below:
"Guest" user can only connect via localhost
By default, the guest user was 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 won't (by default) is restricted in the this.
This was configured via the Loopback_users item in the configuration file.
If you wish 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, []}]}].
By default, the following command is used:
sudo rabbitmqctl environment
Will find:
{default_permissions,[<<".*">>,<<".*">>,<<".*">>]}, {default_user,<<"guest">>}, {default_user_tags,[administrator]}, {default_vhost,<<"/">>}, {loopback_users,[<<"guest">>]}, {tcp_listeners,[5672]},
I'm not going to use the default guest
user, I've created a new user rollen
, and then granted all the permissions, use the following command:
set_user_tags rollen administratorrabbitmqctl set_permissions -p / rollen ".*" ".*" ".*"
Then use the following code to remotely access
Package com.rollenholt.rabbitmq.example1;Import Com.rabbitmq.client.Channel;Import com.rabbitmq.client.Connection;Import Com.rabbitmq.client.ConnectionFactory;PublicClassSend {PrivateFinalstatic String queue_name ="Hello";PublicStaticvoidMain(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 (); }}
Reference documents
- Http://www.rabbitmq.com/access-control.html
Remote Connection RABBITMQ failed