nginx整合kafak后,可以将nginx中的数据,直接保存到kafka中
就是给Nginx安装一个kafka插件
操作步骤:
一:安装git
yum install -y git
但是此种安装git版本可能过低,会导致git clone操作使用,所以需要注意git的版本
二:下载kafka的c客户端源码
放在与nginx同一个父级目录下
git clone https://github.com/edenhill/librdkafka
三:进入到librdkafka,然后进行编译
yum install -y gcc gcc-c++ pcre-devel zlib-devel
./configure
make && make install
四:安装nginx整合kafka的插件
git clone https://github.com/brg-liuwei/ngx_kafka_module
五:编译Nginx,将插件一同进行编译
./configure --add-module=/usr/local/src/ngx_kafka_module/
make
make install
六:修改Nginx的配置nginx.conf
#user nobody;
worker_processes 1;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
kafka;
kafka_broker_list kafka-01:9092 kafka-02:9092 kafka-03:9092;
server {
listen 80;
server_name nks_name;
location = /kafka/track {
kafka_topic track;
}
location = /kafka/user {
kafka_topic user;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
七:启动zookeeper 和 kafka集群
/root/apps/zookeeper-3.4.9/bin/zkServer.sh start
/root/apps/kafka_2.11-0.10.2.1/bin/kafka-server-start.sh -daemon /root/apps/kafka_2.11-0.10.2.1/config/server.properties
八:启动nginx
/usr/local/nginx/sbin/nginx
如果启动时报错:
error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
表示找不到kafka.so.1的文件
解决方法:
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig --需要执行的命令LDCONFIG
九:重启Nginx服务
/usr/local/nginx/sbin/nginx -s reload
十:向nginx中写入数据
curl localhost/kafka/track -d "hello world"
localhost表示本机,nginx的80端口
/kafka/track 表示nginx中配置的server -> location中的路径 信息
十一:启动消费者
在某一个kafka服务器上,启动一个个消费者
/root/app/kafka_2.11-0.8.2.2/bin/kafka-console-consumer.sh --zookeeper zk-02:2181,zk-01:2181,zk-03:2181 --topic track --from-beginning
其中的track 对应 Location = kafka/track 中的track
十二:前端程序记录日志(通过nginx记录到kafka)
wx.request({ //用POST方式请求es可以只指定index和type,不用指定id url: "http://NginxLog-01/kafka/track", data: { time: new Date(), openid: openid, lat: lat, log: log }, method: "POST" })