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 ...
随机推荐
- Leetcode: Longest Common Subsequence
Given two strings text1 and text2, return the length of their longest common subsequence. A subseque ...
- flutter 右滑返回上一页
import 'package:flutter/material.dart'; import 'package:flutter_app/pages/SplashScreen.dart'; import ...
- UniPush使用指南
原贴:https://ask.dcloud.net.cn/article/35622 UniPush使用指南 分类:uni-app Push unipush 从HBuilderX 1.7.2起,uni ...
- 123457123457#0#----com.MC.konglongtianse222----前拼后广--恐龙填色mc-mc1111
com.MC.konglongtianse222----前拼后广--恐龙填色mc
- 《楞严经四种清净明诲》 (转自学佛网:http://www.xuefo.net/nr/article56/559965.html)
<楞严经四种清净明诲> 佛告阿难:“汝常闻我毗奈耶中,宣说修行三决定义.所谓摄心为戒,因戒生定,因定发慧,是则名为三无漏学. “阿难,云何摄心,我名为戒? “若诸世界六道众生其心不淫,则不 ...
- NETTY keeplive 参数,心跳检测
当设置为true的时候,TCP会实现监控连接是否有效,当连接处于空闲状态的时候,超过了2个小时,本地的TCP实现会发送一个数据包给远程的 socket,如果远程没有发回响应,TCP会持续尝试11分钟, ...
- curl命令测试网络请求中DNS解析、响应时间
https://blog.csdn.net/dreamer2020/article/details/78152576
- mac的公式编辑器: mathtype/latex
mathtype 收费版,且马上不能在mac系统上使用 latex 搜索一下: ➜ ~ brew search latex ==> Formulae gnome-latex latex2html ...
- 【C/C++开发】C语言实现函数可变参数
函数原型: int printf(const char *format[,argument]...) 返 回 值: 成功则返回实际输出的字符数,失败返回-1. 函数说明: ...
- 【ARM-LInux开发】利用scp 远程上传下载文件/文件夹
利用scp 远程上传下载文件/文件夹 scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o s ...