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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…