Search code examples
spring-bootapache-kafkaspring-kafkakafka-producer-api

Kafka Producer AWSSchemaRegistryConstants properties for YAML File


I have set up a Kakfa producer using my own ProducerFactory bean, it uses AWS Glue Schema Registry to register AVRO schemas, I want to move all the properties to a YML file but I dont know how to declare AWSSchemaRegistryConstants properties in a yml file, any ideas? The code looks as follow:

@Configuration
@EnableKafka
class KafkaProducerConfig {

 @Bean
 fun producerConfigs(): Map<String, Any> {
    val props: MutableMap<String, Any> = HashMap()
    props[ProducerConfig.BOOTSTRAP_SERVERS_CONFIG] = "localhost:9092"
    props[ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG] = IntegerSerializer::class.java
    props[ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG] = AWSKafkaAvroSerializer::class.java
    props[AWSSchemaRegistryConstants.AWS_REGION] = "ap-southeast-2"
    props[AWSSchemaRegistryConstants.SCHEMA_AUTO_REGISTRATION_SETTING] = "true"
    props[AWSSchemaRegistryConstants.SCHEMA_NAME] = "ac-schema"
    props[AWSSchemaRegistryConstants.REGISTRY_NAME] = "poc-registry"
    return props
}

@Bean
 fun producerFactory(): ProducerFactory<Int, AcCk> {
    return DefaultKafkaProducerFactory(producerConfigs())
}

@Bean
 fun kafkaTemplate(): KafkaTemplate<Int, AcCk> {
    return KafkaTemplate(producerFactory())
 }

}

YML File:

spring:
 kafka:
  producer:
   bootstrap-servers: localhost:9092
   key:
     serializer: org.apache.kafka.common.serialization.IntegerSerializer
   value:
     serializer: com.amazonaws.services.schemaregistry.serializers.avro.AWSKafkaAvroSerializer

   AWSSchemaRegistryConstants ?????? 

Solution

  • The indentation under producer is correct, however, you need to open the enum/class for AWSSchemaRegistryConstants and see what each of the properties are and copy the strings over into the config. What you're basically asking for is no different than trying to use ProducerConfig object in the YAML, for example

    Or you can continue to build KafkaTemplate in code rather than use YAML since that'll allow you to have compile time property validation and use the enum class