RabbitMQ报错:Failed to declare queue(s)

错误描述及原因

项目启动时 RabbitMQ 报错了,控制台打印一大堆就不贴了,问题的源头是:

1
Failed to declare queue(s):[你的消息队列名]

也就是说声明队列失败了。

通常发生这种错误的原因有两个:

  • 程序确实没有声明队列 bean
  • 程序中声明了,但 RabbitMQ 的 user 权限不足,因此失败

解决方案

针对第一种情况,在程序中声明队列 bean 即可。如下:

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
@Bean
public Queue queue() {
return new Queue("mail.welcome", true);
}
}

针对第二种情况,需要配置 RabbitMQ 的 user 权限。可以使用命令行,也可以在 RabbitMQ 的浏览器页面中配置 localhost:15672/#/users。步骤如下图所示:

Snipaste_2022-09-09_21-14-46

Snipaste_2022-09-09_21-21-28

这里的权限配置是正则表达式,我报错是因为本该设置为 .*,却错误地设置为了 '.*',自然就识别错误了。重新设置一下即可。

曾梦想仗剑走天涯,后来没钱就没去