使用原始标头发布 null/tombstone 消息 [英] Publish null/tombstone message with raw headers
问题描述
我正在构建一个 Spring Cloud Stream Kafka 处理器应用程序,它将使用字符串键使用原始数据,有时使用来自 Kafka 主题的空负载.我想为另一个主题生成一个字符串键和空负载(在 Kafka 中称为墓碑).为了在消息上使用原始标头,我需要输出一个 byte[]
,但是如果我将 KafkaNull.INSTANCE
编码成一个 byte[]
code> 它将逐字输出对象哈希码的字符串.
I am building a Spring Cloud Stream Kafka processor app that will consume raw data with a String key and sometimes a null payload from a Kafka topic. I want to produce to another topic a String key and the null payload (known as a tombstone within Kafka). In order to use raw headers on the message, I need to output a byte[]
, but if I encode KafkaNull.INSTANCE
into a byte[]
it will literally output a String of the object hashcode.
如果我尝试发送除 byte[]
以外的任何内容,我将无法使用原始标头.
If I try to send anything other than a byte[]
, I can't use raw headers.
这样做的正确方法是什么?如果有效负载为空,标头会在哪里?我在部署时设置了 producer.headerMode=embeddedHeaders
并且这似乎没有什么区别,仍然将哈希码作为有效负载.
What is the correct way to do this? Where would the headers even be if the payload is null? I set producer.headerMode=embeddedHeaders
on deploy and that didn't seem to make a difference, still have the hashcode as the payload.
推荐答案
你可以使用后者引入的 useNativeEncoding
属性(它与 raw
headerMode
因为它跳过标头)但允许您使用本机 Kafka 序列化程序.
You can use the latter introduced useNativeEncoding
property (which overlaps the raw
headerMode
in that it skips headers) but allows you use a native Kafka serializer.
参见 http://docs.spring.io/autorepo/docs/spring-cloud-stream-docs/Brooklyn.BUILD-SNAPSHOT/reference/htmlsingle/#_producer_properties 或 http://docs.spring.io/autorepo/docs/spring-cloud-stream-docs/Chelsea.BUILD-SNAPSHOT/reference/htmlsingle/#_producer_properties
这篇关于使用原始标头发布 null/tombstone 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!