简体   繁体   中英

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. This is Kafka configuration class on spring:

@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'

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?

There's no reason to connect to "Docker IP" if you use port forwarding correctly

localhost:9092 should work fine, but it seems your code isn't using the settings you've provided anyway

Note: KAFKA_CREATE_TOPICS does not do anything for Confluent images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM