python3 访问 rabbitmq 示例
关于 rabbitmq
之前用过 kafka,要是拿这两者做对比的话,大概有以下异同:
- 两者都是一个分布式架构
- kafka 具有较高的吞吐量,rabbimq 吞吐量较小
- rabbitmq 的可靠性更好,确认机制(生产者和 exchange,消费者和队列),支持事务,但会造成阻塞,委托(添加回调来处理发送失败的消息)和备份交换器(将发送失败的消息存下来后面再处理)机制
- kafka 常用于日志收集业务,rabbitmq 常用于抢购,支付业务
rabbitmq demo
producer
# coding: utf-8
import json
import pika credentials = pika.PlainCredentials('sm', 'sm')
connection = pika.BlockingConnection(pika.ConnectionParameters('32.86.5.93', 5672, '/', credentials))
channel = connection.channel() # 声明queue,需要注意这里的配置,消费者声明 queue 时需要与生产者保持一致
channel.queue_declare(queue='viosm', arguments={"x-max-length": 10000}) body = json.dumps({"test": "test"})
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
# 推送到交换机,因为队列是一进一出的,如果推送到队列,只能有一个消费者来获取(来自毛绒绒胖虫子)
ret = channel.basic_publish(exchange="smai",
routing_key="",
body=body)
print(ret) # 返回 None
connection.close()
consumer
# coding: utf-8
import json
import pika credentials = pika.PlainCredentials('sm', 'sm')
connection = pika.BlockingConnection(pika.ConnectionParameters('32.86.5.93', 5672, '/', credentials))
channel = connection.channel() # You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue='viosm', arguments={"x-max-length": 10000}) # 这里需要与生产者的声明保持一致 def msg_consumer(ch, method, properties, data_bytes):
data_json = data_bytes.decode()
data_dict = json.loads(data_json)
print("{}".format(data_dict))
ch.basic_ack(delivery_tag=method.delivery_tag) # 手动提交偏移量 channel.basic_consume('viosm', # 队列名
msg_consumer, # 回调函数
consumer_tag="seemmo_consumer",
# auto_ack=True, # 自动提交偏移量
) channel.start_consuming()
# forever
ending ~
python3 访问 rabbitmq 示例的更多相关文章
- openresty 学习笔记五:访问RabbitMQ消息队列
openresty 学习笔记五:访问RabbitMQ消息队列 之前通过比较选择,决定采用RabbitMQ这种消息队列来做中间件,目的舒缓是为了让整个架构的瓶颈环节.这里是做具体实施,用lua访问Rab ...
- springboot入门系列(三):SpringBoot教程之RabbitMQ示例
SpringBoot教程之RabbitMQ示例 SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用Rabb ...
- python3 访问windows共享目录
python3 访问windows共享目录 1.安装pysmb包 pip install pysmb 2.连接共享目录 #!/usr/bin/env python3 # -*- coding:utf- ...
- openresty 学习笔记番外篇:python访问RabbitMQ消息队列
openresty 学习笔记番外篇:python访问RabbitMQ消息队列 python使用pika扩展库操作RabbitMQ的流程梳理. 客户端连接到消息队列服务器,打开一个channel. 客户 ...
- 从JAVA客户端访问Redis示例(入门)
转自:http://blog.csdn.net/kkdelta/article/details/7217761 本文记录了安装Redis和从JAVA端访问Redis的步骤 从http://downlo ...
- Spring.NET 中的 ADO.NET 数据访问的示例
Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spri ...
- MySQL系列--4.使用Python3访问数据库
1.安装MySQL驱动 pip install mysql-connector 安装完成后进入命令行模式,导入驱动,如果不报错,说明安装成功 Python 3.6.7 (default, Oct 22 ...
- python3之rabbitMQ
1.RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息 ...
- Linux下安装、配置、启动与访问RabbitMQ
一.下载 首先第一步要下载三个rpm安装包,为了方便安装与学习,给出下载途径 网盘网址:https://pan.baidu.com/s/18Z64Lb9KQpRh10RzqZBdoQ 提取码:094v ...
随机推荐
- 目标检测标注工具labelImg安装及使用
目标检测中,原始图片的标注过程是非常重要的,它的作用是在原始图像中标注目标物体位置并对每张图片生成相应的xml文件表示目标标准框的位置.本文介绍一款使用方便且能够标注多类别并能直接生成xml文件的标注 ...
- 海思3531添加移远EC20 4g模块(转)
源: 海思3531添加移远EC20 4g模块 Hi3798移植4G模块(移远EC20)
- PHP判断文件大小是MB、GB、TB...
<?php date_default_timezone_set ("PRC" ); function getFilePro($fileName){ if (!file_exi ...
- ISO/IEC 9899:2011 条款6.4——词法元素
6.4 词法元素 1.token(标记): keyword(关键字) identifier(标识符) constant(常量) string-literal(字符串字面量) punctuator(标点 ...
- j2s7s300 refers to jaco v2 7DOF spherical 3fingers
<?xml version="1.0"?> <!-- j2s7s300 refers to jaco v2 7DOF spherical 3fingers --& ...
- MFC加载osg模型
创建MFC单文档项目, OSGObject.h #pragma once #include <osgViewer\Viewer> #include <osgDB\ReadFile&g ...
- 一些有用的github收藏(持续更新中...)
1.facebook的c++开源库folly(Facebook open source library)介绍 https://github.com/facebook/folly 2.pprint 一个 ...
- 【Leetcode_easy】953. Verifying an Alien Dictionary
problem 953. Verifying an Alien Dictionary solution: class Solution { public: bool isAlienSorted(vec ...
- C#自带的Version判断版本号的大小
Version version1 = new Version("1.0.0.25"); Version version2 = new Version("1.0.0.24& ...
- tcpreplay使用介绍
安装 brew install tcpreplay yum install tcpreplay tcpreplay 回放 tcpreplay is a tool for replaying netwo ...