【发布时间】:2020-11-17 07:32:08
【问题描述】:
我正在尝试将我的 Spring Boot 应用程序连接到 Kafka 主题,但它表明 spring 正在尝试连接到 localhost 而不是 docker ip,因为我在 Windows 上使用 docker。 这是春季的Kafka配置类:
@Configuration
public class KafkaConfig {
@Bean
public ProducerFactory<String,String> producerFactory()
{
Map<String,Object> config = new HashMap<>();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.99.100:9092");
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(config);
}
@Bean
public KafkaTemplate kafkaTemplate()
{
return new KafkaTemplate<>(producerFactory());
}
}
我的文档编写是: 版本:'2'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
ports:
- 2181:2181
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
kafka:
image: confluentinc/cp-kafka:latest
ports:
- 9092:9092
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_CREATE_TOPICS: "scraper:2:4,Topic2:1:1:compact"
KAFKA_ADVERTISED_LISTENERS: LISTENER_INSIDE://kafka:29092,LISTENER_HOST://192.168.99.100:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: LISTENER_INSIDE:PLAINTEXT,LISTENER_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: LISTENER_INSIDE
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_HEAP_OPTS: " -Xmx256m -Xms256m"
错误是:
Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
我的 docker compose 或 spring 属性中是否缺少其他配置?
【问题讨论】:
-
应用程序是否在 Docker 容器本身内运行?
-
你能解决这个问题吗?
标签: spring docker apache-kafka