【问题标题】:Removing Embedded Header in Dynamically Generated Destinations删除动态生成的目标中的嵌入标头
【发布时间】:2018-07-10 14:53:57
【问题描述】:

我想在使用动态生成的目的地时禁用消息中的嵌入标头,类似于来自 here 的示例(例如输出主题 = dyntopic1,dyntopic2,...)。

我已经设置了如下属性,但我仍然得到标题,如果我错过了什么,有什么建议吗?

spring.cloud.stream.bindings.output.group=test-ogroup
spring.cloud.stream.bindings.output.binder=kafka
spring.cloud.stream.bindings.output.producer.headerMode=raw
spring.cloud.stream.bindings.output.content-type=text/plain

Kafka = v0.10

spring-cloud-dependencies.version = Edgware.M1

【问题讨论】:

    标签: spring-cloud-stream spring-cloud-dataflow


    【解决方案1】:

    Spring Cloud Stream 1.3 或更低版本无法为动态目的地设置生产者属性。

    如果提前知道属性,可以在属性中设置...

    spring.cloud.stream.bindings.dyntopic1.producer.headerMode=raw
    

    该功能已被 added to master 提供,并将在 2.0 版本中提供。

    编辑

    只要您不介意使用反射来重置标志,就可以使用 Edgware 来完成。您必须替换通道解析器 bean。

    我使用 Edgware.SR1 对此进行了测试 - 你真的不应该再使用 M1,这是一个预发布里程碑。

    我不能保证这将适用于较新的版本,因为它会破坏框架内部结构。

    @SpringBootApplication
    @EnableBinding(Sink.class)
    public class So48543143Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So48543143Application.class, args);
        }
    
        @Bean
        public ApplicationRunner runner(MessageChannel routeChannel) {
            return args -> {
                routeChannel.send(new GenericMessage<>("foo"));
            };
        }
    
        @ServiceActivator(inputChannel = "routeChannel")
        @Bean
        public AbstractMappingMessageRouter router(MyBinderAwareChannelResolver resolver) {
            ExpressionEvaluatingRouter router = new ExpressionEvaluatingRouter(new LiteralExpression("foo"));
            router.setDefaultOutputChannelName("default");
            router.setChannelResolver(resolver);
            return router;
        }
    
        @Bean
        public MyBinderAwareChannelResolver binderAwareChannelResolver(BindingService bindingService,
                AbstractBindingTargetFactory<? extends MessageChannel> bindingTargetFactory,
                DynamicDestinationsBindable dynamicDestinationsBindable) {
            return new MyBinderAwareChannelResolver(bindingService, bindingTargetFactory, dynamicDestinationsBindable);
        }
    
        public static class MyBinderAwareChannelResolver extends BinderAwareChannelResolver {
    
            public MyBinderAwareChannelResolver(BindingService bindingService,
                    AbstractBindingTargetFactory<? extends MessageChannel> bindingTargetFactory,
                    DynamicDestinationsBindable dynamicDestinationsBindable) {
                super(bindingService, bindingTargetFactory, dynamicDestinationsBindable);
            }
    
            @Override
            public MessageChannel resolveDestination(String channelName) {
                MessageChannel channel = super.resolveDestination(channelName);
                DirectFieldAccessor dfa = new DirectFieldAccessor(channel);
                AbstractDispatcher dispatcher = (AbstractDispatcher) dfa.getPropertyValue("dispatcher");
                dfa = new DirectFieldAccessor(dispatcher);
                @SuppressWarnings("unchecked")
                Set<MessageHandler> handlers = (Set<MessageHandler>) dfa.getPropertyValue("handlers");
                // there should be exactly one handler
                MessageHandler handler = handlers.iterator().next();
                dfa = new DirectFieldAccessor(handler);
                dfa.setPropertyValue("embedHeaders", false);
                return channel;
            }
    
        }
    
    }
    

    【讨论】:

    • 2.0 版本能否与 Kafka v0.10 一起使用?另外,无论如何我可以通过代码本身删除它吗?因为继续为我拥有的每个主题添加更多配置属性会很笨拙。
    • No 2.0 不能与 0.10 一起使用(这很旧,我建议您尽快迁移到更新版本的 Kafka。我编辑了我的答案,只要你不介意使用反射。
    • 感谢您的帮助,将使用它,因为我预计我的环境不会很快升级任何 Kafka。干杯!
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2016-04-23
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    相关资源
    最近更新 更多