定义多个 kafkaComponent bean 时,Kafka Camel Spring Boot AutoConfiguration 不会被拾取

我目前正在使用 这个 camel-kafka-startermaven 依赖 在 Spring Boot 中自动配置我的 kafka camel 组件.

I'm currently using this camel-kafka-starter maven dependency to autoconfigure my kafka camel component in Spring Boot.

如果我添加说,像这样的设置 camel.component.kafka.configuration.linger-ms=20.Camel kafka 组件在路由中拾取它,我可以在日志输出中看到它的配置值.例如

If I add say, a setting like this camel.component.kafka.configuration.linger-ms=20. The camel kafka component picks it up in the route and I can see its config value in the log output. e.g.

@Component
public class route extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("file://target/inbox")
        .to("kafka:topic?brokers=localhost:9092");
    }
}

现在,如果我定义另一个 camelKafkaComponent Bean,它会丢失自动配置!我希望它能够保持 application.propeties 中定义的设置...

Now, if i define another camelKafkaComponent Bean, it loses the autoconfiguration! I'd expect it to maintain the settings defined in application.propeties...

@Configuration
public class MyConfiguration {
  @Bean
  public KafkaComponent myKafkaComponent() {
    return new KafkaComponent();  //missing the linger-ms=20 i set in application.properties!
  }

  @Component
  public class route extends RouteBuilder {
     @Override
     public void configure() throws Exception {
        from("file://target/inbox")
        .to("myKafkaComponent:topic?brokers=localhost:9092");
     }
  }
}

有没有办法通过一些设置来维护自动配置,例如

Is there a way to maintain the autoconfiguration by some setting e.g.

myKafkaComponent.useAutoConfig()

理想情况下,在我的 application.properties 中,我有大约 40 多个设置,我想将它们转移到我的用户定义的骆驼 kafkaComponent.是否有必要创建另一个 Config 类并映射骆驼文档中定义的所有这些值?(带有类似 String @Value 注释的东西)

Ideally in my application.properties I'd have ~40+ settings that I would like to just transfer over to my user defined camel kafkaComponent. Is it necessary to create another Config class and map all those values defined in the camel docs? (with something like String @Value annotations)

我在看 源代码 并且想知道是否有办法在我的 bean 定义中调用它.

edit: I was looking at the source code and was also wondering if there was a way to call it inside my bean definition.

推荐答案

你可以创建一个类型的bean

You can create a bean of type

ComponentCustomizer<KafkaComponent>

可用于自定义camel自动配置的kafka组件

which can be used to customize the kafka component auto configured by camel

相关文章