0

I cannot load StringDeserializer and StringSerializer with PropertySource, but all other spring.kafka related things get loaded.

Is it a bug in spring PropertySource?

My common application look like this:

@Configuration
@PropertySource("classpath:/config/kafka.properties")
public class CommonKafkaAutoConfiguration {
}

Below configuration doesn't get loaded when filename kafka.properties but okay when application.properties

spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer

Funny fact is following get loaded in both cases:

spring.kafka.consumer.max-poll-records=20

Update I can see that its get overriden by kafkaBinderDefaultProperties

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Dasma
  • 1,023
  • 13
  • 34
  • What version of Boot? I just tried it with 2.4.1 with no problems. Also, Boot auto configures the `String(Des|S)erializer` for both key and value by default. – Gary Russell Dec 15 '20 at 18:08
  • I tried with spring-boot 2.3.7 and 2.4.1 both have same error. – Dasma Dec 15 '20 at 18:30
  • What makes you think it's not working? - as I said, they are `String(Des|S)erializer`s by default. – Gary Russell Dec 15 '20 at 18:33
  • Because of test... in running application and Spock test... I can see it doesn't get loaded when called kafka.properties but okay application.properties Further on... I have narrowed it down to CommonKafkaAutoConfiguration – Dasma Dec 15 '20 at 18:43
  • I update my issue... it load byteserializer – Dasma Dec 15 '20 at 18:44

2 Answers2

0

This works fine for me...

@SpringBootApplication
@PropertySource("classpath:/config/kafka.properties")
public class So65311053Application {

    public static void main(String[] args) {
        SpringApplication.run(So65311053Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template, ConsumerFactory<?, ?> cf) {
        return args -> {
            template.send("so65311053", "foo");

            System.out.println(template.getProducerFactory()
                    .getConfigurationProperties()
                    .get(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG));

            System.out.println(cf.getConfigurationProperties()
                    .get(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG));
        };
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so65311053").partitions(1).replicas(1).build();
    }

    @KafkaListener(id = "so65311053", topics = "so65311053")
    void listen(String in) {
        System.out.println(in);
    }

}
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.IntegerDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.IntegerSerializer

spring.kafka.consumer.auto-offset-reset=earliest
2020-12-15 13:38:49.154  INFO 4612 --- [           main] o.a.k.clients.consumer.ConsumerConfig    : ConsumerConfig values: 
    allow.auto.create.topics = true
    auto.commit.interval.ms = 5000
    auto.offset.reset = earliest
    ...
    key.deserializer = class org.apache.kafka.common.serialization.IntegerDeserializer
    ...
2020-12-15 13:38:49.211  INFO 4612 --- [           main] o.a.k.clients.producer.ProducerConfig    : ProducerConfig values: 
    ...
    key.serializer = class org.apache.kafka.common.serialization.IntegerSerializer

class org.apache.kafka.common.serialization.IntegerSerializer
class org.apache.kafka.common.serialization.IntegerDeserializer
2020-12-15 13:38:49.349  INFO 4612 --- [o65311053-0-C-1] o.s.k.l.KafkaMessageListenerContainer    : so65311053: partitions assigned: [so65311053-0]
foo
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
0

The error occur because of KafkaBinderEnvironmentPostProcessor

I solved it by creating my own EnvironmentPostProcessor

Dasma
  • 1,023
  • 13
  • 34