解决RabbitMQ远程不能访问的问题
刚刚安装的RabbitMQ-Server-3.3.5,并且也已经开启了Web管理功能,但是现在存在一个问题:
出于安全的考虑,guest这个默认的用户只能通过http://localhost:15672 来登录,不能使用IP地址登录,也就是不能远程访问,这对于服务器上没有安装桌面的情况是无法管理维护的。
要解决这个问题需要配置远程登录权限,这里通过配置文件来实现远程访问。
这里主要介绍Unix和Windows的配置文件修改。
一、Windows
Windows环境下默认配置文件为目录/%RabbitMQ Server%/rabbitmq_server-3.3.5/etc下的rabbitmq.config.example文件,我们可以直接在这个文件中修改(可以不用去设置环境变量了),也可以自己再新建一个rabbitmq.config文件,然后把这个文件路径配置到环境变量中,这里介绍就直接修改rabbitmq.config.example文件。
注意:修改之前,需要先停止RabbitMQ服务!!不然是不能保存的!!!
配置之前需要先添加用户,用于外网的访问,可以使用命令行来实现添加用户,需要在RabbitMQ的安装目录sbin目录下执行:
root@master:/opt/rabbitmq_server-3.3.5/sbin$ rabbitmqctl add_user admin admin
也可以通过web管理页面来添加用户和密码,使用guest登录web管理页面http://localhost:15672,进入“admin”标签页,然后点击“Add a user ”,输入对用的帐号密码,然后选择用户角色(一定要选择):
为了授权该用户对VirtualHost"/" 的访问,用户添加之后,需要对该用户进行授权,不然运行会出现错误:
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""}
详细错误日志为:
java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:766) at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:61) at com.asiainfo.mq.rabbitmq.rabbitmqtest.SendTest.main(SendTest.java:29) Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""} at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) ... 3 more Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; reason: {#method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'hello' in vhost '/' refused for user 'admin', class-id=50, method-id=10), null, ""} at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:473) at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:313) at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:533)
操作过程为:在Admin标签页下点击新增的用户"admin",进入授权页面,默认直接点击"set permission"即可:
用户以及授权添加完成之后,在rabbitmq.config.example文件中,添加以下内容,保存后重启RabbitMQ服务:
…… [ {rabbit, [%% %% Network Connectivity %% ==================== %% %% By default, RabbitMQ will listen on all interfaces, using %% the standard (reserved) AMQP port. %% {tcp_listeners, [5672]}, {loopback_users, ["admin"]}, …… ]} ].
在浏览器中输入http://192.168.0.124:15672实现通过IP地址访问,成功登录:
测试用例见博文“RabbitMQ远程调用测试用例”
二、Unix
unix环境下,配置文件在%/rabbitmq_server-3.3.5%/etc/rabbitmq/rabbitmq.config.example
root@master:/opt/rabbitmq_server-3.3.5/etc/rabbitmq# vi rabbitmq.config.example
%% -*- mode: erlang -*- %% ---------------------------------------------------------------------------- %% RabbitMQ Sample Configuration File. %% %% See http://www.rabbitmq.com/configure.html for details. %% ---------------------------------------------------------------------------- [ {rabbit, [%% %% Network Connectivity %% ==================== %% %% By default, RabbitMQ will listen on all interfaces, using %% the standard (reserved) AMQP port. %% {tcp_listeners, [5672]}, {loopback_users, ["admin"]}, …… ]} ].
参考地址:
http://blog.haohtml.com/archives/15249
http://www.rabbitmq.com/access-control.html
https://www.rabbitmq.com/configure.html
博客地址: http://www.cnblogs.com/dwf07223,本文以学习、研究和分享为主,欢迎转载,转载请务必保留此出处。若本博文中有不妥或者错误处请不吝赐教。 |