[英]How to add headers to a message before posting to Kafka Topic using Spring Cloud Stream
I just realised that the message once received on Kafka Topic looses the headers.我刚刚意识到在 Kafka Topic 上收到的消息会丢失标题。 Is there a way to add headers to the message before posting it to Kafka Topic and then read it at the consumer?有没有办法在将消息发布到 Kafka Topic 之前向消息添加标头,然后在消费者处读取它? I am using Java 11, Spring Cloud Hoxton.SR6, Spring 2.2.4, kafka_2.13-2.6.0.我正在使用 Java 11、Spring Cloud Hoxton.SR6、Spring 2.2.4、kafka_2.13-2.6.0。 Thanks in advance!提前致谢!
I think posted the question a little too early.我认为发布这个问题有点太早了。 Its pretty simple.它很简单。 Get the request headers from HttpServletRequest or HttpRequest(spring) and then while writing to the MessageChannel, call the CopyHeaders method while passing the headers.从 HttpServletRequest 或 HttpRequest(spring) 获取请求标头,然后在写入 MessageChannel 时,在传递标头时调用 CopyHeaders 方法。 You can also skip the ones which are not needed.您也可以跳过不需要的那些。 At the consumer, instead of a Custom Object, get the Message object which has all the headers在消费者处,而不是自定义对象,获取包含所有标头的 Message 对象
Producer MessageBuilder.withPayload(message).copyHeaders(headersMap).build();
生产者MessageBuilder.withPayload(message).copyHeaders(headersMap).build();
Consumer processMessage(Message<?> message)
消费者processMessage(Message<?> message)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.