Kafka 生产者/消费者无法使用 Spring Boot 3.1.5 连接到除 localhost:9092 之外的代理

问题描述 投票:0回答:1

我正在使用 Spring Boot 3.1.5 和 Kafka 3.0.12,尝试完全根据 https://www.baeldung.com/spring-kafka 上的示例构建应用程序。 当我在 docker 中使用本地 kafka 为我的主题启用侦听器时,它运行良好。 但是将 Kafka 移动到另一台计算机后,我在消费者上收到以下错误:

[org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] 警告 o.apache.kafka.clients.NetworkClient - [消费者 clientId=consumer-otaGrp-1, groupId=otaGrp] 连接到节点 1 (localhost /127.0.0.1:9092) 无法建立。经纪人可能不可用。

项目中-没有设置本地地址:127.0.0.1, 我不明白:为什么应用程序不使用设置的地址172.16.16.15?

应用程序属性非常简单:

server.port=8081
spring.kafka.bootstrap-servers=172.16.16.15:9092
spring.kafka.consumer.groupid=otaGrp
message.topic.name=ota_card_parameters_tracking
long.message.topic.name=longMessage
greeting.topic.name=greeting
filtered.topic.name=filtered
partitioned.topic.name=partitioned
multi.type.topic.name=multitype
test.topic=testtopic1
spring.jmx.enabled=true

这是 Consumer 方法。

@KafkaListener(topics = "${test.topic}")
public void receive(ConsumerRecord<?, ?> consumerRecord) {
    LOGGER.info("received payload='{}'", consumerRecord.toString());

    payload = consumerRecord.toString();
    latch.countDown();
}

更新:这可以依赖于 maven-plugin 重新打包吗? 从 IDE 运行良好。没有 Kafka 错误的是 [org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] 警告 o.apache.kafka.clients.NetworkClient - [消费者 clientId=consumer-testGrp-1,groupId=testGrp] 引导代理 172.16.16.15:9092 (id:-1 机架:空)已断开连接

此错误仅来自命令行: jdk-17.0.2/bin/java -jar spring-kafka-expl-1.0.0-SNAPSHOT.jar --spring.config.location=application.properties

在 pom.xml 中

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

and plugin with repackage for easi started from command line:
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.baeldung.kafka.embedded.KafkaProducerConsumerApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
spring-boot apache-kafka spring-kafka
1个回答
0
投票

要解决此问题,需要在 Kafka 端将advertisement.listeners(如果您使用的是 Docker 镜像,则设置为 KAFKA_ADVERTISED_LISTENERS)设置为外部地址(主机/IP),以便客户端可以正确连接到它。详细信息在简短有用的文章中

非常感谢@OneCricketeer 的有用评论!并链接到文章。

© www.soinside.com 2019 - 2024. All rights reserved.