NoBrokersAvailableError

pykafka使用中报错:

NoBrokersAvailableError


解决方法:

C:\Windows\System32\drivers\etc\hosts,添加映射

192.168.00.60 KAFKA-VN00


# -- coding: utf-8 --


# from kafka import KafkaProducer
#
# producer = KafkaProducer(bootstrap_servers=['192.168.55.60:9092','192.168.55.61:9092','192.168.55.62:9092'])  #此处ip可以是多个['0.0.0.1:9092','0.0.0.2:9092','0.0.0.3:9092' ]
#
# for i in range(3):
#     msg = "msg%d" % i
#     producer.send('test', msg)
# producer.close()

from pykafka import KafkaClient
# from kafka import KafkaProducer

import json
import logging
import time
import sys


host='192.168.00.00:9092,192.168.00.01:9092,192.168.00.02:9092'
client = KafkaClient(hosts=host)

print("135")
topic = client.topics[b'test']

producer = topic.get_producer()
producer.start()

# 生产消息
msg_dict = {
    "sleep_time": 10,
    "db_config": {
        "database": "test",
        "host": "192.168.137.12",
        "user": "root",
        "password": "root"
    },
    "table": "msg",
    "msg": "Hello World"
}
msg = json.dumps(msg_dict)
msg=bytes(msg, encoding = "utf8")
for i in range(100):
    producer.produce(msg)
producer.stop()

你可能感兴趣的:(python)