【发布时间】:2020-04-30 15:13:58
【问题描述】:
将 Spring-Boot 与 RabbitMQ 结合使用,我正在尝试创建一个可以有 n 个队列的交换器,每个微服务一个,因此每个微服务都会收到相同的消息。
Producer 微服务定义了 Fanout Exchange。 每个 Consumer 微服务都会创建一个队列并尝试将其连接到 Producer 交换器
当Producer首先启动时,exchange被创建。启动消费者微服务绑定到生产者交换。但是,如果消费者微服务首先启动,它们将不会绑定,因为还没有任何东西可以绑定到此日志:
2020-01-13 22:24:49.640 INFO [,,,] 88649 --- [ main] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [localhost:5672]
2020-01-13 22:24:49.685 INFO [,,,] 88649 --- [ main] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#7746ae18:0/SimpleConnection@428ea503 [delegate=amqp://guest@127.0.0.1:5672/, localPort= 62282]
2020-01-13 22:24:49.726 ERROR [,,,] 88649 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'abc-exchange' in vhost '/', class-id=50, method-id=20)
2020-01-13 22:24:50.748 ERROR [,,,] 88649 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'abc-exchange' in vhost '/', class-id=50, method-id=20)
2020-01-13 22:24:52.754 ERROR [,,,] 88649 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'abc-exchange' in vhost '/', class-id=50, method-id=20)
2020-01-13 22:24:56.763 ERROR [,,,] 88649 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'abc-exchange' in vhost '/', class-id=50, method-id=20)
2020-01-13 22:25:01.794 ERROR [,,,] 88649 --- [ 127.0.0.1:5672] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'abc-exchange' in vhost '/', class-id=50, method-id=20)
2020-01-13 22:25:01.807 INFO [,,,] 88649 --- [ main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.io.IOException
2020-01-13 22:25:01.859 DEBUG [,,,] 88649 --- [ main] .b.c.i.c.AppConfig$CustomHttpTraceFilter : Filter 'httpTraceFilter' configured for use
如何配置消费者微服务(或生产者)以尝试将队列绑定到生产者交换,即使它们在交换存在之前就已启动。
另一种方法是生产者根据启动的消费者微服务信息动态创建队列,然后监听给定队列。但是问题仍然存在,如果队列没有足够快地创建或者 Consumer 在 Producer 之前创建,那么监听器将抛出异常
【问题讨论】:
标签: java spring spring-boot rabbitmq microservices