kafka内网外网问题

提问说明

我在10.10.2.17:28801上部署kafka,10.10.2.17:28801113.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"





发表于: 3月前   最后更新时间: 3月前   游览量:376
上一条: 到头了!
下一条: 已经是最后了!

  • 容器内的网络是互通的,我觉得你的映射应该不是问题。
    你用命令来消费和生产进行测试一下。
    • 我在10.10.2.19上运行produce.py(里面写的接口为10.10.2.17:28801)可以运行产生的消息,是可以发送到kafka的,我在pycharm上运行consume.py(里面写的接口为113.204.194.92:1317)时报错:“kafka.errors.NoBrokersAvailable: NoBrokersAvailable”
        • 额 你是映射,用host吧,不然你没办法通的。
          kafka是自动发现其他的节点的,虽然你做了映射,但是获取到的是内部ip。所以你就不通。