spring kafka 入站和出站映射头不同

发布于 2025-01-13 15:12:31 字数 531 浏览 3 评论 0原文

我们正在使用 spring-kafka,对于与我们通信但未设置 spring_json_header_types 标头的非 spring 应用程序,我们指定某些标头应通过将它们添加为 rawMappedHeaders 来映射为字符串。我们通过设置配置为所有活页夹执行此操作: spring.cloud.stream.kafka.binder.headerMapperBeanName 作为我们为其设置 rawMappedHeaders 的默认KafkaHeaderMapper。这适用于我们拥有的生产者和消费者绑定。

然而,当我们向某些 Spring 应用程序发送出站数据时,我们实际上不想遗漏这些 rawMappedHeader 字段的 spring_json_header_types 值,因为下游应用程序尚未适应这种非 Spring 应用程序通信。

有没有一种方法可以通过配置或代码将 headerMapperBeanName 仅应用于所有生产者或仅应用于所有消费者,以便我可以让它们以不同的方式映射出站和入站标头?或者有没有更好的方法来做到这一点,DefaultKafkaHeaderMapper 本身可以处理?

we are using spring-kafka, and for non-spring apps that communicate with us that don't set the spring_json_header_types header, we are specifying that certain headers should be mapped in as Strings by adding them as rawMappedHeaders. We do this for all of our binders by setting the config:
spring.cloud.stream.kafka.binder.headerMapperBeanName as the defaultKafkaHeaderMapper which we've set rawMappedHeaders for. This applies to both the producer and consumer bindings we have.

However, when we send data outbound to some of our spring applications, we actually don't want to leave out the spring_json_header_types values for those rawMappedHeader fields, since the downstream apps have not adjusted to this non-spring app communication yet.

Is there a way through the config or code that I can apply a headerMapperBeanName to only all of the producers or only to all of the consumers so that I can have them map headers differently for outbound vs inbound? Or is there a better way of doing this that the DefaultKafkaHeaderMapper itself can handle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2025-01-20 15:12:31

绑定器级别只有一个标头映射器。

创建一个自定义的 KafkaHeaderMapper 实现可能是最简单的,该实现委托给不同的 DefaultKafkaHeaderMapper 来进行入站和出站映射。

public interface KafkaHeaderMapper {

    /**
     * Map from the given {@link MessageHeaders} to the specified target headers.
     * @param headers the abstracted MessageHeaders.
     * @param target the native target headers.
     */
    void fromHeaders(MessageHeaders headers, Headers target);

    /**
     * Map from the given native headers to a map of headers for the eventual
     * {@link MessageHeaders}.
     * @param source the native headers.
     * @param target the target headers.
     */
    void toHeaders(Headers source, Map<String, Object> target);

}

There is only one header mapper at the binder level.

It is probably easiest to create a custom KafkaHeaderMapper implementation that delegates to a different DefaultKafkaHeaderMapper for inbound and outbound mappings.

public interface KafkaHeaderMapper {

    /**
     * Map from the given {@link MessageHeaders} to the specified target headers.
     * @param headers the abstracted MessageHeaders.
     * @param target the native target headers.
     */
    void fromHeaders(MessageHeaders headers, Headers target);

    /**
     * Map from the given native headers to a map of headers for the eventual
     * {@link MessageHeaders}.
     * @param source the native headers.
     * @param target the target headers.
     */
    void toHeaders(Headers source, Map<String, Object> target);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文