繁体   English   中英

我的 spring 应用程序无法连接到 kafka 代理

[英]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. 这是 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());
}
}

我的文档编写是:版本:'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 IP”

localhost:9092应该可以正常工作,但是您的代码似乎没有使用您提供的设置

注意: KAFKA_CREATE_TOPICS对 Confluent 图像没有任何作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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