我在虚拟机上安装了zookeeper和kafka docker容器。我的设置看起来像
zookeeper:
image: confluentinc/cp-zookeeper:latest
name:zookeeper
ZOOKEEPER_CLIENT_PORT: 2181
ports:
- "2181:2181"
kafka:
image: confluentinc/cp-kafka:latest
hostname: kafka
KAFKA_BROKER_ID: "-1"
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181");
ALLOW_PLAINTEXT_LISTENER: "yes"
KAFKA_LISTENERS: "PLAINTEXT://9092, PLAINTEXT_HOST://29092"
KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://kafka:9092, PLAINTEXT_HOST://localhost:29092"
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: "PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT"
KAFKA_INTER_BROKER_LISTENER_NAME: "PLAINTEXT"
KAFKA_DEFAULT_REPLICATION_FACTOR: "1"
AUTO.LEADER.REBALANCE.ENABLE: "true"
KAFKA_UNCLEAN_LEADER_ELECTION_ENABLE: "true"
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: "1"
现在我正尝试从本地计算机向kafka发送一些消息,但出现以下错误。
Connection to node -1 (//ip-of-virtualmachine:29092) could not be established. Broker may not be available.
在我的本地计算机上,我配置kafka生产者以发送消息。我的代码中配置引导服务器的属性是
String bootstrapServers = "virtual-machine-ip:29092";
Properties kafka-properties = new Properties();
kafka-properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
.
.
.
create producer and send some record etc.
我在互联网上找到的所有示例都与docker和kafka有关。它们都与kafka,docker和虚拟机无关。任何帮助,将不胜感激。
[您可能没有将kafka容器的端口暴露给主机,请尝试通过以下方式暴露端口29092:
ports:
- "29092:29092"
或在主机网络docker -d --net=host...
中运行容器,如果您是从docker-compose运行的,则将network_mode: host
添加到服务中>
要修复您的docker配置,您可以在这里获得启发:https://github.com/simplesteph/kafka-stack-docker-compose/blob/master/zk-single-kafka-single.yml
如果代码在VM外部运行,则需要使外部端口从VM转发到主机,再通过容器。