Here is how we did it.
Two sample classes as follow:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
@EnableKafka
@Configuration
@EnableConfigurationProperties(KafkaConsumerProperties.class)
public class KafkaContainerConfig {
@Autowired
protected KafkaConsumerProperties kafkaConsumerProperties;
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(kafkaConsumerProperties.getKafkaConsumerConfig());
}
...
@Configuration
@ConfigurationProperties
public class KafkaConsumerProperties {
protected Map<String, Object> kafkaConsumerConfig = new HashMap<>();
@ConfigurationProperties("kafkaConsumerConfig")
public Map<String, Object> getKafkaConsumerConfig() {
return (kafkaConsumerConfig);
}
...
To provide the kafkaConsumer config from a properties file, you can use: mapname[key]=value
//application.properties
kafkaConsumerConfig[bootstrap.servers]=localhost:9092, localhost:9093, localhost:9094
kafkaConsumerConfig[group.id]=test-consumer-group-local
kafkaConsumerConfig[value.deserializer]=org.apache.kafka.common.serialization.StringDeserializer
kafkaConsumerConfig[key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
To provide the kafkaConsumer config from a yaml file, you can use "[key]": value
In application.yml file:
kafkaConsumerConfig:
"[bootstrap.servers]": localhost:9092, localhost:9093, localhost:9094
"[group.id]": test-consumer-group-local
"[value.deserializer]": org.apache.kafka.common.serialization.StringDeserializer
"[key.deserializer]": org.apache.kafka.common.serialization.StringDeserializer
Pattern
? What kind ofPattern
class is that? – K Erlandsson