【问题标题】:My spring app can't connect to kafka broker我的 spring 应用程序无法连接到 kafka 代理
【发布时间】: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


【解决方案1】:

如果你正确使用端口转发,就没有理由连接到“Doc​​ker IP”

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

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

【讨论】:

    猜你喜欢
    • 2020-01-21
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2018-10-18
    • 2021-12-22
    • 2019-08-06
    • 1970-01-01
    • 2020-06-02
    相关资源
    最近更新 更多