1、下載並安裝Win32OpenSSL-1_0_2s
2、從github上下載代碼:
https://github.com/edenhill/librdkafka/tree/v0.11.6-RC4,解壓並用vs2017打開win32/librdkafka.vcxproj工程文件
3、選中librdkafka工程,右鍵屬性-Platform Toolset,改爲Visual Stdio 2015(v140)
4、找到regexp.c文件,在開始部分添加代碼:
#if _MSC_VER>=1900
#include "stdio.h"
_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);
#ifdef __cplusplus
extern "C"
#endif
FILE* __cdecl __iob_func(unsigned i) {
return __acrt_iob_func(i);
}
#endif /* _MSC_VER>=1900 */
5、編譯代碼
6、選中librdkafkacpp工程後,照步驟4修改平臺工具集爲Visual Stdio 2015(v140)
7、編譯librdkafkacpp代碼,此時rdfkafka已經算編譯成功了。
8、將rdkafka.h和rdkafkacpp.h移動到新建的inc目錄,將librdkafka.lib,librdkafkacpp.lib移動到lib文件夾下,爲下面的c++ kafka應用作準備。
9、新建rdkafka的producer工程,寫producer相關代碼:
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <csignal>
#include <cstring>
#include <getopt.h>
#include "rdkafkacpp.h"
static bool run = true;
static void sigterm(int sig) {
run = false;
}
class ExampleDeliveryReportCb : public RdKafka::DeliveryReportCb {
public:
void dr_cb(RdKafka::Message &message) {
std::cout << "Message delivery for (" << message.len() << " bytes): " <<
message.errstr() << std::endl;
if (message.key())
std::cout << "Key: " << *(message.key()) << ";" << std::endl;
}
};
class ExampleEventCb : public RdKafka::EventCb {
public:
void event_cb(RdKafka::Event &event) {
switch (event.type())
{
case RdKafka::Event::EVENT_ERROR:
std::cerr << "ERROR (" << RdKafka::err2str(event.err()) << "): " <<
event.str() << std::endl;
if (event.err() == RdKafka::ERR__ALL_BROKERS_DOWN)
run = false;
break;
case RdKafka::Event::EVENT_STATS:
std::cerr << "\"STATS\": " << event.str() << std::endl;
break;
case RdKafka::Event::EVENT_LOG:
fprintf(stderr, "LOG-%i-%s: %s\n",
event.severity(), event.fac().c_str(), event.str().c_str());
break;
default:
std::cerr << "EVENT " << event.type() <<
" (" << RdKafka::err2str(event.err()) << "): " <<
event.str() << std::endl;
break;
}
}
};
int main()
{
std::string brokers = "localhost";
std::string errstr;
std::string topic_str = "test";
int32_t partition = RdKafka::Topic::PARTITION_UA;
RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
RdKafka::Conf *tconf = RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC);
conf->set("bootstrap.servers", brokers, errstr);
ExampleEventCb ex_event_cb;
conf->set("event_cb", &ex_event_cb, errstr);
signal(SIGINT, sigterm);
signal(SIGTERM, sigterm);
ExampleDeliveryReportCb ex_dr_cb;
conf->set("dr_cb", &ex_dr_cb, errstr);
RdKafka::Producer *producer = RdKafka::Producer::create(conf, errstr);
if (!producer) {
std::cerr << "Failed to create producer: " << errstr << std::endl;
exit(1);
}
std::cout << "% Created producer " << producer->name() << std::endl;
RdKafka::Topic *topic = RdKafka::Topic::create(producer, topic_str,
tconf, errstr);
if (!topic) {
std::cerr << "Failed to create topic: " << errstr << std::endl;
exit(1);
}
for (std::string line; run && std::getline(std::cin, line);) {
if (line.empty()) {
producer->poll(0);
continue;
}
RdKafka::ErrorCode resp =
producer->produce(topic, partition,
RdKafka::Producer::RK_MSG_COPY /* Copy payload */,
const_cast<char *>(line.c_str()), line.size(),
NULL, NULL);
if (resp != RdKafka::ERR_NO_ERROR)
std::cerr << "% Produce failed: " <<
RdKafka::err2str(resp) << std::endl;
else
std::cerr << "% Produced message (" << line.size() << " bytes)" <<
std::endl;
producer->poll(0);
}
run = true;
// 退出前處理完輸出隊列中的消息
while (run && producer->outq_len() > 0) {
std::cerr << "Waiting for " << producer->outq_len() << std::endl;
producer->poll(1000);
}
delete conf;
delete tconf;
delete topic;
delete producer;
RdKafka::wait_destroyed(5000);
return 0;
}
完成後將步驟6的inc和lib目錄添加進工程配置裏面,然後編譯代碼。(如果報找不到librdkafka.dll或者librdkafkacpp.dll,就將對應的dll拷貝到Debug\Release的生成文件目錄中。)
8、下載jdk,配置java環境變量JAVA_HOME及PATH
9、下載zookeeper-3.4.14.tar,解壓後複製zoo_sample.cfg重命名成zoo.cfg,修改編輯zoo.cfg,修改dataDir爲【dataDir=/data】
10、添加環境變量
ZOOKEEPER_HOME D:\Program Files\zookeeper-3.5.2-alpha
Path 在現有的值後面添加 ;%ZOOKEEPER_HOME%\bin;
11、運行zookeeper:zkserver
12、下載kafka_2.12-2.3.0,解壓後打開目錄D:\kafka_2.12-0.11.0.0\config下server.properties文件,把log.dirs修改爲【log.dirs=D:\kafka-logs】
12、運行kafka集羣環境:.\bin\windows\kafka-server-start.bat .\config\server.properties
13、運行kafka的producer:.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test
14、運行kafka的customer:.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
15、在producer的cmd裏面寫消息內容,可以看到customer的cmd接收到了消息。
16、啓動步驟9的rdkafka的producer程序(程序中端口設置爲9092,ip設置爲licalhost,topic設置爲test),在啓動窗口發送消息,可以看到customer的cmd裏面接收到了消息。
windows下kafka安裝及c++實現kafka的源碼地址:
https://download.csdn.net/download/qq_23350817/11715765