Kafka Consumer找不到任何可用的Kafka brokers来连接。我希望能够观察日志,因为当我使用docker-compose up -d时,启动Kafka和zookeeper后创建的主题正在退出错误如下:
Traceback (most recent call last):
File "/app/servico_pagamento.py", line 22, in <module>
enviar_pagamento()
File "/app/servico_pagamento.py", line 5, in enviar_pagamento
producer = KafkaProducer(
File "/usr/local/lib/python3.9/site-packages/kafka/producer/kafka.py", line 381, in __init__
client = KafkaClient(metrics=self._metrics, metric_group_prefix='producer',
File "/usr/local/lib/python3.9/site-packages/kafka/client_async.py", line 244, in __init__
self.config['api_version'] = self.check_version(timeout=check_timeout)
File "/usr/local/lib/python3.9/site-packages/kafka/client_async.py", line 900, in check_version
raise Errors.NoBrokersAvailable()
kafka.errors.NoBrokersAvailable: NoBrokersAvailable
字符串
如果你需要.yaml文件:
version: '3'
services:
zookeeper:
image: 'bitnami/zookeeper:latest'
ports:
- "2181:2181"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
networks:
- kafka-network
kafka:
image: 'bitnami/kafka:latest'
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- zookeeper
networks:
- kafka-network
pagamento:
build:
context: .
dockerfile: Dockerfile.pagamento
depends_on:
- kafka
networks:
- kafka-network
networks:
kafka-network:
driver: bridge
型
下面是.py相关代码
from kafka import KafkaProducer
import json
def enviar_pagamento():
producer = KafkaProducer(
bootstrap_servers='kafka:9092',
api_version=(0,11,5),
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)
pagamento = {
'id_pedido': 123,
'valor': 50.0,
'status': 'pendente'
}
producer.send('pagamentos_email', value=pagamento)
producer.send('pagamentos_notificacao', value=pagamento)
producer.flush()
if __name__ == "__main__":
enviar_pagamento()
型
1条答案
按热度按时间3qpi33ja1#
1.您的Python代码无法连接到启动时未运行或未健康的服务器。您必须等待或手动重试循环连接
启动Kafka和zookeeper后创建的主题正在退出
如果Kafka退出,则无法创建主题。如前所述,Zookeeper未被使用
我希望能够观察到
删除
-d