Faust-streaming入门Demo报错:'CreateTopicsRequest_v1'无build_request_header属性
解决方案:faust-streaming 连接 Redpanda 出现 AttributeError 问题
问题原因
该错误源于 faust-streaming 依赖的 Kafka 客户端库(如 aiokafka)与 Redpanda 的 API 交互不兼容——主题创建请求的方法调用在新版本客户端库中被修改,但 faust 代码仍在调用旧方法。
解决步骤
1. 手动创建所需主题
通过 Redpanda 的 rpk 工具预先创建 greetings 主题,跳过 faust 自动创建主题的逻辑:
rpk topic create greetings --brokers localhost:9092
2. 修改 faust 应用配置,关闭自动主题创建
在代码中添加配置项禁用自动主题创建,避免触发报错的逻辑:
import faust app = faust.App( 'hello-world', broker='kafka://localhost:9092', value_serializer='raw', auto_create_topics=False, # 新增配置关闭自动创建主题 ) greetings_topic = app.topic('greetings') @app.agent(greetings_topic) async def greet(greetings): async for greeting in greetings: print(greeting)
3. 锁定兼容的依赖版本
若上述方法无效,调整依赖版本至兼容组合,在 requirements.txt 中指定:
faust-streaming==0.10.16 aiokafka==0.8.0
执行安装命令更新依赖:
pip install -r requirements.txt
4. 调整 Kafka 协议版本(可选)
如果 Redpanda 支持的 Kafka 协议版本与 faust 默认设置不匹配,可在配置中指定对应版本:
app = faust.App( 'hello-world', broker='kafka://localhost:9092', value_serializer='raw', auto_create_topics=False, broker_kwargs={ 'api_version': (2, 8, 0), # 匹配 Redpanda 支持的 Kafka 协议版本 }, )
验证运行
重新启动 faust worker:
faust -A hello_world worker -l info
使用 rpk 发送测试消息验证功能:
rpk topic produce greetings --brokers localhost:9092 # 输入消息后按 Ctrl+D 发送
内容的提问来源于stack exchange,提问作者T1mminat0r
相关产品推荐
相关产品推荐

