python版本:   2.7.14

一 消息生产者代码:

 # -*- coding: utf-8 -*-

 import json
import pika
import urllib
import urllib2
import chardet
import sys
import json
from common import CommonMethod
import pika
import time HOST_NAME = "172.21.204.14"
USER_NAME = "xxx"
PASSWORD = "xxx" # 1."Hello World!"
def hello_world():
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello', # specify queue name
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close() # 2."Work queues"
def new_task():
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) # 设置队列持久化
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # 设置消息持久化
))
print(" [x] Sent %r" % message)
connection.close() # 3."Publish/Subscribe"
def emit_log(message):
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.exchange_declare(exchange='logs', # 申明logs交换机
exchange_type='fanout') # 交换机类型: 发布/订阅 channel.basic_publish(exchange='logs',
routing_key='',
body=message)
print(" [x] Sent %r" % message)
connection.close() # 4."Routing"
def emit_log_direct(log_level,message):
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.exchange_declare(exchange='direct_logs', # 申明logs交换机
exchange_type='direct') # 交换机类型: 路由(Routing) channel.basic_publish(exchange='direct_logs',
routing_key=log_level,
body=message)
print(" [x] Sent %r:%r" % (log_level, message))
connection.close() emit_log_direct("info", "info log message:...")
emit_log_direct("error", "error log message:...") # 5."Topic"
# 与Routing模式类似,比Routing模式多了routing_key可以使用通配符"*","#"等,使用更加灵活

二 消息消费者代码:

 # -*- coding: utf-8 -*-

 import json
import pika
import urllib
import urllib2
import chardet
import sys
import json
from common import CommonMethod
import pika
import time HOST_NAME = "172.21.204.14"
USER_NAME = "xxx"
PASSWORD = "xxx" # 1."Hello World!"
def hello_world():
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello', # specify queue name
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close() # 2."Work queues"
def new_task():
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.queue_declare(queue='task_queue', durable=True) # 设置队列持久化
message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # 设置消息持久化
))
print(" [x] Sent %r" % message)
connection.close() # 3."Publish/Subscribe"
def emit_log(message):
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.exchange_declare(exchange='logs', # 申明logs交换机
exchange_type='fanout') # 交换机类型: 发布/订阅 channel.basic_publish(exchange='logs',
routing_key='',
body=message)
print(" [x] Sent %r" % message)
connection.close() # 4."Routing"
def emit_log_direct(log_level,message):
credentials = pika.PlainCredentials(USER_NAME, PASSWORD)
connection = pika.BlockingConnection(pika.ConnectionParameters(HOST_NAME, 5672,'/', credentials))
channel = connection.channel() channel.exchange_declare(exchange='direct_logs', # 申明logs交换机
exchange_type='direct') # 交换机类型: 路由(Routing) channel.basic_publish(exchange='direct_logs',
routing_key=log_level,
body=message)
print(" [x] Sent %r:%r" % (log_level, message))
connection.close() emit_log_direct("info", "info log message:...")
emit_log_direct("error", "error log message:...") # 5."Topic"
# 与Routing模式类似,比Routing模式多了routing_key可以使用通配符"*","#"等,使用更加灵活

三 图片

官网参考文档: http://www.rabbitmq.com/getstarted.html

Python操作Rabbit MQ的5种模式的更多相关文章

  1. Python操作MySQL数据库的三种方法

    https://blog.csdn.net/Oscer2016/article/details/70257024 1. MySQLdb 的使用 (1) 什么是MySQLdb?   MySQLdb 是用 ...

  2. Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  3. python成长之路【第十三篇】:Python操作MySQL之pymysql

    对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎 ...

  4. Python开发【第十九篇】:Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  5. Day12(补充) Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...

  6. Python操作SQLAchemy

    如果对代码不懂就看这个:http://www.cnblogs.com/jixuege-1/p/6272888.html 本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql O ...

  7. Python开发【第十一篇】:Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...

  8. Python操作rabbitmq消息队列持久化

    消息队列持久化 Python操作rabbit消息队列的持久化,如下: # 创建一个名为balance的队列,对queue进行durable持久化设为True(持久化第一步)channel.queue_ ...

  9. Python自动化开发 - Python操作MySQL

    本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy 一.pymysql pymsql是Python中操作MySQL的模块,其使用方法和mysq ...

随机推荐

  1. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  2. poj2063 Investment

    http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...

  3. linux 常用清理或备份文件命令

    find /data/tmp/confluence/backups/ -type f -mtime +7 -exec rm -f {} \; ##查找创建超过7天的文件并强制删除 cp /data/a ...

  4. [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. DNS解析原理和流程

    DNS解析原理和流程   DNS解析其实就是将IP地址(202.96.134.133)变成域名(www.xxxxx.com)   网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所 ...

  6. Ajax棵

    ajax 1.什么是ajax?(异步请求,局部刷新) ajax是一个改善用户体验的技术,实质上是利用浏览器端ajax对象()向服务器发送异步(ajax对象在向服务器发送请求的时候,用户可以继续其他操作 ...

  7. virtualBox虚拟机联网

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

  8. css 播放器按钮实现

    效果图 html代码 //播放按钮 <div id="playBtn" class="circle" style="margin: 20px 0 ...

  9. String源码详解

    一.基本概念. 1.继承实现关系.因为被final修饰,因此是不可继承的String类,避免被他人继承后修改.实现了三个接口.可序列.可比较,有序.几个String兄弟类 2.本质就是字符数组,同时, ...

  10. input/radio/select等标签的值获取和赋值

    input/radio/select等标签的值获取和赋值,这几个是使用率最高的几个标签,获取值和赋值以及初始化自动填充数据和选择: 页面html: <div class=" " ...