zoukankan      html  css  js  c++  java
  • rabbitmq的笔记(三)用Python生产和消费

    消费者:
    #!/usr/local/bin/python2.7
    # -*- coding: utf-8 -*-
    import pika
     
    connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.79', 5672, '/'))
    channel = connection.channel()
     
    def callback(channel, method, properties, message):
    print("get resultc from queue %s" % message)
     
    channel.basic_consume('rhj',
    callback,)
     
    channel.start_consuming()
     
    生产者:
    #!/usr/local/bin/python2.7
    # -*- coding: utf-8 -*-
    import pika
     
    connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.78', 5672,'/'))
    channel = connection.channel()
     
    channel.queue_declare(queue='rhj')
     
    channel.basic_publish(exchange='',
    routing_key='rhj',
    body='rabbitmq003')
    connection.close()
    先执行消费者,会一直监听队列。等执行生产者,消息马上被消费,显示在消费者那里。
    这样的程序适合在新安装的rabbitmq和各种迁移升级里面把读写过程展示出来,可以体现在变更的过程中有没有消息丢失了。
    如果遇到集群,要填写所有节点的读写,这一在参数的一栏这样填写。
    集群的写法:
    Parameters = (
    pika.ConnectionParameters('39.104.65.201', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1),
    pika.ConnectionParameters('39.99.35.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1),
    pika.ConnectionParameters('39.104.24.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1))
    connection = pika.BlockingConnection(Parameters)

  • 相关阅读:
    多测师讲解htm_L标题标签001_高级讲师 肖sir
    Shell特殊变量介绍与实践 $0
    shell 变量定义技巧总结
    shell 环境变量的知识小结
    前端 chrome查看html样式基本操作
    shell 命令 env
    date 命令
    shell 命令 set命令
    shell export 命令
    前端 html span标签
  • 原文地址:https://www.cnblogs.com/hodge01/p/15190498.html
Copyright © 2011-2022 走看看