上一篇 redis 緩存冷啟動(dòng)分析及解決思路谬运, 主要講解緩存冷啟動(dòng)帶來(lái)的問(wèn)題及解決方案思路离福,本篇主要講解如何進(jìn)行請(qǐng)求上報(bào)kafka溅呢。
環(huán)境依賴(lài)
前面26澡屡、27猿挚、28講到的博文環(huán)境即可,上報(bào)kafka 挪蹭,只需在應(yīng)用層nginx上操作(192.168.0.16,192.168.0.17)
請(qǐng)求上報(bào)kafka 其實(shí)很簡(jiǎn)單亭饵,大致思路是:
- 下載lua-resty-kafka休偶,提供lua 操作kafka的方法類(lèi)庫(kù)
- lua 獲取nginx 請(qǐng)求參數(shù)梁厉,組裝上報(bào)對(duì)象
- 上報(bào)對(duì)象 encode cjson 編碼
- lua kakfa 上報(bào)即可
代碼實(shí)現(xiàn)
- 引入 lua-resty-kafka 類(lèi)庫(kù)
yum install -y unzip
cd /usr/local/servers/ && wget https://github.com/doujiang24/lua-resty-kafka/archive/master.zip
unzip master.zip
cp -rf lua-resty-kafka-master/lib/resty/kafka /usr/local/test/lualib/resty/
/usr/local/servers/nginx/sbin/nginx -s reload
- lua 獲取請(qǐng)求,組裝上報(bào)對(duì)象踏兜,encode對(duì)象并上報(bào)(注意:以下代碼只對(duì)流量上報(bào)代碼進(jìn)行注釋?zhuān)渌a參考 前面 28 “分發(fā)層 + 應(yīng)用層” 雙層nginx 架構(gòu) 之 應(yīng)用層實(shí)現(xiàn))
vim /usr/local/test/lua/test.lua
代碼如下:
// 引入 kafka 生產(chǎn)者 類(lèi)庫(kù)
local producer = require("resty.kafka.producer")
// 引入json 解析類(lèi)庫(kù)
local cjson = require("cjson")
// 構(gòu)造kafka 集群節(jié)點(diǎn) broker
local broker_list = {
{ host = "192.168.0.16", port = 9092},
{ host = "192.168.0.17", port = 9092},
{ host = "192.168.0.18", port = 9092}
}
// 定義上報(bào)對(duì)象
local log_obj = {}
// 自定義模塊名稱(chēng)
log_obj["request_module"] = "product_detail_info"
// 獲取請(qǐng)求頭信息
log_obj["headers"] = ngx.req.get_headers()
// 獲取請(qǐng)求uri 參數(shù)
log_obj["uri_args"] = ngx.req.get_uri_args()
// 獲取請(qǐng)求body
log_obj["body"] = ngx.req.read_body()
// 獲取請(qǐng)求的http協(xié)議版本
log_obj["http_version"] = ngx.req.http_version()
// 獲取請(qǐng)求方法
log_obj["method"] = ngx.req.get_method()
// 獲取未解析的請(qǐng)求頭字符串
log_obj["raw_reader"] = ngx.req.raw_header()
// 獲取解析的請(qǐng)求body體內(nèi)容字符串
log_obj["body_data"] = ngx.req.get_body_data()
// 上報(bào)對(duì)象json 字符串編碼
local message = cjson.encode(log_obj)
local uri_args = ngx.req.get_uri_args()
local product_id = uri_args["productId"]
local shop_id = uri_args["shopId"]
// 創(chuàng)建kafka producer 連接對(duì)象词顾,producer_type = "async" 異步
local async_producer = producer:new(broker_list, {producer_type = "async"})
// 請(qǐng)求上報(bào)kafka,kafka 生產(chǎn)者發(fā)送數(shù)據(jù)碱妆,async_prodecer:send(a肉盹,b,c)疹尾,a : 主題名稱(chēng)上忍,b:分區(qū)(保證相同id,全部到相同的kafka node 去纳本,并且順序一致)窍蓝,c:消息(上報(bào)數(shù)據(jù))
local ok, err = async_producer:send("access-log", product_id, message)
// 上報(bào)異常處理
if not ok then
ngx.log(ngx.ERR, "kafka send err:", err)
return
end
local cache_ngx = ngx.shared.test_cache
local product_cache_key = "product_info_"..product_id
local shop_cache_key = "shop_info_"..shop_id
local product_cache = cache_ngx:get(product_cache_key)
local shop_cache = cache_ngx:get(shop_cache_key)
if product_cache == "" or product_cache == nil then
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri("http://192.168.0.3:81",{
method = "GET",
path = "/getProductInfo?productId="..product_id
})
product_cache = resp.body
cache_ngx:set(product_cache_key, product_cache, 10 * 60)
end
if shop_cache == "" or shop_cache == nil then
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri("http://192.168.0.3:81",{
method = "GET",
path = "/getShopInfo?shopId="..shop_id
})
shop_cache = resp.body
cache_ngx:set(shop_cache_key, shop_cache, 10 * 60)
end
local product_cache_json = cjson.decode(product_cache)
local shop_cache_json = cjson.decode(shop_cache)
local context = {
productId = product_cache_json.id,
productName = product_cache_json.name,
productPrice = product_cache_json.price,
productPictureList = product_cache_json.pictureList,
productSecification = product_cache_json.secification,
productService = product_cache_json.service,
productColor = product_cache_json.color,
productSize = product_cache_json.size,
shopId = shop_cache_json.id,
shopName = shop_cache_json.name,
shopLevel = shop_cache_json.level,
shopRate = shop_cache_json.rate
}
local template = require("resty.template")
template.render("product.html", context)
- 配置nginx DNS resolver實(shí)例,避免 DNS 解析失敗
vim /usr/local/servers/nginx/conf/nginx.conf
在 http 部分添加以下內(nèi)容繁成,如下圖:
resolver 8.8.8.8
(注:以上操作 nginx 應(yīng)用服務(wù)器(192.168.0.16,192.168.0.17)都需要進(jìn)行)
- 配置 kafka advertised.host.name 參數(shù)(避免通過(guò)機(jī)器名無(wú)法找到對(duì)應(yīng)的機(jī)器)(所有kafka 節(jié)點(diǎn)都配置)
advertised.host.name = 本機(jī)ip
vim /usr/local/kafka/config/server.properties
- nginx 校驗(yàn) 及 重載
/usr/local/servers/nginx/sbin/nginx -t && /usr/local/servers/nginx/sbin/nginx -s reload
- 啟動(dòng)kafka(確保 zookeeper 已啟動(dòng))
cd /usr/local/kafka && nohup bin/kafka-server-start.sh config/server.properties &
- kafka 中創(chuàng)建 access-log 主題
cd cd /usr/local/kafka && bin/kafka-topics.sh --zookeeper my-cache1:2181,my-cache2:2181,my-cache3:2181 --topic access-log --replication-factor 1 --partitions 1 --create
- 打開(kāi)kafka consumer 查看數(shù)據(jù)
bin/kafka-console-consumer.sh --zookeeper my-cache1:2181,my-cache2:2181,my-cache3:2181 --topic access-log --from-beginning
- 瀏覽器請(qǐng)求nginx
完畢吓笙,利用nginx + lua 實(shí)現(xiàn)請(qǐng)求流量上報(bào)kafka就到這里了。
以上就是本章內(nèi)容巾腕,如有不對(duì)的地方面睛,請(qǐng)多多指教,謝謝尊搬!
為了方便有需要的人叁鉴,本系列全部軟件都在 https://pan.baidu.com/s/1qYsJZfY
下章預(yù)告:主要講解 “ storm 實(shí)時(shí)統(tǒng)計(jì)kafka上報(bào)熱點(diǎn)商品”
作者:逐暗者 (轉(zhuǎn)載請(qǐng)注明出處)