Nginx Kafka Module環(huán)境搭建
參考官網(wǎng):https://github.com/brg-liuwei/ngx_kafka_module/blob/master/README.md
進入目錄 cd /usr/local/src/
1.上傳下載的安裝文件
2.進入libkafka目錄 cd librdkafka
安裝依賴 yum install -y gcc gcc-c++ pcre-devel zlib-level
編譯安裝 安裝完成的默認路徑/usr/local/include
./configure
make && make install
3.進入nginx目錄 cd nginx-1.8.1/ (編譯nginx洪橘,然后將將插件同時編譯)
./configure --add-module=/usr/local/src/ngx_kafka_module/
make
make install
4.修改配置文件崎淳,需要提前啟動zk并創(chuàng)建要配置的主題信息
5.開啟消費者接收消息
6.啟動nginx 進入nginx sbin目錄./nginx 蝇裤,
1)報錯掷漱,找不到kafka.so.1的文件 error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
2)加載so庫 echo "/usr/local/lib" >> /etc/ld.so.conf ldconfig
3)重新啟動成功訪問:
curl xxxxx/brokers/topics -d "hello ngx_kafka_module" -v
7.頁面埋點采集配置
8.采集服務器js代碼
(function () {
?? var params = {};
?? //Document對象數(shù)據(jù)
?? if(document) {
? ? ?? params.domain = document.domain || '';
? ? ?? params.url = document.URL || '';
? ? ?? params.title = document.title || '';
? ? ?? params.referrer = document.referrer || '';
?? } ?
?? //Window對象數(shù)據(jù)
?? if(window && window.screen) {
? ? ?? params.sh = window.screen.height || 0;
? ? ?? params.sw = window.screen.width || 0;
? ? ?? params.cd = window.screen.colorDepth || 0;
?? } ?
?? //navigator對象數(shù)據(jù)
?? if(navigator) {
? ? ?? params.lang = navigator.language || '';
?? } ?
?? //解析_maq配置
?? if(_maq) {
? ? ? for(var i in _maq) {
? ? ? ? ? ? var paramsName =? _maq[i][0];
? ? ? ? ? ? var paramsValue =? _maq[i][1];
? ? ? ? ? ? params[paramsName] = paramsValue;
? ? ? ? }
?? } ?
?? //拼接參數(shù)串
?? var args = '';
?? for(var i in params) {
? ? ?? if(args != '') {
? ? ? ? ?? args += '&';
? ? ?? } ?
? ? ?? args += i + '=' + encodeURIComponent(params[i]);
?? } ?
?
?? //通過Image對象請求后端腳本
?? var img = new Image(1, 1);
?? img.src = 'http://xxxxx/log.gif?' + args;
})();
?
html頁面的js腳本全封,訪問采集服務器的js
?
<script type="text/javascript">
?? var _maq = _maq || [];
?? $(function (){
? ? ?? $("button").click(function () {
? ? ? ? ?? var listenClick = $(this).attr("data-id");
? ? ? ? ?? var userName = $("#userId").val();
? ? ? ? ?? var date = getFormatDate();
? ? ? ? ?? _maq.push(['_setAccount', userName]);
? ? ? ? ?? _maq.push(['_setClick', listenClick]);
? ? ? ? ?? _maq.push(['_setClickTime', date]);
? ? ? ? ?? //解析_maq配置
? ? ? ? ?? var ma = document.createElement('script');
? ? ? ? ?? ma.type = 'text/javascript';
? ? ? ? ?? ma.async = true;
? ? ? ? ?? ma.src = 'http://xxxxx/ma/ma.js';
? ? ? ? ?? var s = document.getElementsByTagName('script')[0];
? ? ? ? ?? s.parentNode.insertBefore(ma, s);
? ? ?? })
?? })
?
?? function getFormatDate(){
? ? ?? var nowDate = new Date();
? ? ?? var year = nowDate.getFullYear();
? ? ?? var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
? ? ?? var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
? ? ?? var hour = nowDate.getHours()< 10 ? "0" + nowDate.getHours() : nowDate.getHours();
? ? ?? var minute = nowDate.getMinutes()< 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
? ? ?? var second = nowDate.getSeconds()< 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
? ? ?? return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
?? }
</script>
配置nginx.conf
配置kafka信息
?? kafka;
?? kafka_broker_list xxxxx:9092;
?
?? server {
? ? ? ? listen 80;
? ? ? ? server_name xxxxx;
? ? location = /mykafka/brokers/topics {
? ? kafka_topic nginx_topic;
?? }
在此接收js的log.gif請求马昙,模擬post發(fā)送到kafka
? ? location /log.gif {
? ? #偽裝成gif文件
? ? default_type image/gif;
? ? #本身關閉access_log,通過subrequest記錄log
? ? access_log off;
? ? ? ? access_by_lua "
? ? local uid = ngx.var.cookie___utrace
? ? if not uid then
? ? uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent)
? ? end
? ? ngx.header['Set-Cookie'] = {'__utrace=' .. uid .. '; path=/'}
? ? if ngx.var.arg_domain then
? ? ngx.location.capture('/i-log?' .. ngx.var.args .. '&utrace=' .. uid)
? ? ngx.location.capture('/mykafka/brokers/topics',{method=ngx.HTTP_POST,body=ngx.var.args})
? ? end
end
";
9.寫入日志成功刹悴,kafka接收數(shù)據(jù)成功接入如下