Python_消息队列
rabbitpy: RabbitMQ Simplified
###[scheme]://[username]:[password]@[host]:[port]/[virtual_host]
url = 'amqp://guest:guest@localhost:5672/%2F'
这个%2f是字符斜杠/ uni-encode之后的结果,而/就是默认的hostname,它在HTTP传输的时候必须要转义。
Port 15672 is for the management panel and is open for connections from a different machine.
通过 http://serverip:15672 访问 RabbitMQ 的 Web 管理界面,默认用户名密码都是 guest
Port 5672 is for applications and can only be accessed from the internal network.
部署:
1.安装erlang 语言
由于rabbitMQ是erlang语言实现的,所以我们需要安装erlang
sudo apt-get install erlang-nox
查看版本: erl -version
2.安装rabbitMQ
安装最新版rabbitMQ
sudo apt-get install rabbitmq-server
3.查看rabbitMQ状态,active(running)表示在线
sudo systemctl status rabbitmq-server
服务端命令
rabbitmqctl 用来管理RabbitMQ中间件的命令行工具.它通过连接中间件节点来执行所有操作
rabbitmqctl 命令,查看队列信息,所包含的信息包括 name,arguments, messages,memory
用户管理
注意rabbitmqctl 管理RabbitMQ 内部用户数据库. 任何来自其它认证后端的用户对于rabbitmqctl来说是不可见的.
sudo rabbitmqctl list_queues
rabbitmqctl cluster_status
获取 RabbitMQ Server 状态 命令为 rabbitmqctl status
rabbitmq-plugins list
rabbitmq-plugins 是管理RabbitMQ broker插件的命令行
后台管理界面是以插件的形式需要用户手动安装,不像activemq启动后后台管理服务也自动启动可直接访问,
rabbitmq 后台管理界面访问http://localhost:15672
开启后台管理页面
要在浏览器中访问 RabbitMQ,需要使用 RabbitMQ 的 Web 界面插件。可以按照以下步骤安装和配置:
1. 安装 RabbitMQ Web 插件:
sudo rabbitmq-plugins enable rabbitmq_management
2. 重启 RabbitMQ 服务:
sudo systemctl restart rabbitmq-server
3. 确认 Web 界面已经启用:
sudo rabbitmq-plugins list
在生产环境中,应该限制对 RabbitMQ 管理插件的访问,并使用HTTPS来保护您的数据
生产者
import rabbitpy
url = 'amqp://guest:guest@192.168.89.38:30595/%2F'
connection = rabbitpy.Connection(url)
channel = connection.channel()
###声明一个exchange,指定channel名称
exchange = rabbitpy.Exchange(channel, 'chapter2-example')
####使用declare方法来发送Exchange.Declare命令
exchange.declare()
####建立队列,指定队列名称
queue = rabbitpy.Queue(channel, 'example')
###发送Queue.Declare命令,会返回(此队列中的消息数,此队列的消费者数)
queue.declare()
###绑定队列到exchange# 会发送Queue.Bind命令,传入exchange和路由key
queue.bind(exchange, 'example-routing-key')
####下面就可以发送消息了
for message_number in range(10):
message = rabbitpy.Message(channel, # 指定channel
f'Test message {message_number}', # 消息体
{'content_type':'text/plain'}, # 消息属性(字典)
opinionated=True)
# 创建Basic.Public方法帧(frame),消息头帧,和一个消息体帧,然后传输到RabbitMQ
message.publish(exchange, 'example-routing-key')
代码
import rabbitpy,datetime
###[scheme]://[username]:[password]@[host]:[port]/[virtual_host]
url = 'amqp://guest:guest@localhost:5672/%2F'
with rabbitpy.Connection(url) as connection:
with connection.channel() as channel:
exchange = rabbitpy.Exchange(channel, 'chapter4-example')
exchange.declare()
channel.enable_publisher_confirms() # 开启发布确认
body = 'This is an important message'
message = rabbitpy.Message(channel, body,
{'content_type': 'text/plain','message_type': 'very important'}
)
if message.publish('chapter4-example','important.message'):
print('The message was confirmed')
### DirectExchange FanoutExchange HeadersExchange TopicExchange
### bind declare delete unbind
消费者
import rabbitpy
with rabbitpy.Connection() as conn:
with conn.channel() as channel:
amqp = rabbitpy.AMQP(channel)
for message in amqp.basic_consume('queue-name'):
print(message)
#### basic_get basic_cancel basic_nack basic_publish
with conn.channel() as channel:
queue = rabbitpy.Queue(channel, 'example')
for message in queue.consume():
print 'Message: %r' % message
message.ack()
import rabbitpy
with rabbitpy.Connection('amqp://guest:guest@localhost:5672/%2f') as conn:
with conn.channel() as channel:
queue = rabbitpy.Queue(channel, 'example')
while len(queue) > 0:
message = queue.get()
message.pprint(True)
message.ack()
print('There are {} more messages in the queue'.format(len(queue)))
rabbitpy.Queue
#queue = rabbitpy.Queue(channel, 'my-queue')
rabbitpy.AMQP
The rabbitpy.AMQP adapter provides a more traditional AMQP client library API seen in libraries like pika.
import rabbitpy
with rabbitpy.Connection() as conn:
with conn.channel() as channel:
amqp = rabbitpy.AMQP(channel)
定义rabbitmqWrapper
定义 rabbitmqPublisher
class RabbitmqPublisher(LoggerMixin):
self.queue = self.channel.queue_declare(queue=queue_name, durable=True)
@decorators.tomorrow_threads(100)
class RabbitmqConsumer(LoggerMixin):
consuming_function
处理消息的函数,函数有且只能有一个参数,参数表示消息。是为了简单,放弃策略和模板来强制参数
@decorators.keep_circulating(1) # 是为了保证无论rabbitmq异常中断多久,无需重启程序就能保证恢复后,程序正常。
channel.basic_qos(prefetch_count=self._threads_num)
rabbitmq消费端加入精确控频 rabbitmq有消费确认的概念
代码
Python编程中,经常会遇到需要定义抽象基类(Abstract Base Class)的情况。抽象基类是一种特殊的类,不能被实例化,
主要用于定义接口和约束子类的行为。
Python提供了ABC(Abstract Base Class)包,它提供了一组用于创建和使用抽象基类的工具和装饰器
抽象类是一种特殊的类, 只能被继承 不能被实例化, 子类需要实现基类指定的抽象方法
metaclass的实例化结果是类,而class实例化的结果是instance
abc模块,Python 对于ABC的支持模块,定义了一个特殊的metaclass—— ABCMeta
还有一些装饰器—— @abstractmethod 和 @abstarctproperty
抽象基类如果想要声明“抽象方法”,可以使用 @abstractmethod ,如果想声明“抽象属性”,可以使用 @abstractproperty
设计模式
创建类设计模式(5种)
单例模式、工厂模式(简单工厂模式、抽象工厂模式)、建造者模式、原型模式
结构类设计模式(7种)
代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式
行为类设计模式(11种)
策略模式、责任链模式、命令模式、中介者模式、模板模式、
迭代器模式、访问者模式、观察者模式、解释器模式、备忘录模式、状态模式
工厂模式 内容:定义了一个用于创建对象的接口(工厂类),让工厂子类决定实例化哪一个产品类。
from abc import ABCMeta, abstractmethod
抽象产品(Product):定义了产品的规范,描述了产品的主要特性和功能。
class Payment(metaclass=ABCMeta): @abstractmethod def pay(self):
具体产品(ConcreteProduct):实现了抽象产品定义的接口,由具体工厂来创建,它同具体工厂之间一一对应
class WechatPay(Payment):
class Alipay(Payment):
抽象工厂(Abstract Factory):提供了创建产品的接口,调用者通过它访问具体工厂的工厂方法newProduct()来创建产品。
class PaymentFactory(metaclass=ABCMeta)
@abstractmethod def create_method(self):
具体工厂(ConcreteFactory):主要实现抽象工厂中的抽象方法,完成具体产品的创建。
class AlipayFactory(PaymentFactory)
def create_method(self):return Alipay()
class HuabeiFactory(PaymentFactory)
应用:
pf = HuabeiFactory()
p = pf.create_method()
p.pay(100)
1.rabbitmq 管理平台操作
RabbitMQ management 插件
Queues
找到目标队列,purge --> 清空队列
找到目标队列,delete --> 删除队列
RabbitMQ Management 插件不仅提供了 Web 管理界面,还提供了 HTTP API 接口来方便调用
rabbitmqadmin 也是RabbitMQ Management 插件提供的功能,它会包装 HTTP API 接口,使其调用显得更加简洁方便
2.rabbitmqctl 命令行
rabbitmqctl 工具是用来管理 RabbitMQ 中间件的命令行工具,它通过连接各个 RabbitMQ 节点来执行所有操作。
# 查看所有队列
rabbitmqctl list_queues
# 根据 queue_name 参数,删除对应的队列
rabbitmqctl delete_queue queue_name
#根据 queue_name 参数,清空对应的队列
rabbitmqctl -p vhostpath purge_queue queue_name
3.rabbitmq API命令
RabbitMQ management 插件同样是由 Erlang 语言编写的,并且和 RabbitMQ 服务运行在同一个 Erlang 虚拟机中
参考
python rabbitmq的库,rabbitpy代替pika https://www.cnblogs.com/ExMan/p/11801246.html
python3 工厂模式 python工厂函数原理 https://blog.51cto.com/u_16213675/7365145
https://blog.csdn.net/yjw123456/article/details/118756425
https://rabbitpy.readthedocs.io/en/latest/api/channel.html