Repository navigation
Expand file tree
/
Copy pathconsumer.py
More file actions
56 lines (46 loc) · 1.45 KB
/
Copy pathconsumer.py
File metadata and controls
56 lines (46 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
"""
Python 消费者:从 Kafka 消费消息
可接收来自 Go 生产者 (go-queue kq) 发送的消息
"""
import json
import signal
import sys
from kafka import KafkaConsumer
from kafka.errors import KafkaError
BROKERS = ["127.0.0.1:9092"]
TOPIC = "go-python-topic"
GROUP_ID = "python-consumer-group"
def main():
consumer = KafkaConsumer(
TOPIC,
bootstrap_servers=BROKERS,
group_id=GROUP_ID,
auto_offset_reset="earliest",
value_deserializer=lambda m: m.decode("utf-8"),
)
def shutdown(sig, frame):
consumer.close()
print("\n消费者已停止")
sys.exit(0)
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
print("Python Consumer 已启动,等待消息 (Ctrl+C 退出)")
print(f"Topic: {TOPIC}")
print("---")
try:
for message in consumer:
try:
msg = json.loads(message.value)
source = msg.get("source", "unknown")
content = msg.get("content", message.value)
ts = msg.get("timestamp", "")
print(f"[Python Consumer] 收到来自 {source}: {content} (时间: {ts})")
except json.JSONDecodeError:
print(f"[Python Consumer] 收到: {message.value}")
except KafkaError as e:
print(f"Kafka 错误: {e}")
finally:
consumer.close()
if __name__ == "__main__":
main()