需要收集的信息:
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、html的截图+搭建的过程+结果截图以文档或视频演示形式提供。
2、作业实现过程的代码。
(注意:1、需要把搭建过程用文档写清楚,2、运行效果:在html上点击相应的连接或按钮,应该在Kafka中看到有
消息流进来,效果最好以视频形式演示。)
安装librdkafka
yum install wget git -y
yum install gcc-c++ -y
git clone https://github.com/edenhill/librdkafka
cd librdkafka
./configure
make
sudo make install
下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -zxf nginx-1.18.0.tar.gz
下载ngx_kafka_module,并编译
git clone https://github.com/brg-liuwei/ngx_kafka_module
进入到nginx的源码包目录下 (编译nginx,然后将插件同时编译)
cd /opt/nginx-1.18.0
./configure --add-module=/root/ngx_kafka_module/
make && make install
修改nginx.conf
因为跨域问题所以添加了
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
允许跨域
启动nginx,启动kafka,创建消费者
/usr/local/nginx/sbin/nginx
kafka-server-start.sh config/server.properties
kafka-topics.sh --zookeeper 192.168.72.134:2181/myKafka --describe --topic tp_log
测试
curl 192.168.72.134/kafka/log -d "hello ngx_kafka_module" -v
创建html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>kafka_log</title>
</head>
<script src="./jquery-3.4.1.min.js"></script>
<body>
<input id="user_id" type="text" value="user" hidden="hidden">
<input id="job_code" type="text" value="code" hidden="hidden">
<button id="click" class="action" act_type="select">查询</button>
<button id="collect" class="action" act_type="insert">插入</button>
<button id="send" class="action" act_type="update">修改</button>
<button id="upload" class="action" act_type="delete">删除</button>
<label id="label"></label>
</body>
<style>
.action {height: 30px;font-family: 微软雅黑;font-size: 14px;color: aliceblue;border: none;border-radius: 4px;}
#click {background-color: cadetblue;}
#collect {background-color: cornflowerblue;}
#send {background-color: coral;}
#upload {background-color: indianred;}
</style>
<script>
$(document).ready(function () {
$(".action").click(function () {
var user_id = $("#user_id").val()+formatNewDate();
var act_time = new Date();
var action = this.getAttribute("act_type")
var job_code = $("#job_code").val()+formatNewDate();
var log = "user_id:"+user_id + "\t" +"act_time:"+act_time + "\t" +"action:"+ action + "\t" +"job_code:"+job_code;
$.ajax({
url: "http://192.168.72.134:80/kafka/log",
type: "POST",
dataType: "json",
data: log
})
});
})
function add0(m){return m<10?'0'+m:m }
function formatNewDate(){
//shijianchuo是整数,否则要parseInt转换
var time = new Date();
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'_'+add0(d)+'_'+add0(h)+'_'+add0(mm)+'_'+add0(s);
}
</script>
</html>