目录
异常详情
Exception in thread "main" java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:129)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:125)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:147)
at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:439)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:65)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:160)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1216)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1173)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1131)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1294)
at com.test.demo.rabbitmq.Producer.main(Producer.java:20)
Caused by: com.rabbitmq.client.ShutdownSignalException: connection error; protocol method: #method<connection.close>(reply-code=530, reply-text=NOT_ALLOWED - vhost / not found, class-id=10, method-id=40)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:502)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:293)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:141)
... 8 more
问题分析
-
错误代码:
530
- RabbitMQ 使用的错误代码,表示虚拟主机未找到。
-
错误消息:
NOT_ALLOWED - vhost / not found
- 表明客户端试图连接的虚拟主机
/
(通常表示根虚拟主机)在 RabbitMQ 中不存在。
- 表明客户端试图连接的虚拟主机
-
class-id=10
和method-id=40
:- 这些是 AMQP 协议中的标识符,
class-id=10
和method-id=40
相关联的 AMQP 方法是connection.close
,表示连接被关闭。
- 这些是 AMQP 协议中的标识符,
可能原因
-
虚拟主机未创建:
- 客户端尝试连接的虚拟主机在 RabbitMQ 实例中不存在。虚拟主机需要在 RabbitMQ 中显式创建,才能被使用。
-
虚拟主机名称错误:
- 客户端配置中的虚拟主机名称可能拼写错误或不匹配实际创建的虚拟主机名称。
-
权限问题:
- 即使虚拟主机存在,如果客户端的用户没有权限访问该虚拟主机,也会遇到类似的问题。
解决方案
检查和创建虚拟主机:
列出现有虚拟主机:
root@rabbitmq:/# rabbitmqctl list_vhosts
输出结果
Listing vhosts ...
name
all
root@rabbitmq:/#
在这里我们明显可以看到虚拟主机仅有一个all,而代码中并未声明如下:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(ip);
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("123456");
ConnectionFactory中virtualHost默认为/ ,因此在进行创建连接配置时,未能找到虚拟主机/ 而引起报错。
创建虚拟主机
root@rabbitmq:/# rabbitmqctl add_vhost /
Adding vhost "/" ...
root@rabbitmq:/# rabbitmqctl list_vhosts
Listing vhosts ...
name
all
/
检查用户权限
方式1:查看所有用户权限
root@rabbitmq:/# rabbitmqctl list_permissions
Listing permissions for vhost "/" ...
user configure write read
admin .* .* .*
root@rabbitmq:/#
方式2:查看指定用户权限
root@rabbitmq:/# rabbitmqctl list_user_permissions admin
Listing permissions for user "admin" ...
vhost configure write read
all .* .* .*
/ .* .* .*
检查无问题后进行验证一切正常,如果不想使用指令则可以通过管理控台的admin栏位进行手动添加。
方式二:
此方式限定在可正常登录控制台的场景下