连接拒绝尝试使用 Spring 从 Cloud Foundry 上的 RabbitMQ 队列读取

Connection refused trying to read from RabbitMQ Queue on Cloud Foundry using Spring

我正在尝试使用 RabbitMQ posting 来自一个应用程序的消息并在另一个应用程序中接收它们。这些应用程序托管在 Cloud Foundry 上,并且都绑定到同一个 RabbitMQ 实例。

我能够 post 从第一个应用程序向队列发送消息,但是在我的第二个应用程序中,它使用 @RabbitListener 和 @RabbitHandler 来监听这个队列,我收到一个连接被拒绝的错误。不过它在我的本地 RabbitMQ 实例上运行良好。

Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: errno: 111 (Connection refused), error: Connection refused (local port 45596 to address 127.0.0.1 (localhost), remote port 5672 to address 127.0.0.1 (localhost))

这是完整的日志。

我观察到,远程端口保持不变,仍然是 5672,但本地端口不断从一个日志更改到另一个日志。我不确定这些端口是从哪里获取的,因为我认为 Spring 应该为我处理这个,而且因为我的第一个应用程序有类似的设置,它似乎工作正常。

这是我的配置的样子-

@Bean
        public MessageConverter jsonMessageConverter(){
            return new Jackson2JsonMessageConverter();
        }

        @Bean
        public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
            return new RabbitAdmin(connectionFactory);
        }

        @Bean
        public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
            RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
            rabbitTemplate.setMessageConverter(jsonMessageConverter());
            return rabbitTemplate;
        }

        @Bean
        public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory rabbitConnectionFactory) {
            SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
            container.setConnectionFactory(rabbitConnectionFactory);
            container.setQueueNames("queue2911");
            container.setAutoStartup(false);
            //container.setMessageListener(exampleListener());
            return container;
        }

如有任何帮助,我们将不胜感激。

操作系统选择本地端口。

您尝试连接到 localhost 的事实意味着您使用的是默认连接工厂 (localhost:5672) 而不是启动配置的连接工厂。

为两个应用打开 DEBUG 日志记录并比较自动配置报告。