我在10.10.2.17:28801
上部署kafka,10.10.2.17:28801
和113.204.194.92:1317
是映射关系,我的消费者是192.168.1.164
,怎么实现消费者可以从kafka上收到消息呢?我现在可以从10.10.2.19(producer)
产生消息发送到kafka(10.10.2.17:28801)
,但是作为192.168.1.164
(消费者),通过访问113.204.194.92:1317应该可以从kafka里收到消息呀?我用的是docker-compose.yml部署的kafka,刚接触kafka,还有很多不明白的地方,还请大佬多多指教,谢谢!!!
贴上相关代码
1.producer:
import time
from kafka import SimpleProducer, KafkaClient
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers = ['10.10.2.17:28801'])
# Assign a topic
topic = 'test_kafka'
def test():
print('begin')
n = 1
while (n<=100):
producer.send(topic, str(n).encode("utf-8"))
print("send" + str(n))
n += 1
time.sleep(0.5)
print('done')
if __name__ == '__main__':
test()
2. consumer:
from kafka import KafkaConsumer
#connect to Kafka server and pass the topic we want to consume
consumer = KafkaConsumer('test_kafka', bootstrap_servers = ['113.204.194.92:1317'])
print("waitting for receiving message:.......")
for msg in consumer:
print(msg.value)
3. docker-compose.yml:
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
environment:
JMX: 9000
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "28801:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 10.10.2.17
KAFKA_CREATE_TOPICS: "test_kafka"
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
kafka-manager:
image: sheepkiller/kafka-manager
links:
- kafka
- zookeeper
environment:
ZK_HOSTS: zookeeper:2181
APPLICATION_SECRET: "letmein"
ports:
- "9000:9000"
expose:
- "9000"