主要参考此博文:https://blog.csdn.net/weixin_39617052/article/details/79723849
1、docker 及 虚拟机 启动
2、$ docker info (查看 docker 信息)主要看下 镜像仓库是否是阿里的
3、$ docker search rabbitmq:management ( 查询 management 版本的 rabbitmq ) 此版本有 管理界面
4、 $ docker pull rabbitmq:management ( 拉取 management 版本的 rabbitmq )拉取最好指定版本 不然会拉取 latest 版本
5、$ docker run -d --hostname my-rabbit --name rabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p 5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
( 启动 同时 重置管理界面 账号 密码 ) 管理页面 默认账号:guest 默认密码:guest
-d: 后台运行容器,并返回容器ID
--hostname:指定容器主机名称
--name:指定容器名称
-p:将mq端口号映射到本地 格式为:主机(宿主)端口:容器端口
扩展:( 参考 https://blog.csdn.net/weixin_33889245/article/details/91907299 )
6、 http://192.168.99.100:15672/ 登录 管理页面
7、 消息 生产者 消费者 代码
package com.ll.scdemo01.rabbit;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.concurrent.TimeoutException;
public class Sender {
private final static String QUEUE_NAME = "hello1";
public static void main(String[] argv) throws java.io.IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("admin");
factory.setPassword("admin");
factory.setHost("192.168.99.100");
factory.setPort(5672);
factory.setVirtualHost("/");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent ‘" + message + "‘");
channel.close();
connection.close();
}
}
package com.ll.scdemo01.rabbit;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer {
private final static String QUEUE_NAME = "hello";
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("admin");
factory.setPassword("admin");
factory.setHost("192.168.99.100");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setConnectionTimeout(600000); // in milliseconds
factory.setRequestedHeartbeat(60); // in seconds
factory.setHandshakeTimeout(6000); // in milliseconds
factory.setRequestedChannelMax(5);
factory.setNetworkRecoveryInterval(500);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Waiting for messages. ");
com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received ‘" + message + "‘");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.1.2</version>
</dependency>
docker 从阿里仓 拉取rabbitMq镜像 及部署 使用(环境:win10-家庭版-Docker Toolbox)