简体   繁体   English

kafka消费者Java具有多个主题

[英]kafka consumer java with multiple topics

we have one consumer group and three topics, all three topics are of different schema . 我们有一个消费者组和三个主题,这三个主题都具有不同的架构。 created one consumer with a for loop passing each topic at a time and polling it processing and committing manually. 通过一个for循环创建了一个使用者,一次传递了每个主题,并对其进行轮询并手动进行处理和提交。 Method used is consumer created common and in for loop I am subscribing one topic at a time and processing data. 使用的方法是用户创建的通用方法,在for循环中,我一次订阅一个主题并处理数据。 I am seeing a random lag of consumer , although the topic has data my consumer fetches no records from topic and fetches sometimes. 我看到了一个随机的用户延迟,尽管该主题具有我的用户未从该主题获取任何记录的数据,并且有时会获取该数据。 When I work out with a single topic instead of looping through three topics it is working but unable to reproduce. 当我处理一个主题而不是遍历三个主题时,它可以工作但无法重现。 need help to debug the issue and reproduce the same, 需要帮助调试问题并重现问题,

Rather than looping three topics in a single method, you could create a skeleton thread like so that consumes from any topic. 与其在一个方法中循环三个主题,不如创建一个骨架线程,以便从任何主题中使用。 See examples here 在这里查看示例

I can't say if this will "fix" the problem, but trying to consume from topics with different schemas in one application is usually not a scalable pattern, but it's not really clear what you're trying to do. 我不能说这是否可以“解决”问题,但是尝试在一个应用程序中使用具有不同模式的主题通常不是可扩展的模式,但是还不清楚您要做什么。

class ConsumerThread extends Thread {

    KafkaConsumer consumer;
    AtomicBoolean stopped = new AtomicBoolean();

    ConsumerThread(Properties props, String subscribePattern) {
        this.consumer = new KafkaConsumer...
        this.consumer.subscribe(subscribePattern);
    } 

    @Override
    public void run() {
        while (!this.stopped.get()) {
            ... records = this.consumer.poll(100);
            for ( ... each record ... ) {
               // Process record
            } 
        }
    }

    public void stop() {
        this.stopped.set(true);
    }
}

Not meant to be production-grade 不意味着是生产级的

Then run three consumers independently. 然后独立运行三个消费者。

new ConsumerThread("t1").start();
new ConsumerThread("t2").start();
new ConsumerThread("t3").start();

Note : KafkaConsumer is not thread-safe. 注意KafkaConsumer不是线程安全的。

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

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