Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

在docker中安装rabbitmq,代码无法通过ip访问,web管理平台可以通过ip访问登录

docker-compose配置

  rabbitmq:
    image: rabbitmq:3.8.9-management
    container_name: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    hostname: my-rabbit
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: admin

pom

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.10.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.30</version>
</dependency>

java访问代码

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.0.33");
factory.setPort(5672);
factory.setUsername("test");
factory.setPassword("test");
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
    String message = new String(delivery.getBody(), "UTF-8");
    System.out.println(" [x] Received '" + message + "'");
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });

错误信息

[AMQP Connection 192.168.0.33:5672] WARN com.rabbitmq.client.impl.ForgivingExceptionHandler - An unexpected connection driver error occured (Exception message: Connection reset)
Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
    at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:385)
    at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:64)
    at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:156)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1130)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1087)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1045)
    at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1207)
    at Recv.main(Recv.java:17)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以使用容器的links
docker-composer可以这样写
external_links:

  • rabbitmq:rabbitmq
  • etcd:etcd
  • redis:redis
  • mysql:mysql

比如在你的代码中访问redis 就不需要写IP了。
redis:6379就可以了


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...