无法使用java客户端访问docker for windows中的rabbitmq

kadbb459  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(235)

我有一个运行rabbitmq的docker容器。我可以访问web管理(http://192.168.0.33:15672/)进行帐户测试。但是我想用java客户端不能访问,抛出访问被拒绝的异常。
我的docker编写文件

version: '3'
services:
  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代码

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class Recv {

    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        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]警告com.rabbitmq.client.impl.宽恕异常处理程序-线程“main”com.rabbitmq.client.authenticationfailureexception中发生意外的连接驱动程序错误(异常消息:连接重置)异常:拒绝访问\u-使用普通身份验证机制拒绝登录。有关详细信息,请参阅代理日志文件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题