SpringBoot无法读取application.yml中Kafka的bootstrap-servers问题排查
问题:SpringBoot无法读取Confluent Kafka配置,默认连接localhost:9092
SpringBoot应用可正常读取邮件等其他配置,但无法读取application.yml中配置的Confluent Kafka凭证及bootstrap-servers,注释凭证后问题依旧。telnet测试目标bootstrap-server连接正常,pom.xml已包含必要依赖,调用接口时日志显示尝试连接localhost:9092并超时。
现有application.yml Kafka配置
kafka: properties: sasl: mechanism: PLAIN jaas: enabled: true config: org.apache.kafka.common.security.plain.PlainLoginModule required username='<redacted>' password='<redacted>'; bootstrap-servers: pkc-12p03.xxxxxx.azure.confluent.cloud:9092 security: protocol: SASL_SSL session: timeout: ms: 5000 basic: auth: credentials: source: USER_INFO user: info: <redacted>:<redacted> schema: registry: url: https://pkc-12p03.xxxx.azure.confluent.cloud:443
接口调用日志
2023-07-26 15:43:10 [,] - INFO [Producer clientId=producer-1] Node -1 disconnected. 2023-07-26 15:43:10 [,] - WARN [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2023-07-26 15:43:10 [,] - WARN [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2023-07-26 15:43:11 [,] - INFO [Producer clientId=producer-1] Node -1 disconnected. 2023-07-26 15:43:11 [,] - WARN [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2023-07-26 15:43:11 [,] - WARN [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2023-07-26 15:43:12 [,] - INFO [Producer clientId=producer-1] Node -1 disconnected. 2023-07-26 15:43:12 [,] - WARN [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2023-07-26 15:43:12 [,] - WARN [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
Kafka使用代码
public class MailServiceImpl implements MailService { private final JavaMailSender javaMailSender; private final MailConfiguration mailConfiguration; private final KafkaTemplate<String, MailRequestDto> kafkaTemplate; private final String topicName = "email_topic"; @Override public BaseResponse<?> sendMail( MailRequestDto emailRequestDto) { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message); try { mimeMessageHelper.setFrom(mailConfiguration.getMailUsername()); mimeMessageHelper.setTo(emailRequestDto.getRecipient()); mimeMessageHelper.setSubject(emailRequestDto.getSubject()); mimeMessageHelper.setText(emailRequestDto.getText()); javaMailSender.send(message); kafkaTemplate.send(topicName, emailRequestDto); Map<String, Object> responseData = buildResponseData(emailRequestDto); return buildSuccessResponse(responseData); } catch (MessagingException e) { return buildErrorResponse(e.getLocalizedMessage()); } } }
问题根源与解决方案
1. 配置层级错误(核心问题)
Spring Boot中,bootstrap-servers是Kafka的核心配置项,必须直接放在spring.kafka节点下,而非嵌套在spring.kafka.properties中。当前配置将其放在properties里,导致Spring Boot无法识别该配置,只能使用默认值localhost:9092。
2. 冗余配置修正
- Confluent Cloud不需要手动设置
sasl.jaas.enabled,该配置无实际作用,可删除。 basic.auth相关配置是Schema Registry的专属配置,无需放在Kafka的properties下,建议单独提取到spring.kafka.schema-registry节点(如果使用Spring Cloud Schema Registry),或者保持在properties中但确认依赖支持。
3. 修正后的正确配置示例
spring: kafka: bootstrap-servers: pkc-12p03.xxxxxx.azure.confluent.cloud:9092 properties: sasl: mechanism: PLAIN jaas: config: org.apache.kafka.common.security.plain.PlainLoginModule required username='<redacted>' password='<redacted>'; security: protocol: SASL_SSL session: timeout: ms: 5000 schema: registry: url: https://pkc-12p03.xxxx.azure.confluent.cloud:443 basic: auth: credentials: source: USER_INFO user: info: <redacted>:<redacted>
4. 验证配置加载
可在代码中注入KafkaProperties并打印,确认配置是否被正确读取:
@Autowired private KafkaProperties kafkaProperties; // 在初始化或接口中打印 System.out.println("Bootstrap Servers: " + kafkaProperties.getBootstrapServers());
内容的提问来源于stack exchange,提问作者CuriousCat
相关产品推荐
相关产品推荐

