[英]My spring app can't connect to kafka broker
I'm trying to coonect my spring boot application to Kafka topic, but it seams that spring is trying to connect to to localhost and not docker ip, because I'm using docker on windows. I'm trying to coonect my spring boot application to Kafka topic, but it seams that spring is trying to connect to to localhost and not docker ip, because I'm using docker on windows. This is Kafka configuration class on spring:
这是 spring 上的 Kafka 配置 class:
@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());
}
}
My docer-compose is: version: '2'我的文档编写是:版本:'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"
The error is:错误是:
Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
is there another configuration missing in my docker compose or spring properties?我的 docker compose 或 spring 属性中是否缺少其他配置?
There's no reason to connect to "Docker IP" if you use port forwarding correctly如果您正确使用端口转发,则没有理由连接到“Docker IP”
localhost:9092
should work fine, but it seems your code isn't using the settings you've provided anyway localhost:9092
应该可以正常工作,但是您的代码似乎没有使用您提供的设置
Note: KAFKA_CREATE_TOPICS
does not do anything for Confluent images注意:
KAFKA_CREATE_TOPICS
对 Confluent 图像没有任何作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.