Fix startup failure when using VaultPropertySource + reactive (#1994)

Fixes gh-1990

When using `@ValuePropertySource` in reactive web app,
`KafkaSender` bean is getting created before `BeanPostProcessor` populate `@Value`
and as a result topic is passed as `null` in `KafkaSender.Builder` and it throws `NullPointerException`

To resolve this we are using `Environment` to get the property value instead on relying on `BeanPostProcessor`
to populate `@Value`
This commit is contained in:
Chintan Radia
2021-07-13 22:44:21 +05:30
committed by GitHub
parent 3793c58fe8
commit 87fac14505

View File

@@ -20,10 +20,10 @@ import java.util.List;
import java.util.Map;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.springframework.core.env.Environment;
import zipkin2.reporter.Sender;
import zipkin2.reporter.kafka.KafkaSender;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -44,9 +44,6 @@ class ZipkinKafkaSenderConfiguration {
@EnableConfigurationProperties(KafkaProperties.class)
static class ZipkinKafkaSenderBeanConfiguration {
@Value("${spring.zipkin.kafka.topic:zipkin}")
private String topic;
static String join(List<?> parts) {
StringBuilder to = new StringBuilder();
for (int i = 0, length = parts.size(); i < length; i++) {
@@ -59,7 +56,12 @@ class ZipkinKafkaSenderConfiguration {
}
@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME)
Sender kafkaSender(KafkaProperties config) {
Sender kafkaSender(KafkaProperties config, Environment environment) {
// Need to get property value from Environment
// because when using @VaultPropertySource in reactive web app
// this bean is initiated before @Value is resolved
// See gh-1990
String topic = environment.getProperty("spring.zipkin.kafka.topic", "zipkin");
Map<String, Object> properties = config.buildProducerProperties();
properties.put("key.serializer", ByteArraySerializer.class.getName());
properties.put("value.serializer", ByteArraySerializer.class.getName());
@@ -68,7 +70,7 @@ class ZipkinKafkaSenderConfiguration {
if (bootstrapServers instanceof List) {
properties.put("bootstrap.servers", join((List) bootstrapServers));
}
return KafkaSender.newBuilder().topic(this.topic).overrides(properties).build();
return KafkaSender.newBuilder().topic(topic).overrides(properties).build();
}
}