28 “分發(fā)層 + 應(yīng)用層” 雙層nginx 架構(gòu) 之 應(yīng)用層實現(xiàn)

上一篇 “分發(fā)層 + 應(yīng)用層” 雙層nginx 架構(gòu) 之 分發(fā), 主要講解了基于openResty部署 nginx 分發(fā)層,及如何提高nginx 緩存命中率,本篇繼續(xù)上兩篇話題,實現(xiàn)應(yīng)用層數(shù)據(jù)緩存更新,為模板提供數(shù)據(jù)來源。

應(yīng)用層 nginx 思路邏輯

  • 分發(fā)層nginx 請求應(yīng)用層,nginx lua 接收請求
  • 獲取請求參數(shù)
  • 根據(jù)請求參數(shù)到nginx本地緩存獲取數(shù)據(jù)
  • nginx 本地緩存沒有數(shù)據(jù),請求緩存服務(wù)(數(shù)據(jù)獲取
    順序: redis > ehcache > mysql)獲取數(shù)據(jù),并設(shè)置本地緩存
  • nginx 數(shù)據(jù)組裝,動態(tài)渲染網(wǎng)頁模板,響應(yīng)分發(fā)層nginx

下面利用上兩篇準(zhǔn)備好的環(huán)境,來實現(xiàn)上面邏輯,以下操作基于應(yīng)用層nginx (192.168.0.16,192.168.0.17)來操作

為了實現(xiàn)以上邏輯,我們還需要為nginx 提供http、template 支持,因此需要為應(yīng)用層nginx test 項目引入http 、template 依賴

引入 http 依賴
cd /usr/local/test/lualib/resty
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua  
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua 
引入 template 依賴
cd /usr/local/test/lualib/resty
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua
mkdir /usr/local/test/lualib/resty/html
cd /usr/local/test/lualib/resty/html
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua

創(chuàng)建html 模板位置,編寫 html templates 模板代碼

  • 創(chuàng)建模板路徑

mkdir /usr/local/test/templates

  • 編寫模板代碼 - product.html

vim product.html

加入以下代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品詳情頁</title>
</head>
<body>
         商品id: {* productId *}<br/>
         商品名稱: {* productName *}<br/>
         商品圖片列表: {* productPictureList *}<br/>
         商品規(guī)格: {* productSpecification *}<br/>
         商品售后服務(wù): {* productService *}<br/>
         商品顏色: {* productColor *}<br/>
         商品大小: {* productSize *}<br/>
         店鋪id: {* shopId *}<br/>
         店鋪名稱: {* shopName *}<br/>
         店鋪等級: {* shopLevel *}<br/>
         店鋪好評率: {* shopRate*}<br/>
</body>
</html>

定義 nginx 本地緩存

cd /usr/local/servers/nginx/conf && vim nginx.conf

在 http 里面加上以下內(nèi)容:

lua_shared_dict test_cache 128m; 聲明一個 大小128m 的 test_cache 對象

聲明 test_cache 對象

在test 項目中配置 template 路徑信息

vim /usr/local/test/test.conf
在 server 里面加上以下內(nèi)容:

set $template_location "/templates";  
set $template_root "/usr/local/test/templates";
配置 template 路徑信息

實現(xiàn)應(yīng)用層nginx 代碼邏輯

vim /usr/local/test/lua/test.lua

代碼如下:

// 獲取請求參數(shù)
local uri_args = ngx.req.get_uri_args()
local product_id = uri_args["productId"]
local shop_id = uri_args["shopId"]
// 獲取之前聲明的test_cache 緩存對象
local cache_ngx = ngx.shared.test_cache
//組裝商品、店鋪緩存key
local product_cache_key = "product_info_"..product_id
local shop_cache_key = "shop_info_"..shop_id
// 從nginx 本地緩存中獲取商品、店鋪信息
local product_cache = cache_ngx:get(product_cache_key)
local shop_cache = cache_ngx:get(shop_cache_key)
// 判斷 商品緩存 是否有數(shù)據(jù),如果沒有,發(fā)送請求到商品緩存服務(wù)獲取
if product_cache == "" or product_cache == nil then
      local http = require("resty.http")
      local httpc = http.new()
       // 發(fā)送請求到商品緩存服務(wù)獲取
      local resp, err = httpc:request_uri("http://192.168.0.3:81",{
            method = "GET",
            path = "/getProductInfo?productId="..product_id
      })
      // 獲取請求響應(yīng)數(shù)據(jù),并設(shè)置nginx 本地緩存,過期時間為 10 分鐘
      product_cache = resp.body
      cache_ngx:set(product_cache_key, product_cache, 10 * 60)
end
// 判斷 店鋪緩存 是否有數(shù)據(jù),如果沒有,發(fā)送請求到店鋪緩存服務(wù)獲取
if shop_cache == "" or shop_cache == nil then
      local http = require("resty.http")
      local httpc = http.new()
     // 發(fā)送請求到店鋪緩存服務(wù)獲取
      local resp, err = httpc:request_uri("http://192.168.0.3:81",{
            method = "GET",
            path = "/getShopInfo?shopId="..shop_id
      })
       // 獲取請求響應(yīng)數(shù)據(jù),并設(shè)置nginx 本地緩存,過期時間為 10 分鐘
      shop_cache = resp.body
      cache_ngx:set(shop_cache_key, shop_cache, 10 * 60)
end
// 引入json 解析類庫
local cjson = require("cjson")
// 對商品、店鋪數(shù)據(jù)進(jìn)行json 轉(zhuǎn)換
local product_cache_json = cjson.decode(product_cache)
local shop_cache_json = cjson.decode(shop_cache)
// 構(gòu)造模板渲染對象
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
}
// 引入template 解析渲染類庫
local template = require("resty.template")
// 渲染模板
template.render("product.html", context)

驗證應(yīng)用層

nginx 校驗 及 重載

/usr/local/servers/nginx/sbin/nginx -t && /usr/local/servers/nginx/sbin/nginx -s reload

瀏覽器驗證

http://192.168.0.18/test?reqUrl=test&productId=1&shopId=1

第一次訪問
第一次后端緩存服務(wù)日志
第二次訪問
第二次后端緩存服務(wù)日志

通過上面可以看出,當(dāng)?shù)谝淮卧L問是,應(yīng)用層nginx 緩存沒有數(shù)據(jù),故請求后端服務(wù)獲取數(shù)據(jù);第二次請求是,直接nginx 緩存取數(shù)據(jù),沒有走后端服務(wù)。

總結(jié):用戶請求nginx 后,首先從nginx 本地緩存獲取數(shù)據(jù),后端服務(wù)獲取。這里我們的目的就達(dá)到了

以上就是本章內(nèi)容,如有不對的地方,請多多指教,謝謝!

為了方便有需要的人,本系列全部軟件都在 https://pan.baidu.com/s/1qYsJZfY

下章預(yù)告:主要講解 分布式緩存重建并發(fā)沖突問題以及zookeeper分布式鎖解決方案

作者:逐暗者 (轉(zhuǎn)載請注明出處)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,003評論 2 374

推薦閱讀更多精彩內(nèi)容