简体   繁体   中英

Spring boot is not able to connect to rabbitmq ruuning inside docker container..while from browser i can access rabbitmq console

"I used the following command to create rabbit-mq contaier"

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:15672 rabbitmq:3-management

"so As per my knowledge any conncetion from localhost:8080 will be redirected to the container 15672, where rabbit-mq is running.Even after starting the container i can able to open rabbit-mq with localhost:8080"

"But my spring boot application is not able to connect to localhost:8080" "Here is the error i am getting"

2020-08-14 20:17:01.300 ERROR 15508 --- [:0:0:0:0:1:8080] c.r.c.impl.ForgivingExceptionHandler     : An unexpected connection driver error occured

java.net.SocketException: Socket Closed
    at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_261]
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_261]
    at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_261]
    at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_261]
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_261]
    at java.io.BufferedInputStream.read(BufferedInputStream.java:265) ~[na:1.8.0_261]
    at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288) ~[na:1.8.0_261]
    at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:184) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:665) ~[amqp-client-5.9.0.jar:5.9.0]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_261]

2020-08-14 20:17:01.307 ERROR 15508 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.amqp.AmqpTimeoutException: java.util.concurrent.TimeoutException] with root cause

java.util.concurrent.TimeoutException: null
    at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:502) ~[amqp-client-5.9.0.jar:5.9.0]
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:326

"following is my application.properties file"

spring.rabbitmq.host = localhost
spring.rabbitmq.port = 8080
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest
server.port=8081

"Following is my configuration file"

package com.example.connecttorabbitmqdocker.configuration;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    
    
    @Value("${javainuse.rabbitmq.queue}")
    String queueName;

    @Value("${javainuse.rabbitmq.exchange}")
    String exchange;

    @Value("${javainuse.rabbitmq.routingkey}")
    private String routingkey;

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

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(exchange);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(routingkey);
    }

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

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

"Please give any suggestions you have"

Only management interface of RabbitMQ lives under the port 15672 . You can access AMQP protocol through port 5672 . You can do:

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:5672 rabbitmq:3-management

and see how it connects.

RabbitMQ is access via port 5672 and management interface through 15672. So both these port shoudl be exposed to access the Queue as well as the Management interface. You can execute below command to run the container:

docker run -d --hostname my-rabbit --name some-rabbit -p 8080:5672 -p 8081:15672 rabbitmq:3-management

Java Application would access rabbitmq via 8080 (spring.rabbitmq.port = 8080) and the management interface can be accessed via 8081.

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