Kafka基于nginx_kafka_module 收集日志

使用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、测试结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值