赞
踩
目录:
1.简介
2.部署RabbitMQ
3.启动RabbitMQ
4.实操消费
5.Docker搭建RabbitMQ集群
该实例为了巩固docker知识
MQ全称Message Queue 消息队列,RabbitMQ是基于AMQP(高级消息队列协议)实现的。消息队列通常用以应用之间相互通信,解决同步问题,在项目中用到该消息队列,但是环境在服务器上(各种跳板),有时候自己测试不方便,故用docker搭建RabbitMQ本地环境,方便测试调试.
后续有关RabbitMQ的学习和深入就基于这个容器实操.
1、查询rabbitmq镜像
docker search rabbitmq:management
2、拉取rabbitmq镜像
docker pull rabbitmq:management
3.1.创建并启动容器
docker run -d --hostname my-rabbit --name rabbit -p 8080:15672 rabbitmq:management
ps:
--hostname:指定容器主机名称
--name:指定容器名称
-p:将mq端口号映射到本地
3.2备选启动同时设置用户和密码
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
ps: 此处的端口访问是有区别的,控制台端口用于管理rabbitmq,应用访问端口号为rabbitclient等应用访问。
15672:控制台端口号
5672:应用访问端口号
3.3查看rabbitmq运行状况:
docker logs rabbit
3.4 访问
http://localhost:15672
3.5 登陆
默认账号
Account : guest
Password : guest
启动Rabbit
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
ps:所用user>>> admin/admin
添加pom.xml依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
</dependencies>
发送端(运行发送端,连续发送两条信息到指定队列)
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.util.concurrent.TimeoutException; /** * @Auther: chenyj * @Date: 2018/12/4 10:58 * @Description: */ public class Sender { //队列名称 private final static String QUEUE_NAME = "hello"; public static void main(String[] argv)throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); //Account factory.setUsername("admin"); //Password factory.setPassword("admin"); //rabbit所在服务器主机名 factory.setHost("192.168.209.128"); //应用访问端口 factory.setPort(5672); //指定RabbitMQ服务器的虚拟主机 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(); } }
UI查看
ps: 方才发送了两条信息
消费端
import com.rabbitmq.client.*; import java.io.IOException; /** * @Auther: chenyj * @Date: 2018/12/4 11:05 * @Description: */ public class Receiver { //队列名称 private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); //Account factory.setUsername("admin"); //Password factory.setPassword("admin"); //rabbit所在服务器主机名 factory.setHost("192.168.209.128"); //应用访问端口 factory.setPort(5672); //指定RabbitMQ服务器的虚拟主机 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. "); 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); } }
运行上述消费者,控制台输出
Waiting for messages.
[x] Received 'Hello World!'
[x] Received 'Hello World!'
此时Rabbit页面已经全部消费成功
暂时没该需求,如有用到的小伙伴可以参考一下博客
https://blog.csdn.net/belonghuang157405/article/details/83540148
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。