所以我在主机上启动了一个kafka和zookeeper示例。现在我想通过两个Docker与之交互——生产者和消费者
producer docker中的代码是:
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)
消费者docker的代码是:
from kafka import KafkaConsumer
consumer = KafkaConsumer(bootstrap_servers=['localhost:9092'])
consumer.subscribe(['stupid'])
for message in consumer:
print (message.value)
消费者的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条答案
按热度按时间ev7lccsx1#
检查播发主机名,运行方式:
crcmnpdw2#
尝试: