美文网首页
搭建本地 Kafka 测试环境

搭建本地 Kafka 测试环境

作者: 焉知非鱼 | 来源:发表于2017-11-07 21:11 被阅读94次
# 启动 zookeeper
~/opt/zookeeper-3.4.10/bin/zkServer.sh start 

 # 创建 topic
 ~/opt/kafka_2.11-0.11.0.1/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic ohmyfish

# 创建生产者
~/opt/kafka_2.11-0.11.0.1/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic  ohmyfish 

安装 openresty

brew install openresty/brew/openresty

安装的路径应该在:

/usr/local/Cellar/openresty/1.13.6.1/nginx/sbin/

需要把这个路径添加到 PATH 路径中.

然后用 nginx -V 查看 nginx 是否正常:

nginx version: openresty/1.13.6.1
built by clang 9.0.0 (clang-900.0.39.2)
built with OpenSSL 1.0.2k  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/openresty/1.13.6.1/nginx --with-cc-opt='-O2 -I/usr/local/include -I/usr/local/opt/pcre/include -I/usr/local/opt/openresty-openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.07 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.11 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.18 --add-module=../redis2-nginx-module-0.14 --add-moduexport JAVA_HOME=$(/usr/libexec/java_home)
le=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.3 --with-ld-opt='-Wl,-rpath,/usr/local/Cellar/openresty/1.13.6.1/luajit/lib -L/usr/local/lib -L/usr/local/opt/pcre/lib -L/usr/local/opt/openresty-openssl/lib' --pid-path=/usr/local/var/run/openresty.pid --lock-path=/usr/local/var/run/openresty.lock --conf-path=/usr/local/etc/openresty/nginx.conf --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-pcre-jit --with-ipv6 --with-stream --with-stream_ssl_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_geoip_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-stream_ssl_module --with-http_ssl_module
  • 安装 luarocks
wget https://luarocks.org/releases/luarocks-2.4.3.tar.gz
tar zxpf luarocks-2.4.3.tar.gz
cd luarocks-2.4.3
./configure --prefix=/usr/local/luarocks-2.4.3 --with-lua=/usr/local/Cellar/openresty/1.13.6.1/luajit --lua-suffix="jit" --with-lua-include=/usr/local/Cellar/openresty/1.13.6.1/luajit/include/luajit-2.1
  • 使用 luarocks 安装 lua-iconv 模块
luarocks install lua-iconv
  • 打印 hello world
$ mkdir ~/openresty-test ~/openresty-test/logs/ ~/openresty-test/conf/
$
$ tree ~/openresty-test
/Users/yuansheng/openresty-test
├── conf
└── logs

在 conf 目录下创建一个名为 nginx.conf 的配置文件:

# use root;
worker_processes  1;        #nginx worker 数量
error_log logs/error.log;   #指定错误日志文件路径
events {
    worker_connections 1024;
}

http {
    server {
        #监听端口,若你的6699端口已经被占用,则需要修改
        listen 6699;
        location / {
            default_type text/html;

            content_by_lua_block {
                ngx.say("HelloWorld")
            }
        }
    }
}

如果出错就去 logs 目录看下 error.log 文件。如果是文件权限问题就在 nginx.conf 中加上

user root;

或修改文件/目录第所属组:

sudo chown -R www:www /data/app/ald_log/logs
sudo chown -R www:www proxy_temp
......

我们要把 nginx 接收到的日志转换为 json 格式, 修改我们的 nginx 配置文件:

user root;
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;

            content_by_lua_block {
                ngx.say("Young For Perl 6")
            }

        }

        location /d.html {
            default_type 'text/plain';
            content_by_lua_file html/get_to_json.lua;
        }
    }
}

开启 nginx :

# sudo nginx -p ~/openresty-test
sudo nginx -p `pwd`/ -c conf/nginx.conf
nginx.png html.png

然后 :

curl  http://localhost:8080

如果正常, 会打印 Young For Perl 6 这句话。同时

tail -f /data/app/ald_log/logs/json_log.log

会看到转换出来的 json 日志, 如果出错了, 把 iconv.so 文件拷贝到 openresty 相关目录:

cp /usr/local/luarocks-2.4.3/lib/lua/5.1/iconv.so /usr/local/Cellar/openresty/1.13.6.1/luajit/lib/lua/5.1/
cp /usr/local/luarocks-2.4.3/lib/lua/5.1/iconv.so ./conf
cp /usr/local/luarocks-2.4.3/lib/lua/5.1/iconv.so /usr/local/lib/lua/5.1/
nginx: [emerg] getgrnam("root") failed in /usr/local/nginx/conf/nginx.conf:1

