Search code examples
javaspring-bootapache-kafkaspring-kafka

Can't connect to Kafka broker using Spring Boot


I have a standard Spring Boot setup with some Kafka configuration that has a @Listener in it:

@SpringBootApplication
public class MyApp {

  public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
  }

}
@EnableKafka
@Configuration
class KafkaConfig {

    @KafkaListener(id = "my_id", groupId = "my_id", topics = "some_topic")
    public void listen(String in) {
        // ...
    }
}

and when I start this up I don't see a new consumer appear on the Kafka admin UI, but I see this repeated ad nauseam in the log:

2023-04-21 09:21:50.043  INFO 14284 --- [ e2e_test-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-my_id-1, groupId=my_id] Node -1 disconnected.
2023-04-21 09:21:50.043  WARN 14284 --- [ e2e_test-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-my_id-1, groupId=my_id] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
2023-04-21 09:21:50.043  WARN 14284 --- [ e2e_test-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-my_id-1, groupId=my_id] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected

Why is it trying to connect to a broker at localhost when I configured it to connect elsewhere in my application.yml?

config:
  kafka:
    servers: "my.server:9092"
    ssl: false

Solution

  • Kafka client in Spring Boot is controlled by the configuration properties with the prefix spring.kafka.*:

    spring:
      kafka:
        bootstrap-servers: my.server:9092
    

    See docs: https://docs.spring.io/spring-kafka/reference/html/