• Python rabbitmq


    一、RabbitMQ安装

    安装配置epel源

       $ rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
     
    安装erlang
       $ yum -y install erlang
     
    安装RabbitMQ
       $ yum -y install rabbitmq-server
    二、启动rabbitmq

    /etc/init.d/rabbitmq-server start

    [root@rabbitmq init.d]# ./rabbitmq-server start
    Starting rabbitmq-server: SUCCESS
    rabbitmq-server.

    1.生产者和消费者模型(三种模型:a,acknowledgment 消息不丢失;b,durable消息不丢失;c,消息获取顺序)

    生产者:

    __author__ = 'alex'
    #!/usr/bin/env python
    import pika
    
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.queue_declare(queue='hello1',durable=True)
    
    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body='Hello World!',
                          properties=pika.BasicProperties(
                              delivery_mode=2, # make message persistent
                          ))
    print(" [x] Sent 'Hello World!'")
    connection.close()

    消费者:

    __author__ = 'alex'
    import pika
    import time
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    
    channel = connection.channel()
    
    channel.queue_declare(queue='hello1',durable=True)
    
    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body,type(body))
        time.sleep(2)
        print ("OK")
        ch.basic_ack(delivery_tag = method.delivery_tag)
    
    channel.basic_consume(callback,
                          queue='hello',
                          no_ack=False)
    
    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

    2.发布者和订阅者模型

    a. exchange type = fanout

    发布者:

    __author__ = 'alex'
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',
                             type='fanout')
    
    message = ' '.join(sys.argv[0]) or "info: Hello World!"
    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message)
    print(" [x] Sent %r" % message)
    connection.close()

    订阅者:

    #!/usr/bin/env python
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',
                             type='fanout')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    print (queue_name)
    
    channel.queue_bind(exchange='logs',
                       queue=queue_name)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r" % body)
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()

    b.exchange type = direct

    发布者:(执行发布的时候带参数,确定发布参数的级别)

    __author__ = 'alex'
    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',
                             type='direct')
    
    severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='direct_logs',
                          routing_key=severity,
                          body=message)
    print(" [x] Sent %r:%r" % (severity, message))
    connection.close()

    订阅者:(可以同时执行多个订阅者,定义每个订阅者的接收消息的级别,只接收相同级别的消息)

    __author__ = 'alex'
    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='direct_logs',
                             type='direct')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    print (queue_name)
    
    severities = sys.argv[1:]
    if not severities:
        sys.stderr.write("Usage: %s [info] [warning] [error]
    " % sys.argv[0])
        sys.exit(1)
    
    for severity in severities:
        channel.queue_bind(exchange='direct_logs',
                           queue=queue_name,
                           routing_key=severity)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()

    c. exchange type = topic

    # 表示可以匹配 0 个 或 多个 单词
    * 表示只能匹配 一个 单词

    生产者:

    __author__ = 'alex'
    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='topic_logs',
                             type='topic')
    
    routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='topic_logs',
                          routing_key=routing_key,
                          body=message)
    print(" [x] Sent %r:%r" % (routing_key, message))
    connection.close()

    消费者:

    __author__ = 'alex'
    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='192.168.2.109'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='topic_logs',
                             type='topic')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    
    binding_keys = sys.argv[1:]
    if not binding_keys:
        sys.stderr.write("Usage: %s [binding_key]...
    " % sys.argv[0])
        sys.exit(1)
    
    for binding_key in binding_keys:
        channel.queue_bind(exchange='topic_logs',
                           queue=queue_name,
                           routing_key=binding_key)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()

    在执行消费者的时候,带参数可以匹配模糊的符号,*或者#,在生产者执行的时候,会自动匹配消费者的规则,如果匹配规则,那么消费者可以接收到相关的信息。

  • 相关阅读:
    elementui 表格格式化
    安装mongoDB出现的问题:无法启动
    学习笔记-angular 使用uuid
    cordova app打包apk签名
    ionic3 清除navpush的堆栈 (android真机返回键bug)
    ionic3 StatusBar 不显示问题
    ionic3+angular 倒计时效果
    windows下一台机器运行多个tomcat
    JDBC连接数据库小结
    工具类---提供精确的浮点数运算
  • 原文地址:https://www.cnblogs.com/python-study/p/5860839.html
Copyright © 2020-2023  润新知