加user root owner;

重启

nginx -c /usr/local/nginx/conf/nginx.conf

成功运行

当然你也可以将项目文件夹权限提升
http://127.0.0.1:8088/d.html?ak=1b25933fd0123c8bdb3c7b175accb41c&wsr=%7B%22scene%22%3A1089%2C%22path%22%3A%22pages%2Findex%2Findex%22%2C%22query%22%3A%7B%7D%7D&uu=15164276248163540744&at=15168851992628554707&st=1516885201936&tp=ald_pulldownrefresh&ev=event&nt=wifi&pm=iPhone%206s%3CiPhone8%2C1%3E&pr=2&ww=375&wh=603&lang=zh_CN&wv=6.6.1&lat=undefined&lng=undefined&spd=undefined&v=5.4.1&ct=1&sr=34a19c3fbbb51f46&rq_c=10

get_to_json.lua:

local uri_args = ngx.req.get_uri_args()
local dkjson = require "cjson"
local iconv = require("iconv")
local page_json = { }

function mylog(msg)
    local file, err = io.open("/data/app/ald_log/logs/json_log.log","aw+")
    if file == nil then
        ngx.say(err)
    else
            file:write (msg..'\n')
            file:flush();
            file:close();
    end
end

function get_client_ip()
    local headers=ngx.req.get_headers()
    local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
    return ip
end

function Split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
    if not nFindLastIndex then
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
    break
    end
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
        nFindStartIndex = nFindLastIndex + string.len(szSeparator)
    nSplitIndex = nSplitIndex + 1
    end
    return nSplitArray
    end


if uri_args then
     for key, val in pairs(uri_args) do
            if key == 'ag' then
                for k, v in pairs(dkjson.decode(val)) do
                    page_json["ag_"..string.lower(k)] = v
                end
            end
            if  key == 'wsr' then
                if val ~= 'undefined' then
                    for k, v in pairs(dkjson.decode(val)) do
                        if k == 'query' then
                            if type(v) == 'table' then
                                for qk, qv in pairs(v) do
                                    page_json["wsr_query_"..string.lower(qk)] = qv
                                end
                            else
                                page_json["wsr_query_"] = v 
                                
                            end

                        end
                        page_json[string.lower(k)] = v
                    end
                    page_json[string.lower(key)] = dkjson.decode(val)
                end
        
            end
            if  key == 'ct' then
                if pcall(dkjson.decode,val) then
                    if  type(dkjson.decode(val)) == "table" then --处理字典
                            for k, v in pairs(dkjson.decode(val)) do
                                page_json["ct_"..k] = v
                            end
                        page_json[string.lower(key)] = val --保留原ct的json
                    else
                            page_json[string.lower(key)] = val --处理数值类型
                    end
                   else
                    page_json[string.lower(key)] = val -- 处理字符串类型
                   end
            else
                    page_json[string.lower(key)] = val
            end

                        
    end
end
function getUserAgentByKey(key)
    local h = ngx.req.get_headers()
    local lists = Split(h['user-agent'], " ")

    for k,v in pairs(lists) do
        if string.find(v,key) then 
            return string.lower(string.gsub(v, key.."/", ""))
        end
    end
    return false
end

page_json['server_time'] = ngx.now()

-- local t = io.popen('/data/app/ald_log/www/ip/main '..get_client_ip())
-- local ail = t:read("*all")
-- if ail ~= nil then
--  local list = Split(ail, "\t")
--  page_json['country'] = list[1]
--  page_json['province'] = list[2]
--  page_json['city'] = list[3]
--  if page_json['city'] == nil then
--     page_json['city'] = list[2]
        
--  end
-- else
    
--  page_json['country'] = '' 
--  page_json['province'] = ''
--  page_json['city'] = ''
-- end

if page_json['nt'] == nil or page_json['nt'] == 'undefined' then
page_json['nt'] = getUserAgentByKey('NetType')

end
page_json['client_ip'] = get_client_ip()
page_json['lua_wechat_version'] = getUserAgentByKey('MicroMessenger')
mylog(dkjson.encode(page_json))

相关文章

网友评论

      本文标题:搭建本地 Kafka 测试环境

      本文链接:https://www.haomeiwen.com/subject/wmlsmxtx.html