Mac环境下RabbitMq安装与测试教程
2022/4/6 23:26:05
本文主要是介绍Mac环境下RabbitMq安装与测试教程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
https://www.cnblogs.com/yihuihui/p/9095130.html
Mac环境下RabbitMq安装与测试教程
RabbitMq安装与测试教程
Installing on Mac
I. 安装
1 5 9 |
brew install rabbitmq ## 进入安装目录 cd /usr/local/Cellar/rabbitmq/3.7.5 # 启动 brew services start rabbitmq # 当前窗口启动 rabbitmq-server |
启动控制台之前需要先开启插件
1 |
./rabbitmq-plugins enable rabbitmq_management |
进入控制台: http://localhost:15672/
用户名和密码:guest,guest
II. 配置与测试
1. 添加账号
首先是得启动mq
1 5 |
## 添加账号 ./rabbitmqctl add_user admin admin ## 添加访问权限 ./rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*" ## 设置超级权限 ./rabbitmqctl set_user_tags admin administrator |
2. 编码实测
pom引入依赖
1 |
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> </dependency> |
开始写代码
1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 |
public class RabbitMqTest { //消息队列名称 private final static String QUEUE_NAME = "hello"; @Test public void send() throws java.io.IOException, TimeoutException { //创建连接工程 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setPort(5672); factory.setUsername("admin"); factory.setPassword("admin"); //创建连接 Connection connection = factory.newConnection(); //创建消息通道 Channel channel = connection.createChannel(); //生成一个消息队列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); for (int i = 0; i < 10; i++) { String message = "Hello World RabbitMQ count: " + i; //发布消息,第一个参数表示路由(Exchange名称),未""则表示使用默认消息路由 channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } //关闭消息通道和连接 channel.close(); connection.close(); } @Test public void consumer() throws java.io.IOException, java.lang.InterruptedException, TimeoutException { //创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setPort(5672); factory.setUsername("admin"); factory.setPassword("admin"); //创建连接 Connection connection = factory.newConnection(); //创建消息信道 Channel channel = connection.createChannel(); //消息队列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); System.out.println("[*] Waiting for message. To exist press CTRL+C"); AtomicInteger count = new AtomicInteger(0); //消费者用于获取消息信道绑定的消息队列中的信息 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"); try { System.out.println(" [x] Received '" + message); } finally { System.out.println(" [x] Done"); channel.basicAck(envelope.getDeliveryTag(), false); } } }; channel.basicConsume(QUEUE_NAME, false, consumer); Thread.sleep(1000 * 60); } } |
需要注意的一点是:
- 生产消息:
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
- 消费消息:
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
- 生产和消费都声明channel,要求两者的配置参数一致,否则无法消费数据
3. 输出说明
首先执行塞入数据,执行完毕之后,可以到控制台进行查看:
可以看到多出了一个Queue,对列名为hello,总共有10条数据
接下来就是消费数据了,执行consumer方法,输出日志
1 5 9 13 17 21 |
[*] Waiting for message. To exist press CTRL+C [x] Received 'Hello World RabbitMQ count: 0 [x] Done [x] Received 'Hello World RabbitMQ count: 1 [x] Done [x] Received 'Hello World RabbitMQ count: 2 [x] Done [x] Received 'Hello World RabbitMQ count: 3 [x] Done [x] Received 'Hello World RabbitMQ count: 4 [x] Done [x] Received 'Hello World RabbitMQ count: 5 [x] Done [x] Received 'Hello World RabbitMQ count: 6 [x] Done [x] Received 'Hello World RabbitMQ count: 7 [x] Done [x] Received 'Hello World RabbitMQ count: 8 [x] Done [x] Received 'Hello World RabbitMQ count: 9 [x] Done |
回头去查看queue,发现总得数据量为0了
4. ACK问题
对于ack的问题,如果在消费数据的时候,出现异常,而我不希望数据丢失,这个时候就需要考虑手动ack的机制来保证了
首先需要设置手动ack
1 |
// 设置autoAck为false channel.basicConsume(QUEUE_NAME, false, consumer); |
其次在消费数据完毕之后,主动ack/nack
1 5 |
if (success) { channel.basicAck(envelope.getDeliveryTag(), false); } else { channel.basicNack(envelope.getDeliveryTag(), false, false); } |
这篇关于Mac环境下RabbitMq安装与测试教程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16在电脑上怎么模拟手机的运行环境?-icode9专业技术文章分享
- 2024-11-16接收socket数据,莫名其妙socket就关闭了是怎么回事?-icode9专业技术文章分享
- 2024-11-16ts nightly是什么?-icode9专业技术文章分享
- 2024-11-16如何升级vscode版本?-icode9专业技术文章分享
- 2024-11-16如何设置vscode默认的node版本?-icode9专业技术文章分享
- 2024-11-16shell 如何创建一个文件夹?-icode9专业技术文章分享
- 2024-11-16useReducer案例详解:从零开始理解与应用
- 2024-11-15聊聊用LangChain4J构建聊天机器人的那些事儿
- 2024-11-15LangChain 和 LlamaIndex 在检索增强生成(RAG)中的大比拼:全面对比评测
- 2024-11-15平台工程不只是配置管理:超越CFEngine的方法