我希望我能解释这一点!
我正在启动一个停靠的java spring启动应用程序,它将连接到一个停靠的Kafka实例。
为此,我在docker-compose文件中设置了一个链接,允许应用程序在端口9092上连接到kafka docker,名为kafka-cluster。
当我启动这两个容器时,我在java应用程序中得到一个错误,说它无法连接到KafkaAdmin:
[AdminClient clientId=adminclient-2] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
但是它正在尝试连接到localhost/127.0.0.1。
在进一步的日志中,我可以看到它启动了两次到KafkaAdmin的连接:第一次:
2020-03-25 13:53:15.515 INFO 7 --- [ main] o.a.k.clients.admin.AdminClientConfig : AdminClientConfig values:
bootstrap.servers = [kafka-cluster:9092]
client.dns.lookup = default
client.id =
connections.max.idle.ms = 300000
<more properties>然后紧跟在后面(但在localhost上):
2020-03-25 13:53:15.780 INFO 7 --- [ main] o.a.k.clients.admin.AdminClientConfig : AdminClientConfig values:
bootstrap.servers = [localhost:9092]
client.dns.lookup = default
client.id =
connections.max.idle.ms = 300000
<more properties>这是配置:
@Bean
public KafkaAdmin kafkaAdmin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
return new KafkaAdmin(configs);
}
@Bean
public NewTopic sysCcukCdcAssetsCreate() {
return new NewTopic(newPanelTopic, 1, (short) 1);
}
@Bean
public NewTopic sysCcukCdcAssetsUpdate() {
return new NewTopic(updatedPanelTopic, 1, (short) 1);
}其中bootstrapServers = kafka-cluster:9092
我不明白为什么KafkaAdmin似乎有两组配置,但它似乎是导致错误的原因。
非常感谢您提供的任何指导或建议:)
发布于 2020-03-25 23:26:48
所以!这是一只很棒的橡皮鸭。
原来在属性文件中没有设置spring.kafka.bootstrap.servers属性,它默认设置为localhost,将此设置为kafka-cluster:9092 fixed it :)
https://stackoverflow.com/questions/60851128
复制相似问题