简体   繁体   English

如何在使用 Spring Cloud Stream 发布到 Kafka 主题之前向消息添加标头

[英]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.

相关问题 Spring Cloud Stream确定主题Kafka消息来自 - Spring Cloud Stream determine topic Kafka message came from 如何在 Spring Cloud Stream Kafka 绑定中编写订阅主题的方法? - How to write a method subscribing to a topic in Spring Cloud Stream Kafka binding? 使用spring cloud stream kafka读取消息的编程方式 - Programmatic way to read message using spring cloud stream kafka 如何根据条件向2个kafka主题发布消息 - spring cloud stream - How to publish message to 2 kafka topics based on condition - spring cloud stream 如何使用java(spring)将文本对象消息生成到kafka主题中? - How to produce a json object message into kafka topic using java(spring)? 春季云流的DLQ分区Kafka主题 - Partitioned Kafka Topic for DLQ for spring-cloud-stream 如何在 Spring Cloud Stream 中配置函数的绑定以将其输入绑定到 Web 端点并将其输出绑定到 Kafka 主题 - How to configure the bindings of a function in Spring Cloud Stream to bind its input to a web endpoint and its output to a Kafka topic 如何使用spring cloud stream实现自定义kafka分区 - How to implement custom kafka Partition using spring cloud stream 如何使用 Spring Cloud Kafka Stream 3.1 创建生产者 - How can create a producer using Spring Cloud Kafka Stream 3.1 默认情况下如何使用Kafka Spring Cloud Stream并使用汇合API生成的Kafka消息? - How to consume from Kafka Spring Cloud Stream by default and also consume a Kafka message generated by the confluent API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM