当前位置: 首页 > 知识库问答 >
问题:

如何用rabbitmq docker从spring启动在rabbitmq中创建队列

令狐辉
2023-03-14

我花了整整一天的时间来尝试让spring-AMQP示例项目在docker版本的RabbitMQ上运行。我只是在运行标准的rabbitmq Docker。虽然我没有连接问题,但我总是得到与创建队列相关的异常,并且我已经尝试了所有可能的变体,在这一点上。

我尝试在我的配置中声明队列,就像示例项目一样。我尝试显式配置RabbitAdmin。我已经尝试显式配置整个自动配置混乱。我在rabbitmq中创建了一个新用户,并显式分配了权限。我尝试只使用队列名而不声明队列bean。我已经尝试将queues和RabbitAdmin注入到其他bean中,只是为了强制创建它们。

无论我尝试什么,我都会得到以下错误:

2017-04-24 17:42:19.709  WARN 37360 --- [cTaskExecutor-1] o.s.a.r.listener.BlockingQueueConsumer   : Failed to declare queue:"incoming"
2017-04-24 17:42:19.715  WARN 37360 --- [cTaskExecutor-1] o.s.a.r.listener.BlockingQueueConsumer   : Queue declaration failed; retries left=3

org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):["incoming"]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:621) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:520) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1382) [spring-rabbit-1.7.1.RELEASE.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50) ~[amqp-client-4.0.2.jar:4.0.2]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:955) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at com.sun.proxy.$Proxy77.queueDeclarePassive(Unknown Source) ~[na:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:600) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    ... 3 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue '"incoming"' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:229) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:117) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 12 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue '"incoming"' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:505) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:336) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:143) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 1 common frames omitted

实际上,很难确定它是在没有注意到它没有创建队列之后连接到队列失败,还是在初始创建时失败。

在任何特定于docker的文档中都没有提到当在docker容器内运行时需要显式创建队列,并且在容器内进行了挖掘,我发现根本没有配置,我认为这意味着应该允许它创建队列。

这也不是因为列出队列会显示一个空列表,所以这不是与我试图配置的队列不匹配的队列配置的问题。

为了使所有内容都完全显式化,我使用了以下配置类:

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableRabbit
public class Application {

  @Bean
  @ConfigurationProperties("spring.datasource")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }

  @Autowired
  public ConnectionFactory connectionFactory;

  @Bean
  public RabbitAdmin rabbitAdmin() {
    return new RabbitAdmin(connectionFactory);
  }

  @Bean
  public Queue incomingQueue() {
    Queue queue = new Queue("${ep.service.baseline.listen.rabbitq.name}", true, false, false, null);
    return queue;
  }

  @Bean
  public Queue outboundQueue() {
    return new Queue("${ep.service.baseline.send.rabbitq.name}", true, false, false, null);
  }

  @Bean
  TopicExchange exchange() {
    return new TopicExchange("default-exchange", true, false);
  }

  @Bean
  Binding incomingBinding(@Qualifier("incomingQueue") Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(queue.getName());
  }
  @Bean
  Binding outboundBinding(@Qualifier("outboundQueue") Queue queue, TopicExchange exchange) {
    return BindingBuilder.bind(queue).to(exchange).with(queue.getName());
  }
  public static void main(String[] args) throws Exception {

    SpringApplication.run(Application.class, args);
  }
}

并且我的侦听器声明如下:

@RabbitListener(queues = "${ep.service.baseline.listen.rabbitq.name}")
@Component
public class IncomingMessageProcessor {
  private final Logger log = LoggerFactory.getLogger(IncomingMessageProcessor.class);

  @RabbitHandler
  public void process(@Payload String msg) {
    log.info("received message: {}", msg);
  }
}

并且收到以下错误:

2017-04-24 18:06:54.551 ERROR 38157 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.amqp.rabbit.config.internalRabbitListenerEndpointRegistry'; nested exception is org.springframework.amqp.AmqpIllegalStateException: Fatal exception on listener startup
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:879) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.ep.service.baseline.Application.main(Application.java:65) [classes/:na]
Caused by: org.springframework.amqp.AmqpIllegalStateException: Fatal exception on listener startup
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doStart(SimpleMessageListenerContainer.java:846) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.start(AbstractMessageListenerContainer.java:552) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry.startIfNecessary(RabbitListenerEndpointRegistry.java:279) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry.start(RabbitListenerEndpointRegistry.java:235) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 14 common frames omitted
Caused by: org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:548) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1382) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):["incoming"]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:621) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:520) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    ... 2 common frames omitted
Caused by: java.io.IOException: null
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:123) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:992) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:50) ~[amqp-client-4.0.2.jar:4.0.2]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:955) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    at com.sun.proxy.$Proxy97.queueDeclarePassive(Unknown Source) ~[na:na]
    at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:600) ~[spring-rabbit-1.7.1.RELEASE.jar:na]
    ... 3 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue '"incoming"' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:229) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:117) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 12 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue '"incoming"' in vhost '/', class-id=50, method-id=10)
    at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:505) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:336) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:143) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:90) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:634) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47) ~[amqp-client-4.0.2.jar:4.0.2]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:572) ~[amqp-client-4.0.2.jar:4.0.2]
    ... 1 common frames omitted

