简体   繁体   中英

RabbitMq connection refused: connect

I'm dealing with a connection issue in rabbitmq. I've load an image of it in docker who's running as you can see here: 在此处输入图像描述

Then in Spring I've create this config class to create a binding and register a queue:

@Configuration
public class MQConfig {

    @Value("${people.queue}")
    public String queue;

    @Value("${people.exchange}")
    public String exchange;

    @Value("${people.routingkey}")
    public List<String> routingKeys;

    @Bean
    public Queue queue() {
        return new Queue(queue, true, false, false);
    }

    @Bean
    Exchange myExchange() {
        return ExchangeBuilder.topicExchange(exchange).durable(true).build();
    }

    @Bean
    Declarables bindings(TopicExchange exchange, Queue queue) {
        return new Declarables(routingKeys.stream()
                .map(key -> BindingBuilder
                        .bind(queue)
                        .to(exchange)
                        .with(key))
                .collect(Collectors.toList()));
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public ConnectionFactory connectionFactory() throws IOException {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setPort(5672);
        connectionFactory.setHost("localhost");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        //connectionFactory.setPublisherReturns(true);

        return connectionFactory;
    }
}

After running the app I get the following message in the terminal:

2022-08-01 16:35:27.083  INFO 27852 --- [  restartedMain] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
2022-08-01 16:35:31.172  INFO 27852 --- [  restartedMain] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect

I don't know where I am failing to connect, please some help?

Thanks

It connected. All I have to do was to configure both ports before running the image in docker. It seems that running it by default never started the default ports, so once I set them manually it run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM