librdkafa最简单流程,简单易懂
librakafka1.5.0下载地址
https://download.youkuaiyun.com/download/landihao/12816690
C++,为了能看清流程,简单易懂,没有去封装,封装成类并不利于初步学习,消费者部分,写的匆忙,以后会来补充
#include “librdkafka/include/librdkafka/rdkafkacpp.h”
#include
#include
using namespace std;
/*
ExampleRebalanceCb ex_rebalance_cb;
m_conf->set(“rebalance_cb”, &ex_rebalance_cb, errstr);
m_conf->set(“enable.partition.eof”, “true”, errstr);
m_conf->set(“metadata.broker.list”, m_sBrokenList, errstr) //设置服务列表
m_conf->set(“auto.commit.interval.ms”, g_conf.sCommitTimeVal, errstr)
m_conf->set(“group.id”, m_sGroupID, errstr)
MyConsumeCb ex_consume_cb;
m_conf->set(“consume_cb”, &ex_consume_cb, errstr);
ExampleEventCb ex_event_cb;
m_conf->set(“event_cb”, &ex_event_cb, errstr);
m_tconf->set(“auto.offset.reset”, “latest”, errstr);
m_conf->set(“default_topic_conf”, m_tconf, errstr);
RdKafka::KafkaConsumer *consumer = RdKafka::KafkaConsumer::create(m_conf, errstr);
std::vectorstd::string topicsVec;
topicsVec.clear();
topicsVec.push_back(m_sTopicName);
RdKafka::ErrorCode err = consumer->subscribe(topicsVec);
while (m_runFlag)
{
RdKafka::Message msg = consumer->consume(1000);//1000是超时时间单位毫秒
msg_consume(msg, NULL);
delete msg;
}
consumer->close();
delete consumer;/
#include <stdio.h>
void msg_consume(RdKafka::Message *message, void *opaque)
{
switch (message->err())
{
case RdKafka::ERR__TIMED_OUT:
std::cerr << “RdKafka::ERR__TIMED_OUT” << std::endl;
break;
case RdKafka::ERR_NO_ERROR:
printf("%.*s\n",
static_cast<int>(message->len()), static_cast<const char *>(message->payload()));
//intlast_offset_ = message->offset();
break;
default:
/* Errors */
std::cerr << "Consume failed: " << message->errstr() << std::endl;
}
}
int main()
{
RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
RdKafka::Conf *tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
string errstr;
string brokers_ = “0”;
string strfetch_num = “10240000”;
string groupid_ = “01234”;//不是很重要,几乎没用,kafka可以设置
conf->set(“bootstrap.servers”, brokers_, errstr);
conf->set(“group.id”, groupid_, errstr);
conf->set(“max.partition.fetch.bytes”, strfetch_num, errstr);
tconf->set(“auto.offset.reset”, “smallest”, errstr);
RdKafka::Consumer *kafka_consumer_ = RdKafka::Consumer::create(conf, errstr);
string topic = “sun”;
int64_t offset_ = RdKafka::Topic::OFFSET_BEGINNING;
int32_t partition_ = 0;
RdKafka::Topic *topic_ = RdKafka::Topic::create(kafka_consumer_, topic, tconf, errstr);
RdKafka::ErrorCode resp = kafka_consumer_->start(topic_, partition_, offset_);
while (true)
{
RdKafka::Message *msg = kafka_consumer_->consume(topic_, partition_, 1000); //超时时间
msg_consume(msg, NULL);
kafka_consumer_->poll(0);
}
kafka_consumer_->stop(topic_, partition_);
RdKafka::wait_destroyed(5000);
}