简体   繁体   English

Spring Kafka Consumer - 从复杂对象中获取主题

[英]Spring Kafka Consumer - Getting topics from a complex object

I am working on a spring boot kafka consumer application.我正在开发 Spring Boot kafka 消费者应用程序。 It will have different consumers working on different topics.它将有不同的消费者处理不同的主题。 All the information for the consumers will come from the application.yml file.消费者的所有信息都来自 application.yml 文件。

 application: kafka: consumer-config: - name: consumer-a topics: topic1,topic2 ...... - name: consumer-b topics: topic3,topic4 .....

I am not able to set the list of topics from the application properties on to the KafkaListener.我无法将应用程序属性中的主题列表设置到 KafkaListener。

I tried the following:我尝试了以下方法:

 @KafkaListener(topics = "#{'${application.kafka.consumer-config[0].topics}'.split(',')}",containerFactory = "kafkaListenerContainerFactory") @KafkaListener(topics = "#{'${application.kafka.consumer-config.?[name == 'consumer-a'].topics}'.split(',')}", containerFactory = "kafkaListenerContainerFactory")

In both the cases I am getting the following error:在这两种情况下,我都收到以下错误:

java.lang.IllegalArgumentException: Could not resolve placeholder java.lang.IllegalArgumentException:无法解析占位符

What is the best way to get the topics from application properties and set it on KafkaListener topics?从应用程序属性获取主题并将其设置在 KafkaListener 主题上的最佳方法是什么?

What version are you using?你用的是什么版本? I just tested it and it works fine...我刚刚测试了它,它工作正常......

@SpringBootApplication
public class So63583349Application {

    public static void main(String[] args) {
        SpringApplication.run(So63583349Application.class, args);
    }

    @KafkaListener(topics = "#{'${application.kafka.consumer-config[0].topics}'.split(',')}", id = "so63583349")
    public void listen(String in) {
        System.out.println(in);
    }

}

2020-08-25 13:02:28.384 WARN 66237 --- [o63583349-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-so63583349-1, groupId=so63583349] Error while fetching metadata with correlation id 41 : {topic1=UNKNOWN_TOPIC_OR_PARTITION, topic2=UNKNOWN_TOPIC_OR_PARTITION} 2020-08-25 13:02:28.384 WARN 66237 --- [o63583349-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-so63583349-1, Error group3Idso=3493]获取关联 ID 为 41 的元数据:{topic1=UNKNOWN_TOPIC_OR_PARTITION, topic2=UNKNOWN_TOPIC_OR_PARTITION}

For the second one, you can't use SpEL selection within the property placeholder.对于第二个,您不能在属性占位符中使用 SpEL 选择。 Here is one solution for that situation:这是针对这种情况的一种解决方案:

@SpringBootApplication
public class So63583349Application {

    public static void main(String[] args) {
        SpringApplication.run(So63583349Application.class, args);
    }

    @KafkaListener(topics = "#{@props.consumerConfig.?[name == 'consumer-a'].get(0).topics.split(',')}",
            id = "so63583349")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    Props props() {
        return new Props();
    }

}

@ConfigurationProperties(value = "application.kafka")
class Props {

    List<Properties> consumerConfig;

    public List<Properties> getConsumerConfig() {
        return this.consumerConfig;
    }

    public void setConsumerConfig(List<Properties> consumerConfig) {
        this.consumerConfig = consumerConfig;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM