Kafka没有与docker

时间:2016-08-12 01:25:37

标签: python docker apache-kafka

所以我在主机上启动了kafka和zookeeper实例。现在我想通过两个码头工人 - 生产者和消费者

与之互动

PRODUCER泊坞窗中的代码为:

from kafka import KafkaProducer
import time
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])

i = 0
while 1:
  # "kafkaesque" is the name of our topic
  producer.send("stupid", str(i))
  i += 1
  time.sleep(1)

CONSUMER docker的代码是:

from kafka import KafkaConsumer

consumer = KafkaConsumer(bootstrap_servers=['localhost:9092'])
consumer.subscribe(['stupid'])

for message in consumer:
    print (message.value)

CONSUMER的Dockerfile是:

FROM debian:jessie
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y python-setuptools
RUN apt-get install -y python-pip
RUN pip install kafka-python
ADD . /Consumer
WORKDIR /Consumer
CMD ["python", "consumer.py"]

PRODUCER的Dockerfile是:

FROM debian:jessie
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y python-setuptools
RUN apt-get install -y python-pip
RUN pip install kafka-python
ADD . /Producer
WORKDIR /Producer
CMD ["python","counter.py"]

现在我构建了它们,当我运行它时,就像这样:

docker run consumer 
docker run producer

然后对于每一个我得到这个错误:

Traceback (most recent call last):
  File "consumer.py", line 3, in <module>
    consumer = KafkaConsumer(bootstrap_servers=['localhost:9092'])
  File "/usr/local/lib/python2.7/dist-packages/kafka/consumer/group.py", line 284, in __init__
    self._client = KafkaClient(metrics=self._metrics, **self.config)
  File "/usr/local/lib/python2.7/dist-packages/kafka/client_async.py", line 202, in __init__
    self.config['api_version'] = self.check_version(timeout=check_timeout)
  File "/usr/local/lib/python2.7/dist-packages/kafka/client_async.py", line 791, in check_version
    raise Errors.NoBrokersAvailable()
kafka.errors.NoBrokersAvailable: NoBrokersAvailable

**

  

但是当我在主机上运行它们时,它们完美地工作。

**

有人可以指出,如何解决这个问题。我有一些想法,因为在docker的网络端口无法与主机的网络端口进行交互,但我尝试过EXPOSE和-p,每个都没有帮助我。

2 个答案:

答案 0 :(得分:1)

检查Advertised hostname,运行时间:

-e KAFKA_ADVERTISED_HOST_NAME=kafka

答案 1 :(得分:-1)

尝试:

(bootstrap_servers=[], api_version=(0, 9)))