Search code examples
javaspring-bootapache-kafkaspring-kafka

why is springboot not picking up apache kafka bootstrap-servers in application.yml?


My Springboot application does not seem to pick up the credentials provided in the application.yml for confluent kafka credentials. It is able to pick for other credentials like email apart from Kafka confluent which makes me realise that the issue is coming from my configuration. I pasted what was made available to me on their platform.

I have tried to comment out the credentials, I noticed the application gives the same issues. Here is my configuration.

  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

This is what I get in response when I try to hit the endpoint

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

It times out. This is how I am using Kafka. It is a simple usage.

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());
       }
    }

}

Doing a telnet on my bootstrap-server, I get success message. What could be wrong please? I have the necessary dependencies in my pom.xml


Solution

  • Please learn how to use SO markdown to format code (see my edits).

    You need spring: as the first element.

    bootstrap-servers should NOT be under properties, just under spring.kafka.

    properties is for kafka properties that are not exposed directly by Boot.

    https://docs.spring.io/spring-boot/docs/current/reference/html/messaging.html#messaging.kafka.additional-properties

    Otherwise, Spring Boot will default to 127.0.0.1:9092