rabbit的原木是这样的:

=INFO REPORT==== 24-Apr-2017::23:19:55 ===
accepting AMQP connection <0.917.0> (172.17.0.1:42196 -> 172.17.0.3:5672)

=INFO REPORT==== 24-Apr-2017::23:19:55 ===
Connection <0.917.0> (172.17.0.1:42196 -> 172.17.0.3:5672) has a client-provided name: rabbitConnectionFactory#0

=INFO REPORT==== 24-Apr-2017::23:19:55 ===
connection <0.917.0> (172.17.0.1:42196 -> 172.17.0.3:5672 - rabbitConnectionFactory#0): user 'guest' authenticated and granted access to vhost '/'

=ERROR REPORT==== 24-Apr-2017::23:19:57 ===
Channel error on connection <0.917.0> (172.17.0.1:42196 -> 172.17.0.3:5672, vhost: '/', user: 'guest'), channel 1:
operation queue.declare caused a channel exception not_found: no queue '"incoming"' in vhost '/'

=ERROR REPORT==== 24-Apr-2017::23:20:02 ===
Channel error on connection <0.917.0> (172.17.0.1:42196 -> 172.17.0.3:5672, vhost: '/', user: 'guest'), channel 1:
operation queue.declare caused a channel exception not_found: no queue '"incoming"' in vhost '/'

共有1个答案

颜熙云
2023-03-14

这与docker无关,也与经纪人的位置无关。

您的队列名周围似乎有一些虚假的“”...

reply-code=404,reply-text=not_found-vhost“/”中没有队列“”传入“”

(请注意'中的)。

此属性ep.service.baseline.listen.rabbitq.name中有什么内容?

还有,这也行不通...

Queue queue = new Queue("${ep.service.baseline.listen.rabbitq.name}", true, false, false, null);

不能在那里使用属性占位符。这将创建一个名为${ep.service.baseline.listen.rabbitq.name}的队列--rabbit的命名规则非常宽松。

你需要用...

@Value("${ep.service.baseline.listen.rabbitq.name}")
private String queueName;

@Bean
public Queue incomingQueue() {
    Queue queue = new Queue(this.queueName, true, false, false, null);
    return queue;
}

调试日志始终是您的朋友;您将看到记录的所有队列声明。

 类似资料:
  • 我在 RabbitMQ 中创建新队列时遇到了一些问题。我只创建一个使用者客户端,该客户端将使用来自另一个微服务的消息。 这是我到目前为止所做的。 应用程序属性: 配置类: 和侦听器类: 当我运行这个程序时,我有一条ACCESS_REFUSED消息,但我不知道为什么。我错过了什么吗?? 谢谢

  • 问题内容: 根据我对Rabbit-mq的(有限的)经验,如果您为尚不存在的队列创建新的侦听器,则会自动创建该队列。我试图将Spring AMQP项目与rabbit- mq一起使用来设置侦听器,但是却出现错误。这是我的xml配置: 我在RabbitMq日志中得到了这个: 还有来自AMQP的类似错误: 从堆栈跟踪中可以看出,队列是在“被动”模式下创建的-任何人都可以指出我如何不使用被动模式来创建队列,

  • 我有两个独立的应用程序,一个是生产者,一个是具有交换类型默认(DIRECT)的消费者。 下面是带有死信队列设置的Rabbit MQ Producer的配置。 现在,这里是单独应用程序中Rabbit MQ listener的代码。 现在,当我运行消费者服务时,它会创建一个同名的重复队列。如下图所示 Rabbit MQ GUI 我也在消费者中尝试了以下设置,但结果相同 注意:当我在生产者中添加死信队列

  • 问题内容: 我正在尝试使用Spring Reactor 3组件和Spring Integration从JMS队列创建反应式流(Flux)。 我正在尝试从JMS队列(使用Spring Integration的ActiveMQ)创建客户端的响应流(Spring Reactor 3 Flux),以使客户端异步获取JMS消息。我相信我已经正确连接了所有东西,但是在服务器停止之前,客户端不会收到任何JMS消

  • 我收到了一个关于这个问题的建议,一个评论,说我可能没有我需要的交换,因为我的队列已经存在。于是,我就全部手动删除了。 但是,在重新部署应用程序时,我发现所有队列都有以下异常: 我不确定这意味着什么,因为我显然想要创建队列<code>myInput。组,它似乎在抱怨它不存在。。。 我也检查了这个问题,我的问题可能是权限。但我应该有它们——否则我相信我无法删除队列...... 你能给我一个解决方案吗?

  • 有什么方法可以声明惰性加载队列吗?