【Flask基础】1、flask结合RabbitMq实现轻量级的数据接收队列,并使用twisted打印日志

复制以下demo,直接python flask.py 运行,调试完成后可以用nssm将服务注册成windows服务,方便管理且稳定。

flask.py
# -*- coding: utf-8 -*-
"""
File Name  receive
Created on 2019/09/09

@author: cjh
http接口,负责物联网数据接收
"""
from utils.rabbit_mq import RabbitBase
from config import config_rabbit
from twisted.internet import reactor
from flask import Flask, jsonify, request
from utils.log_info import write_log


app = Flask(__name__)


rabbit_data = RabbitBase(  # 实时数据入库队列对象
    config_rabbit['rabbit_host'], config_rabbit['rabbit_port'], config_rabbit['rabbit_user'],
    config_rabbit['rabbit_pwd'], config_rabbit['virtual_host'], config_rabbit['store_queue'])

app_key = "app_key“  # 项目app key


@app.route('/api/get_real_data/', methods=['GET', 'POST'])
def handle_real_data_and_status():
    """
    实时数据接收方法
    :return:
    """
    if request.method == 'POST':
        return input_rabbit_mq('real', request.form)
    else:
        write_log("[RE-FAILURE] [real] [%s]" % request.method)
        return jsonify({'code': 500, 'result': u'Method Get Is Not Allow'})


def input_rabbit_mq(d_type, d_val):
    """
    数据打入rabbitMQ
    :param d_type: 数据类型 real alarm beat
    :param d_val:
    :return:
    """
    ret = {'code': 400, 'result': u'开始'}
    try:
        if d_val.get('appkey') == app_key:
            rabbit_data.publish({d_type: d_val})  # 存入入库消息队列
            write_log('[RE-SUCCESS] [%s] - %s' % (d_type, d_val))  # 记录日志
            ret = {'code': 200, 'result': u'请求成功'}
        else:
            write_log('[RE-ERROR-KEY] [%s] - %s' % (d_type, d_val))  # 记录日志
            ret = {'code': 500, 'result': u'app_key不匹配:{}'.format(d_val.get('appkey'))}
    except Exception as e:
        write_log('[RE-ERROR] %s' % str(repr(e)))  # 记录日志
        ret = {'code': 500, 'result': u'接收错误'.format(repr(e))}
    finally:
        return jsonify(ret)


def run():
    host = "0.0.0.0"
    port = config_service['port']
    app.run(host=host, port=port, debug=True)     # run flask 服务
    reactor.run()                                 # twisted 支持flask为 多进程


if __name__ == "__main__":
    """当前文件测试"""
    run()

rabbit_mq.py

# -*- coding: utf-8 -*-
"""
@CreateTime: 2019/12/7 13:36
@Describe: rabbitMq 基类
@Author: cjh
"""
import rabbitpy


class RabbitBase:
    """
    rabbitMq 基类
    """
    def __init__(self, host, port, user, pwd, v_host, queue, exchange_type='direct'):
        """
        :param str host: 主机
        :param str port: 端口
        :param str user:  用户
        :param str pwd: 用户密码
        :param str v_host: 虚拟主机
        :param str queue: 队列名称
        :param str exchange_type: 交换机类型
        """
        self.count = 0
        self.threads_num = 100
        self._queue_name = queue
        self._exchange = queue
        self.connection = rabbitpy.Connection('amqp://{}:{}@{}:{}/{}'.format(user, pwd, host, port, v_host))
        self.channel = rabbitpy.AMQP(self.connection.channel())  # 使用适配器

        self.channel.exchange_declare(exchange=self._exchange, exchange_type=exchange_type, durable=True)  # 声明 exchange
        self.channel.queue_declare(queue=self._queue_name, durable=True)  # 声明队列
        # 绑定队列与exchange
        self.channel.queue_bind(queue=self._queue_name, exchange=self._exchange, routing_key=self._queue_name)

    def publish(self, msg):
        """rabbitMQ publish"""
        self.channel.basic_publish(exchange='', routing_key=self._queue_name, body=msg)

    def clear(self):
        """rabbitMQ purge"""
        self.channel.queue_purge(self._queue_name)

    def get_message_count(self):
        """rabbitMQ get message num"""
        channel = self.connection.channel()
        queue = rabbitpy.amqp_queue.Queue(channel, self._queue_name)
        queue.durable = True
        self.count = queue.declare(passive=True)[0]
        channel.close()

    def get_message(self):
        """
        获取并处理队列内数据 可重写
        :return:
        """
        pass


if __name__ == '__main__':
    r = RabbitBase('127.0.0.1', '5672', 'guest', 'guest', 'v_host', 'business_queue')
    r.get_message_count()  # get message num
    r.clear()  # purge
    r.publish("{'a': '12345'}")  # publish
    r.get_message_count()  # get message num
    r.get_message()  # get message

config.py
# -*- coding: utf-8 -*-
"""
@CreateTime: 2019/12/7 12:24

@Describe:  配置文件

@Author: cjh
"""
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
LOG_DIR = os.path.join(BASE_DIR, 'log')

config_rabbit = {  # rabbit mq 配置
    'rabbit_host': '127.0.0.1',  # ip
    'rabbit_port': '5672',  # 端口
    'rabbit_user': 'guest',
    'rabbit_pwd': 'guest',
    'virtual_host': 'yt_host',  # 虚拟主机名称
    'store_queue': 'yt_receiver',  # 数据队列
    'business_queue': 'yt_business',  # 业务逻辑处理队列
    'message_queue': 'yt_msg',  # 短信发送队列
    'recharge_queue': 'recharge'  # 微信充值回调队列
}
log_info.py
# -*- coding: utf-8 -*-
"""
@CreateTime: 2020/3/1 20:09
@Describe:
@Author: cjh
"""
from twisted.python import log
from handle.config import LOG_DIR
from utils import logTT
log.addObserver(logTT.ILog(LOG_DIR))


def write_log(message):
    """
    针对各人饮采集器 根据记录内容头部信息 分不同文件记录
    主要头部为: 数据接收日志 [RE-SUCCESS] [RE-FAILURE] [RE-ERROR]   对应是日志文件   receive_yyyy-mm-dd.txt
                数据入库 [ST-SUCCESS] [ST-FAILURE] [ST-ERROR]   对应是日志文件   store_yyyy-mm-dd.txt
                业务处理 [BU-SUCCESS] [BU-FAILURE] [BU-ERROR]   对应是日志文件   business_yyyy-mm-dd.txt
                短信发送 [MSG-SUCCESS] [MSG-FAILURE] [MSG-ERROR]   对应是日志文件   message_yyyy-mm-dd.txt
                数据库sql执行错误                                    对应日志文件

    :param message:
    :return:
    """
    log.msg(message)

你可能感兴趣的:(Flask,python,rabbitmq,队列)