Skip to content

KafkaProducer fails to bind to embedded broker and defaults to localhost:9092 #304

Description

@ZenithDev1

Summary:
When executing TaskTwoTests, the Kafka consumer correctly detects and binds to the dynamic embedded Kafka port, but the KafkaProducer fails to establish a connection because it attempts to connect to the hardcoded/default localhost:9092.

Detailed Behavior:

  • The @embeddedkafka environment assigns a randomized dynamic port at runtime.
  • The default KafkaTemplate utilized by KafkaProducer ignores the test environment properties and falls back to localhost:9092, resulting in continuous connection timeout warnings and disconnect loops (Node -1 disconnected).
  • Students have to manually create a custom @configuration class (KafkaConfig.java) with an explicit KafkaTemplate bean to override this behavior, which adds unnecessary friction to the assignment.

Workaround Applied:
To resolve this locally and pass the test, I had to create a new config package inside com.jpmc.midascore and define a custom KafkaConfig class with an explicit KafkaTemplate<String, Transaction> bean mapped to ${spring.embedded.kafka.brokers}:

  • Package: com.jpmc.midascore.config
  • Class: KafkaConfig.java (handling explicit producer factory and kafka template bindings).

Expected Behavior:
The base repository setup should seamlessly wire the KafkaProducer and KafkaTemplate to the embedded Kafka instance during tests without requiring students to write custom infrastructure configuration code.

Suggested Improvement for Repository:
Consider adding a @TestConfiguration or @DynamicPropertySource directly in the test suite utilities to automatically map the producer factory and template to ${spring.embedded.kafka.brokers} out-of-the-box, saving students from writing extra configuration boilerplate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions