架构组成:
HTML+Nginx+ngx_kafka_module+Kafka
ngx_kafka_module网址:
https://github.com/brg-liuwei/ngx_kafka_module
由于使用ngx_kafka_module,只能接收POST请求,同时一般Web服务器不会和数据收集的Nginx在同一个域名,
会涉及到使用ajax发送请求的跨域问题,可以在nginx中配置跨域来解决。
单节点搭建 参考地址:
单机搭建 Kafka2.12安装与配置/生产与消费
集群搭建 参考地址:
高吞吐消息中间件Kafka集群环境搭建(3台kafka,3台zookeeper)
yum install wget git -y
yum install gcc-c++ -y
git clone https://github.com/edenhill/librdkafka //下载ngx_kafka_module依赖的包
cd librdkafka
./configure
make
make install
wget http://nginx.org/download/nginx-1.17.8.tar.gz //下载
tar -zxf nginx-1.17.8.tar.gz //解压
cd nginx-1.17.8
yum install gcc zlib zlib-devel openssl openssl-devel pcre pcre-devel -y //安装nginx依赖
cd ~ //退回到家目录
git clone https://github.com/brg-liuwei/ngx_kafka_module.git //下载ngx_kafka_module
cd nginx-1.17.8
./configure --add-module=/root/ngx_kafka_module
make && make install
配置 nginx:nginx.conf
nginx.conf内容
具体参数:
[root@kafka ~]# cat /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
kafka;
kafka_broker_list 192.168.80.30:9092;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
# 添加location
location = /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';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
kafka_topic tp_dabing_log;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
问题1:执行curl命令,返回404错误
解决:
1.检查curl指定的链接是否错误。
2.检查nginx.conf文件是否配置正确(修改的nginx.conf文件是安装目录下的,而不是源码目录下的)。
问题2:html页面埋点跨域问题
解决:
在nginx.conf中编辑:add_header 'Access-Control-Allow-Origin' '*';
echo "/usr/local/lib" >> /etc/ld.so.conf //操作系统去加载一开始安装的依赖包
ldconfig //加载
zkServer.sh start
kafka-server-start.sh /opt/kafka_2.12-1.0.2/config/server.properties
/usr/local/nginx/sbin/nginx
curl localhost/log -d "dabing message send to kafka topic" -v
在kafka消费者端能够消费到信息就表示搭建成功
演示效果
下载网盘地址 提取码:aeo3