• RabbitMQ之入门


    生成者:

    #coding:utf-8
    import sys
    
    import pika
    
    credentials=pika.PlainCredentials("guest","guest")
    
    #获取信道
    conn_params=pika.ConnectionParameters("192.168.30.252",credentials=credentials)
    conn_broker=pika.BlockingConnection(conn_params)
    channel=conn_broker.channel()
    
    #声明交换器
    '''
    callback=None,
    exchange=None,  #交换器名称
    exchange_type='direct', #交换器的类型
    passive=False, #校验是否存在
    durable=False, #是否持久化
    auto_delete=False,#最后一个订阅后是否删除
    internal=False,
    nowait=False,
    arguments=None,
    type=None
    '''
    channel.exchange_declare(exchange="hello-exchange",type="direct",passive=False,durable=True,auto_delete=False)
    
    #创建文本消息
    msg=sys.argv[1]
    msg_props=pika.BasicProperties()
    msg_props.content_type='text/plain'
    
    #发布消息
    channel.basic_publish(body=msg,exchange="hello-exchange",properties=msg_props,routing_key="hola")
    

      

    消费者:

    #coding:utf-8
    __author__ = 'similarface'
    import pika
    #建立到代理服务器的连接
    credentials=pika.PlainCredentials('guest','guest')
    conn_params=pika.ConnectionParameters("localhost",credentials=credentials)
    conn_broker=pika.BlockingConnection(conn_params)
    
    #获取信道
    channel=conn_broker.channel()
    
    #声明交换器
    channel.exchange_declare(exchange="hello-exchange",type="direct",passive=False
                             ,durable=True,auto_delete=False)
    
    #声明队列
    channel.queue_declare(queue="hello-queue")
    
    #通过键hola 将队列和交换器绑定
    channel.queue_bind(queue="hello-queue",exchange="hello-exchange",routing_key="hola")
    
    #用于处理传入消息的函数
    def msg_consumer(channel,method,header,body):
        #消息确认
        channel.basic_ack(delivery_tag=method.delivery_tag)
    
        if body=="quit":
            #停止消费并退出
            channel.basic_cancel(consumer_tag="hello-consumer")
            channel.stop_consuming()
        else:
            print(body)
        return
    #订阅消费者
    channel.basic_consume(msg_consumer,queue="hello-queue",consumer_tag="hello-consumer")
    #开始消费
    channel.start_consuming()
  • 相关阅读:
    NanoMQ Newsletter 202206|规则引擎正式发布 & NanoSDK 支持 MQTT over QUIC
    EMQX Newsletter 202206|与 HStreamDB 集成、充电桩通信协议 OCPP 网关开发…
    将 EMQX Cloud 数据通过公网桥接到 AWS IoT
    使用 EMQX Cloud 实现物联网设备一机一密验证
    asm:NASM汇编语言的环境配置(rockylinuxlinux)
    Hibernate 级联添加删除
    DropDownList二级无刷新功能实现
    ASP.NET之AJAX分页步骤
    常用的获取时间差的sql语句
    asp.net中时间差的问题
  • 原文地址:https://www.cnblogs.com/similarface/p/5895626.html
Copyright © 2020-2023  润新知