spring-cloud-streamspring-cloud-stream-binder-kafka

Is there a reason why the existing converters for `spring-kafka` and `spring-cloud-stream-binder-kafka` are different?


In KafkaProperties provided by spring-kafka, the de/serializer of producer and consumer is String{Des/S}serializer.

However, when using spring-cloud-stream-binder-kafka, it is changed to ByteArray{Des/S}erializer by KafkaBinderEnvironmentPostProcessor. Is there a reason to take this as default? Or is there a reason to have different settings?


Solution

  • The byte array (de)serializers are more efficient when converting to/from JSON (which is normally the case with Spring Cloud Stream); it avoids an unnecessary conversion from byte[] to String (and vice-versa) before/after JSON conversion. Jackson can convert directly to/from byte[] which is the native Kafka value type.

    Boot chose String because it provides a much simpler "getting started" experience.

    Typical applications (other than SCSt) will use more sophisticated implementations (JSON, Avro, etc.).