【消息中间件】基于Kafka的消息中间件封装组件

基于Kafka的消息中间件组件封装,老样子直接上代码:

import json
import threading
import uuid
from typing import Union
try:
    from kafka import KafkaConsumer
    from kafka import KafkaProducer
except:
    import os
    os.system(' pip install -i https://pypi.tuna.tsinghua.edu.cn/simple kafka-python ')
    from kafka import KafkaConsumer
    from kafka import KafkaProducer

class KafkaMQ():
    producer = None  # 生产者
    consumer = None  # 消费者
    consumer_channel = None  # 订阅的频道

    def __init__(self,
                 nodes: Union[list, str],
                 topics=None,
                 group_id=None,
                 username=None,
                 pwd=None,
                 security_protocol='PLAINTEXT',
                 create_producter=True
                 ):
        """
        :param nodes:kafka集群节点地址可以是列表或者逗号分隔字符。集群地址必须是:ip:port组成。
        :param topics:消息主题,可以是列表或者逗号分隔字符,可以在创建对象后分配。
        :param group_id:多个拥有相同group_id的消费者被判定为一组,一条数据记录只会被同一个组中的一个消费者消费。一般不设置!
        :param client_id:客户端id,默认时随机编号
        :param username:用户名
        :param pwd:密码
        :param create_producter:默认创建生产者
        """

        if topics is None:
            self.consumer_channel = []
        else:
            self.consumer_channel = topics

        self.nodes = nodes
        self.group_id = group_id
        self.client_id = f'{uuid.uuid4()}'
        self.security_protocol = security_protocol

        # 生产者
        if create_producter:
            self._setPorducter(username=username, pwd=pwd)

        # 消费者
        self._setConsumer(topics=self.consumer_channel, username=username, pwd=pwd)

    # 创建消费者
    def _setConsumer(self, topics=None, username=None, pwd=None):
        self.consumer = KafkaConsumer(
            *topics,
            bootstrap_servers=self.nodes,  # kafka集群地址
            # group_id=self.group_id,  # 消费组id
            client_id=self.client_id,  # 客户端id
            enable_auto_commit=True,  # 每过一段时间自动提交所有已消费的消息(在迭代时提交)
            auto_commit_interval_ms=5000,  # 自动提交的周期(毫秒)

            sasl_mechanism='PLAIN',
            security_protocol=self.security_protocol,
            sasl_plain_username=username,
            sasl_plain_password=pwd,
            # api_version=(0,10)
        )

    def _setPorducter(self, username=None, pwd=None):
        self.producer = KafkaProducer(
            value_serializer=lambda v: json.dumps(v).encode('utf-8'),
            bootstrap_servers=self.nodes,  # kafka集群地址

            sasl_mechanism='PLAIN',
            security_protocol=self.security_protocol,
            sasl_plain_username=username,
            sasl_plain_password=pwd,
            # api_version=(0,10)
        )

    def publish(self, channel, message):
        '''发送消息,指定频道'''

        future = self.producer.send(channel, message)
        try:
            future.get(timeout=10)
            return True, ''
        except Exception as e:
            # print(str(e))
            return False, str(e)

        # 连接对象全局存在,不用关闭
        # self.producer.close()

    def subscribe(self, topic):
        '''订阅单个频道'''
        self.consumer.subscribe(topic)

    def psubscribe(self, topics: list):
        '''订阅多个频道'''
        self.consumer.subscribe(topics)

    def unsubscribe(self):
        '''取消订阅所有主题'''
        self.consumer.unsubscribe()

    def continue_psubscribe(self, topics=[]):
        '''持续监听多个频道,返回解析数据'''
        if topics:
            self.psubscribe(topics)
        while True:
            # for msg in self.consumer:
            msg = next(self.consumer)
            t,m = msg.topic, msg.value.decode()
            return t,m

    def close(self):
        self.consumer.close(autocommit=True)
        self.producer.close()

    # def _parse_recv_data(self,res):
    #     '''数据解析'''
    #     # print("接收到的数据:", res)
    #     try:
    #         channel = res.topic
    #         data = res.value
    #
    #         print('')
    #         print('频道:', channel)
    #         print('数据:', data)
    #         return data
    #     except:
    #         print(f'【警告】收到无法解析的异常数据!!\nres:'
    #               + json.dumps(res, indent=4, ensure_ascii=False)
    #               )
    #         return False


    def recv_command(self,obj,TOPIC,handler) -> None:
        '''接收指令'''
        if TOPIC:
            self.psubscribe(TOPIC)
        while True:
            # for msg in self.consumer:
            msg = next(self.consumer)
            t,m = msg.topic, msg.value.decode()
            print(t,m)
            handler(obj,m)

    def listen(self,obj,TOPIC_handler:dict) -> bool:
        try:
            for TOPIC,handler in TOPIC_handler.items():
                print(f'【监听】频道:{TOPIC},消息监听程序启动!')
                t3 = threading.Thread(target=self.recv_command,args=(obj,TOPIC,handler))  # 单次消费
                t3.start()
            return True
        except Exception as e:
            print(e)
            return False

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神精兵院院长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值