Package: kafkaproxy
kafkaproxykafka生产者和消费者的代理工具.代理对象用于推迟初始化.我们可以在需要的地方用代理对象的全局变量直接编写逻辑,避免被代理的对象来回在函数间传递.特性支持代理kafka-python,aiokafka和confluent-kafka的生产者消费者对象.提供统一通用的生产者消费者接口包装安装只安装本项目不安装被代理对象的依赖:pip install kafkaproxy安装本项目同时确定要代理的对象包为kafka-python:pip install kafkaproxy[kafka]安装本项目同时确定要代理的对象包为aiokafka:pip install kafkaproxy[aio]安装本项目同时确定要代理的对象包为confluent-kafka:pip install kafkaproxy[confluent]使用本项目支持代理3种kafka模块中的对应模块,使用枚举KafkaType中的取值在调用方法initialize_from_addresses初始化时指定.
代理对象除了原样代理对象外还提供了生产者和消费者的统一通用接口包装.
由于对应的方法是动态绑定的,因此如果需要他们的typehints可以用typing.cast将代理对象转化为对应的协议对象同步接口生产者使用ProducerProtocol异步接口生产者使用AioProducerProtocol同步接消费产者使用ConsumerProtocol异步接消费产者使用AioConsumerProtocol代理kafka-python或confluent-kafka生产者fromkafkaproxyimportProducerProxy,KafkaType,ProducerProtocolfromtypingimportcastimporttimekafkap=ProducerProxy()defrun()->None:p=cast(ProducerProtocol,kafkap)withp.mount()ascli:foriinrange(10):cli.publish("topic1",f"send{i}")time.sleep(0.1)# kafkap.initialize_from_addresses("localhost:9094", kafka_type=KafkaType.ConfluentKafka, acks="all")kafkap.initialize_from_addresses("localhost:9094",kafka_type=KafkaType.Kafka)try:print("start publishing")run()finally:print("stoped")代理kafka-python或confluent-kafka消费者fromkafkaproxyimportConsumerProxy,KafkaType,ConsumerProtocolfromtypingimportcastkafkac=ConsumerProxy()defrun()->None:c=cast(ConsumerProtocol,kafkac)withc.watch()asg:forrecording:print(record.value)# kafkac.initialize_from_addresses("localhost:9094", "topic1", group_id="test2", kafka_type=KafkaType.Kafka)kafkac.initialize_from_addresses("localhost:9094","topic1",group_id="test2",kafka_type=KafkaType.ConfluentKafka)try:print("start watching")run()finally:print("stoped")代理aiokafka生产者importasynciofromkafkaproxyimportProducerProxy,KafkaType,AioProducerProtocolfromtypingimportcastkafkap=ProducerProxy()asyncdefrun()->None:p=cast(AioProducerProtocol,kafkap)asyncwithp.mount()ascli:foriinrange(10):awaitcli.publish("topic1",f"send{i}")awaitasyncio.sleep(0.1)asyncdefmain()->None:kafkap.initialize_from_addresses("localhost:9094",kafka_type=KafkaType.AioKafka,acks="all")awaitrun()try:print("start watching")asyncio.run(main())finally:print("stoped")代理aiokafka消费者importasynciofromkafkaproxyimportConsumerProxy,KafkaAutoOffsetReset,KafkaType,AioConsumerProtocolfromtypingimportcastkafkac=ConsumerProxy()asyncdefrun()->None:c=cast(AioConsumerProtocol,kafkac)asyncwithc.watch()asg:asyncforrecording:print(record.value)asyncdefmain()->None:kafkac.initialize_from_addresses("localhost:9094","topic1",group_id="test2",kafka_type=KafkaType.AioKafka,auto_offset_reset=KafkaAutoOffsetReset.earliest)awaitrun()try:print("start watching")asyncio.run(main())finally:print("stoped")v0.0.1项目创建
MIT LicenseCopyright (c) 2023 Python-ToolsPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.