博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Day90
阅读量:4693 次
发布时间:2019-06-09

本文共 3929 字,大约阅读时间需要 13 分钟。

# rabbitmq队列详解 # 转载:http://www.cnblogs.com/alex3714/articles/5248247.html # 安装python rabbitMQ module # pip install pika # or # easy_install pika # or # 源码 # https://pypi.python.org/pypi/pika ################################################################################# # 实现最简单的队列通信 # send端 # !/usr/bin/env python # import pika # connection = pika.BlockingConnection(pika.ConnectionParameters( #     'localhost')) # channel = connection.channel() # # 声明queue # channel.queue_declare(queue='hello') # # n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange. # channel.basic_publish(exchange='', #                       routing_key='hello', #                       body='Hello World!') # print(" [x] Sent 'Hello World!'") # connection.close() ################################################################################# # receive端 # _*_coding:utf-8_*_ # __author__ = 'Alex Li' # import pika # connection = pika.BlockingConnection(pika.ConnectionParameters( #     'localhost')) # 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='hello') # def callback(ch, method, properties, body): #     print(" [x] Received %r" % body) # channel.basic_consume(callback, #                       queue='hello', #                       no_ack=True) # print(' [*] Waiting for messages. To exit press CTRL+C') # channel.start_consuming() ################################################################################# # 远程连接rabbitmq server的话,需要配置权限 # 首先在rabbitmq server上创建一个用户 # sudo rabbitmqctl  add_user alex alex3714   # 同时还要配置权限,允许从外面访问 # sudo rabbitmqctl set_permissions -p / alex ".*" ".*" ".*" # 客户端连接的时候需要配置认证参数 # credentials = pika.PlainCredentials('alex', 'alex3714') # connection = pika.BlockingConnection(pika.ConnectionParameters( #     '10.211.55.5', 5672, '/', credentials)) # channel = connection.channel() ################################################################################# # 消息持久化 # channel.queue_declare(queue='hello', durable=True) # channel.queue_declare(queue='task_queue', durable=True) # channel.basic_publish(exchange='', #                       routing_key="task_queue", #                       body=message, #                       properties=pika.BasicProperties( #                          delivery_mode = 2, # make message persistent #                       )) ################################################################################# # 消息公平分发 # 如果Rabbit只管按顺序把消息发到各个消费者身上,不考虑消费者负载的话,很可能出现,一个机器配置不高的消费者那里堆积了很多消息处理不完, # 同时配置高的消费者却一直很轻松。为解决此问题,可以在各个消费者端,配置perfetch=1, # 意思就是告诉RabbitMQ在我这个消费者当前消息还没处理完的时候就不要再给我发新消息了。 ################################################################################# # Publish\Subscribe(消息发布\订阅)  # 之前的例子都基本都是1对1的消息发送和接收, # 即消息只能发送到指定的queue里,但有些时候你想让你的消息被所有的Queue收到,类似广播的效果,这时候就要用到exchange了 # 表达式符号说明:#代表一个或多个字符,*代表任何字符 #       例:#.a会匹配a.a,aa.a,aaa.a等 #           *.a会匹配a.a,b.a,c.a等 #      注:使用RoutingKey为#,Exchange Type为topic的时候相当于使用fanout  # headers: 通过headers 来决定把消息发给哪些queue ################################################################################# # 有选择的接收消息(exchange type=direct)  # RabbitMQ还支持根据关键字发送,即:队列绑定关键字, # 发送者将数据根据关键字发送到消息exchange,exchange根据 关键字 判定应该将数据发送至指定队列。 ################################################################################# # Remote procedure call (RPC) # fibonacci_rpc = FibonacciRpcClient() # result = fibonacci_rpc.call(4) # print("fib(4) is %r" % result) #################################################################################

转载于:https://www.cnblogs.com/pandaboy1123/p/9159508.html

你可能感兴趣的文章
Spark 1.4.1中Beeline使用的gc overhead limit exceeded
查看>>
zookeeper和dubbo的关系
查看>>
Android中调用webservice
查看>>
编译安装zabbix3.4及实现邮件报警
查看>>
《程序是怎样跑起来的》第七章
查看>>
control遍历
查看>>
js实现弹幕效果
查看>>
Java 中Model 与 实体的区别
查看>>
LintCode 55---比较字符串
查看>>
lodash (js实用工具库)
查看>>
并行进程问题
查看>>
微信机器人 简化版
查看>>
360微博提醒增强版的下载地址
查看>>
nginx 报错 HTTP ERROR 500 (PHP数组简写模式)
查看>>
ubuntu命令安装java 6 jdk
查看>>
系统分析与设计第一次作业
查看>>
008:派
查看>>
Android技术——在Android中的随意视图中找控件
查看>>
Java并发 - 线程池的使用
查看>>
博客园app for xamarin android
查看>>