0

I'm facing an issue where my Kafka ProducerConfig is getting an invalid bootstrap.servers value because my unit test @PropertySource isn't resolving the spring.embedded.kafka.brokers property. When I dump my producer config to the logs, I get the following:

acks = 0
batch.size = 10000
bootstrap.servers = [${spring.embedded.kafka.brokers}]
...

Clearly, the property isn't getting resolved. Suppose I have the following embedded Kafka test.

@EmbeddedKafka
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
public class EmbeddedKafkaTest {

    @Value("${spring.embedded.kafka.brokers}")
    private String embeddedKafkaBrokers;

    @Test
    public void test(){}

    @SpringBootConfiguration
    @PropertySource("classpath:kafkaTestProps.properties")
    @EnableAutoConfiguration
    class EmbeddedKafkaTestConfiguration {
    }

}

and my kafkaTestProperties.properties file is as follows:

embedded-kafka-brokers=${spring.embedded.kafka.brokers}
...

embedded-kafka-brokers will eventually be provided to Kafka's ProducerConfig through some underlying autoconfiguration.

Interestingly, the embeddedKafkaBrokers instance field in the test class does contain the broker IPs set by embedded kafka.

I've concluded there's an issue with the property source loading order, where @EmbeddedKafka isn't setting the broker IP system property in time for kafkaTestProperties.properties to resolve it. This problem arose after porting our code base to Spring Boot 2 and Spring Cloud Finchley SR2, where Spring Kafka APIs have upgraded.

I've tried removing @SpringBootTest and wrapping the test() code in a SpringApplicationBuilder to no avail.

Any advice as to how I can fix this? Perhaps I can leverage @AutoConfigurationAfter or @Order? Is there a way to order the loading of property sources?

blackcompe
  • 3,180
  • 16
  • 27

1 Answers1

0

It looks that your placeholder is not valid. Try to replace:

embedded-kafka-brokers=${"spring.embedded.kafka.brokers"}

with:

embedded-kafka-brokers=${spring.embedded.kafka.brokers}

and provide it to the producer using class with @ConfigurationProperties or using @Value("${embedded-kafka-brokers}").

documentation link