赞
踩
使用Kafka做日志收集。
需要收集的信息:
1、用户ID(user_id)
2、时间(act_time)
3、操作(action,可以是:点击:click,收藏:job_collect,投简历:cv_send,上传简历:cv_upload)
4、对方企业编码(job_code)
1、HTML可以理解为拉勾的职位浏览页面
2、Nginx用于收集用户的点击数据流,记录日志access.log
3、将Nginx收集的日志数据发送到Kafka主题:tp_individual
架构:
HTML+Nginx+ngx_kafka_module+Kafka
提示:
学员需要自己下载nginx,配置nginx的ngx_kafka_module,自定义一个html页面,能做到点击连接就收集用户动作数据即可。
操作步骤如下:
1.安装git
yum install -y git
2.切换到/opt/lagou/servers目录,然后将kafka的c客户端源码clone到本地
cd /opt/lagou/servers
git clone https://github.com/edenhill/librdkafka
如果无法访问github的话,就使用下面的地址
git clone https://gitee.com/tkxiong/librdkafka.git
3.进入到librdkafka,然后进行编译
cd librdkafka
yum install -y gcc gcc-c++ pcre-devel zlib-devel
./configure
make && make install
4.安装nginx整合kafka的插件,进入到/usr/local/src,clone nginx整合kafka的源码
cd /usr/local/src
git clone https://github.com/brg-liuwei/ngx_kafka_module
如果无法访问github的话,就使用下面的地址:
git clone https://gitee.com/chengfangang/ngx_kafka_module.git
5.进入到nginx的源码包目录下 (编译nginx,然后将将插件同时编译)
cd /opt/lagou/servers/nginx-1.17.8
./configure --add-module=/usr/local/src/ngx_kafka_module/
make
make install
6.修改nginx的配置文件
vi /opt/lagou/servers/nginx-1.17.8/conf/nginx.conf
http {
# some other configs
kafka;
kafka_broker_list centos7-1:9092; # host:port ...
server {
location = /kafka/log {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
kafka_topic topic_user_action;
}
}
7、加载so库
#开机加载/usr/local/lib下面的库
echo "/usr/local/lib" >> /etc/ld.so.conf
#手动加载
ldconfig
8、启动nginx(前提:先启动zookeeper和kafka)
cd /usr/local/nginx/sbin
./nginx -c /opt/lagou/servers/nginx-1.17.8/conf/nginx.conf
9、测试
#启动kafka消费者
kafka-console-consumer.sh --bootstrap-server centos7-1:9092 --topic topic_user_action --from-beginning
curl http://centos7-1:80/kafka/log -d "message send to kafka topic"
消费者接受到消息
10、html网页
<!doctype html> <html> <head> <meta charSet="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, minimum-scale=1.0"/> <title>kafka测试</title> </head> <body> <button onclick="action('click')">点击</button> <button onclick="action('jobCollect')">收藏</button> <button onclick="action('cvSend')">投简历</button> <button onclick="action('cvUpload')">上传简历</button> <script type="text/javascript" src='https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js'></script> <script> function action(_action){ var info = {}; info.userId = '123456'; info.actTime = new Date(); info.action = _action; info.jobCode = "lagou"; $.ajax({ type: "post", url: "http://centos7-1:80/kafka/log", data: JSON.stringify(info), success: function(res){ } }); } </script> </body> </html>
网页效果预览
11、测试结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。