• pb格式的文件与rabbitmq连用(rabbitmq生产与消费)


    背景:

    这篇主要讲解的是pb协议输入如何放入到mq进行生产和消费,与实际原理都是一样的,与python使用protobuf协议传输固定格式协议数据 这个文章相对应

    https://www.cnblogs.com/chongyou/p/13543340.html

    工作中的运用

    在我们工作中经常会使用到生产数据与消费数据,一般普通的消息生产如下

    生产消息

    import pika
    
    def SendToMQ(data):
    
        ip = '172.XX.233.XX'    # mq的连接地址
        ex = 'test-ex-msgrecv'   #mq的exchange
        rkey = 'test-rkey-msgrecv-msgdb-64v'   #mq的rkey 
        credentials = pika.PlainCredentials('xx', 'xx')      #mq的账户和密码  ,注意我们需要使用到pika,
        parameters = pika.ConnectionParameters('%s'%(ip),5672,'/',credentials)
        connection = pika.BlockingConnection(parameters) #连接时的一系列的配置
        channel = connection.channel()   #创建一个频道
    
        channel.exchange_declare(exchange=ex,   #这一块都在说明mq的配置
                                 exchange_type='direct',   #mq类型
                                 durable=False)    #是否持久化
        # sendstr = RecvMsg()
        channel.basic_publish(exchange=ex,      #使用的exchange
                              routing_key=rkey,   #使用的那个rkey
                              body="张君字符串")    #传输的内容,什么内容都可以
    
        channel.close()
        connection.close()
    

      

    消费

    import pika
    
    import logging
    import time
    from datetime import datetime
    
    
    def Ex_To_MQ(hostIP,rkey):
        print("MQ config:hostIP:%s,rkey:%s"%(hostIP,rkey))
        logging.debug("MQ is starting")
        credentials=pika.PlainCredentials('push','testmq')    
        connection=pika.BlockingConnection(pika.ConnectionParameters(hostIP,5672,'/',credentials))
        channel=connection.channel()
        test_queue = 'test-sms-delivery-2'
        channel.exchange_declare(exchange='test-ex-xx',exchange_type='direct')   #注意生产的与消费的exchange要一致
        result = channel.queue_declare(queue=test_queue)
        q_name = result.method.queue
    
        channel.queue_bind(exchange='test-ex-xx', #绑定的exchange
                           queue=q_name,
                           routing_key=rkey)  
        def unpack(data):
            j = jpush_protocol_pb2.JPushProtocol()
            j.Clear()
            j.ParseFromString(data)
            print(str(j))
            return j
    
        def callback(ch, method, properties, body):
            print "body:",body
            a = unpack(body)
            logging.debug("[message batch received] %r:%r"%(method.routing_key,a))
    
        channel.basic_consume(callback,
                              queue=q_name,
                              no_ack=True)
        channel.start_consuming()
    
    if __name__=='__main__':
        logging.basicConfig(level=logging.DEBUG,
            format='[%(levelname)s][%(funcName)s][%(lineno)d][%(asctime)s][%(message)s]',
            filename='./log/consume_ex2_conn.log',
            filemode='w'
            )
        logging.debug("the process is starting")
        jobs=[]
    
        hostIP="xx.xx.xx.118"
        rkey="test-rkey-xx-xx-64v"
        Ex_To_MQ(hostIP,rkey)
    

      

    与PB协议文件连用

    其实与pb文件一起使用,主要是把data数据的源更换成pb协议的就行了,结合上一篇文章,那么生产消息就成了

    #-*- coding: utf-8 -*-
    import string
    import pika
    import json
    import random
    import types
    import time
    
    import   jpush_protocol_pb2
    msg_ctrl = jpush_protocol_pb2.JPushProtocol()
    
    def ReturnRid():
        rid, sid = random.randint(4294967296, 18446744073709551616), random.randint(4294967296, 18446744073709551616)
        return rid, sid
    
    def usage():
        print("""Usage:<appkey><uid><mid>
        for example:
            python %s a47e89aaa2b3123537d91da0 6013066725 [1088953989,]""" % (sys.argv[0]))
    
    
    def GetMsgCtrl(appkey, msgid,uid):
        #head = jpush_protocol_pb2.JPushProtocolHead()
        msg_ctrl.head.ver = 0
        msg_ctrl.head.uid = uid
        msg_ctrl.head.app_key = appkey
        msg_ctrl.head.platform = 0
        msg_ctrl.head.rom = 0
        msg_ctrl.head.ctime = int(time.time())
    
        msg_ctrl.body.msg.msgid = int(msgid)
        msg_ctrl.body.msg.real_uid = int(uid)
        msg_ctrl.body.msg.platform = jpush_protocol_pb2.JPushPushMessage.ANDROID
        print(msg_ctrl)
        str_msg = msg_ctrl.SerializeToString()
        pare=msg_ctrl.ParseFromString(str_msg)
        print(pare)
        print(str_msg)
        return str_msg
    
    
    
    
    def SendToMQ(data):
    
        ip = '172.19.233.118'
        ex = 'test-ex-msgrecv'
        rkey = 'test-rkey-msgrecv-msgdb-64v'
        credentials = pika.PlainCredentials('push', 'testmq')
        parameters = pika.ConnectionParameters('%s'%(ip),5672,'/',credentials)
        connection = pika.BlockingConnection(parameters)
        channel = connection.channel()
    
        channel.exchange_declare(exchange=ex,
                                 exchange_type='direct',
                                 durable=False)
        # sendstr = RecvMsg()
        channel.basic_publish(exchange=ex,
                              routing_key=rkey,
                              body=data)
    
        channel.close()
        connection.close()
    
    
    

      

  • 相关阅读:
    struts2学习笔记(二)
    struts2学习问题(一)
    struts2学习(一)
    给 ecplise 配置struts2配置环境
    Servlet的生命周期
    奇数位丢弃
    快速排序和归并排序(C语言)
    学习正则表达式及c#应用
    C#string与stringBuilder的区别
    c#之字符串函数
  • 原文地址:https://www.cnblogs.com/chongyou/p/13579884.html
Copyright © 2020-2023  润新知