卡夫卡消费者接收开销?

时间:2018-02-16 13:46:03

标签: apache-kafka wireshark

我有一个kafka消费者每隔10秒进行一次民意调查。我正在使用wireshark来监控我的网络的活动。

我注意到即使我没有进行任何获取请求,代理和我的消费者之间仍然存在流量。我还注意到,它是周期性发送和接收的相同数据包(几乎相同,只是对有效负载的轻微更改)。

这是一种保持活跃的数据包吗?如何减少它们?

以下是这些数据包的屏幕截图: enter image description here

PS:我使用cppkafka作为lib和kafka broker 0.8.2.2

编辑:客户的代码

bool running = true;

int main(int argc, char* argv[]) {
    string brokers;
    string topic_name;
    string group_id;

    po::options_description options("Options");
    options.add_options()
        ("help,h",     "produce this help message")
        ("brokers,b",  po::value<string>(&brokers)->required(), 
                       "the kafka broker list")
        ("topic,t",    po::value<string>(&topic_name)->required(),
                       "the topic in which to write to")
        ("group-id,g", po::value<string>(&group_id)->required(),
                       "the consumer group id")
        ;

    po::variables_map vm;

    try {
        po::store(po::command_line_parser(argc, argv).options(options).run(), vm);
        po::notify(vm);
    }
    catch (exception& ex) {
        cout << "Error parsing options: " << ex.what() << endl;
        cout << endl;
        cout << options << endl;
        return 1;
    }

    // Stop processing on SIGINT
    signal(SIGINT, [](int) { running = false; });

    // Construct the configuration
    Configuration config = {
        { "metadata.broker.list", brokers },
        { "api.version.request", false },
        { "broker.version.fallback", "0.8.2.2" },   
        { "group.id", group_id },
        // Disable auto commit
        { "enable.auto.commit", false }
    };

    // Create the consumer
    Consumer consumer(config);

    // Subscribe to the topic
    TopicPartitionList topicList;
    cppkafka::TopicPartition topPar(topic_name,0);
    topPar.set_offset(0);
    topicList.push_back(topPar);
    cout << "Consuming messages from topic " << topic_name << endl;

    consumer.assign(topicList);

    // Now read lines and write them into kafka
    while (running) {
        // Try to consume a message
        Message msg = consumer.poll();
        if (msg) {
            // If we managed to get a message
            if (msg.get_error()) {
                // Ignore EOF notifications from rdkafka
                if (!msg.is_eof()) {
                    cout << "[+] Received error notification: " << msg.get_error() << endl;
                } else {
                    std::this_thread::sleep_for(std::chrono::milliseconds(10000));
                }
            } else {
                // Print the key (if any)
                if (msg.get_key()) {
                    cout << msg.get_key() << " -> ";
                }
                // Print the payload
                cout << msg.get_payload() << endl;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Cppkafka建立在librdkafka之上。 Librdkafka尝试为所有已分配的分区预取消息,因此当您调用poll()时,消息立即可用。

默认情况下,librdkafka非常积极(旨在获得最佳性能),因此您每秒钟会看到几个FetchRequests。

有关详细信息,请参阅librdkafka的常见问题解答:

答案 1 :(得分:0)

您可能会看到heartbeat messages以保持消费者群体的活力,您可以在此处找到有关他们的更多信息:https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol#AGuideToTheKafkaProtocol-GroupMembershipAPI

可以通过修改 heartbeat.interval.ms 来调整心跳间隔,检查librdkafka configuration