我的docker-compose.yml是
version: "2.1"
services:
pg-master:
image: postgres-9.6:3
ports:
- "5432:5432"
environment:
- VIRTUAL_HOST=postgres.local
web:
build: .
links:
- pg-master:postgres.local
ports:
- "8080:8080"
zookeeper:
image: jplock/zookeeper:3.4.6
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka:1.0.0
ports:
- "9092:9092"
environment:
- KAFKA_ADVERTISED_HOST_NAME=localhost
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_CREATE_TOPICS=TestTopic1:3:1
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_BROKER_ID=999
volumes:
- /var/run/docker.sock:/var/run/docker.sock
在web docker容器中,java kafka producer:
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
this.kafkaProducer = new KafkaProducer<String, String>(props);
但是,我得到了[kafka-producer-network-thread | producer-2] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-2] Connection to node -1 could not be established. Broker may not be available.
不知道为什么。欢迎任何建议。非常感谢。
更新
我可以通过kafka docker容器内外的bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic TestTopic1 --from-beginning
和bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TestTopic1
生成和使用消息。
docker container中的localhost
是什么?
更新
现在,我手动将Kafka Java生成器配置的KAFKA_ADVERTISED_HOST_NAME
和bootstrap.servers
更改为docker host ip(由ifconfig
获取)。
现在,它正在发挥作用。现在,我不知道如何通过程序或bash自动获取docker主机ip并将其设置为docker-compose文件。