1.rabbitmq的安装使用
1.yum安装即可
yum install erlang rabbitmq-server -y
2.启动rabbitmq服务端
systemctl start rabbitmq-server
3.配置rabbitmq,创建管理用户以及后台管理页面
sudo rabbitmqctl add_user pengpeng 123
4.给新用户,设置管理员角色
sudo rabbitmqctl set_user_tags pengpeng administrator
5.给与这个用户,对所有的队列,可读可写
语法:set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
sudo rabbitmqctl set_permissions -p "/" pengpeng ".*" ".*" ".*"
6.添加rabbtimq管理界面
rabbitmq-plugins enable rabbitmq_management
7.访问mq的管理界面
http://123.206.16.61:15672/
服务器id:15672
8.登录rabbitmq服务端
9.练习rabbitmq的消息生产消费
生产者代码.py
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='水续传')
channel.basic_publish(exchange='',
routing_key='水续传',
body='大郎 起来喝药了')
print("已经发送了消息")
connection.close()
~
消费者.py
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue="水续传")
def callbak(ch,method,properties,body):
print("消费者接收到了任务:%r"%body.decode("utf8"))
channel.basic_consume(callbak,queue="水续传",no_ack=True)
channel.start_consuming()
10.单生产者,单消费者
执行服务端代码,生成队列,写入消息
执行消费者代码,从队列中取走消息
11.单生产者,多消费者 ,默认是轮询消费机制
12.消息丢列之确认机制,保证消息正确被处理 rabbitmq的ack确认机制
生产者代码不变
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='西游记')
channel.basic_publish(exchange='',
routing_key='西游记',
body='大师兄,师傅被妖怪抓走了')
print("已经发送了消息")
connection.close()
消费者代码如下.py
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='西游记')
def callback(ch, method, properties, body):
print("消费者接受到了任务: %r" % body.decode("utf-8"))
int('asdfasdf')
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(callback,queue='西游记',no_ack=False)
channel.start_consuming()
2.消息和队列持久化
生产者代码.py
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
'''
实现rabbitmq持久化条件
delivery_mode=2
使用durable=True声明queue是持久化
'''
channel.queue_declare(queue='python',durable=True)
channel.basic_publish(exchange='',
routing_key='python',
body='life is short,i use python ',
properties=pika.BasicProperties(
delivery_mode=2,
)
)
connection.close()
消费者代码.py
import pika
credentials = pika.PlainCredentials("pengpeng","123")
connection = pika.BlockingConnection(pika.ConnectionParameters('123.206.16.61',credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='python',durable=True)
'''
必须确保给与服务端消息回复,代表我已经消费了数据,否则数据一直持久化,不会消失
'''
def callback(ch, method, properties, body):
print("成功取出了消息 >>: %r" % body.decode("utf-8"))
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(callback,queue='python',no_ack=False)
channel.start_consuming()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类