第四節:千萬級流量下商品詳情頁的全套方案和壓測分析

一.   整體業務再次分析

 

詳細分析:

 

 

 

 

二. 

1. 封裝 requestTemplateRendering.lua 文件,用來抓取網頁文件,放到openresty下的script文件夾中。

 

查看代碼
local template = require("resty.template")
local lrredis = require("redisUtils")
local lgethtml = require("requestHtml")
local lreqparm = require("requestUtils")
--獲取請求參數
local reqParams = lreqparm.getRequestParam()
-- 定義本地緩存
local html_template_cache = ngx.shared.html_template_cache

-- 獲取請求ID的參數
local reqId = reqParams["id"];

ngx.log(ngx.INFO, "requestID:", reqId);
-- 校驗參數
if reqId==nil then
    ngx.say("缺少ID參數");
    return
end

-- 布隆過濾器檢查id是否存在
local bfexist = lrredis.bfexists("bf_taxi",reqId)
ngx.log(ngx.INFO, "布隆過濾器檢驗:", bfexist)

-- 校驗key不存在直接返回
if bfexist==0 then
    ngx.say("布隆過濾器校驗key不存在...")
    return
end

-- 拼接hget的key
local hgetkey = "hkey_".. reqId

-- 通過hget獲取map的所有數據
local templateData = lrredis.hgetall(hgetkey);
if next(templateData) == nil then
    ngx.say("redis沒有存儲數據...")
    return
end


--獲取模板價格數據
local amount = templateData["amount"]
ngx.log(ngx.INFO, "amount:", amount)
if amount == nil then
    ngx.say("價格數據未配置");
    return
end


-- 獲取本地緩存對象
ngx.log(ngx.INFO, "開始從緩存中獲取模板數據----")
local html_template = html_template_cache:get(reqId)
-- 判斷本地緩存是否存在
if html_template == nil then
    -- 獲取模板url中的數據
        ngx.log(ngx.INFO, "緩存中不存在數據開始遠程獲取模板")
    local url = templateData["url"]
        ngx.log(ngx.INFO, "從緩存中獲取的url地址:", url)
    if url == nil then
        ngx.say("URL路徑未配置");
        return
    end
        -- 抓取遠程url的html
        ngx.log(ngx.INFO, "開始抓取模板數據:", url)
    local returnResult = lgethtml.gethtml(url);
    -- 判斷抓取模板是否正常
    if returnResult==nil then
        ngx.say("抓取URL失敗...");
        return
    end
        -- 判斷html狀態
    if returnResult.status==200 then
        html_template = returnResult.body
                --設置模板緩存爲一小時
                ngx.log(ngx.INFO, "將模板數據加入到本地緩存")
        html_template_cache:set(reqId,html_template,60 * 60)
    end
end
ngx.log(ngx.INFO, "緩存中獲取模板數據結束----")

-- 模板渲染
--編譯得到一個lua函數

local func = template.compile(html_template)
local context = {amount=amount}
ngx.log(ngx.INFO, "開始渲染模板數據")
--執行函數,得到渲染之後的內容
local content = func(context)

--通過ngx API輸出
ngx.say(content)

 

2. 配置nginx

(1). 配置本地緩存

# 配置模板緩存
lua_shared_dict html_template_cache 10m;

 

(2). 配置server

 # 6. 千萬流量的全套方案
    server {
        listen 8888;
        charset utf-8;
        server_name test2.hi-whales.com;
        
        # 配置路徑重寫
        location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
           rewrite ^/(.*) http://test1.hi-whales.com/$1 permanent;
        }
    
        #刪除本地緩存
        location /delete {
             default_type text/html;
             content_by_lua '
              local lreqparm = require("requestUtils")
              --獲取請求參數
              local reqParams = lreqparm.getRequestParam()
               -- 定義本地緩存
              local html_template_cache = ngx.shared.html_template_cache

                -- 獲取請求ID的參數
              local reqId = reqParams["id"];

              ngx.log(ngx.INFO, "requestID:", reqId);
              -- 校驗參數
              if reqId==nil then
                  ngx.say("缺少ID參數");
                  return
               end
               -- 獲取本地緩存對象
               html_template_cache:delete(reqId);
               ngx.say("清除緩存成功");
            ';
        }

        location /template {
            default_type text/html;
            content_by_lua_file /root/mydevelop/openresty/script/requestTemplateRendering.lua;     
            # content_by_lua '    ngx.say("<p>hhhhh")     ';           
                  
        }
   }

 

3.初始化數據

#連接redis
redis-cli -c -h 127.0.0.1 -p 6380 -a 123456
# 在redis中添加一個布隆過濾器 錯誤率是0.01 數量是1萬個
BF.RESERVE bf_taxi 0.01 10000 NONSCALING
# 在bf_test 的布隆過濾器添加一個key
BF.ADD bf_taxi 1001
#檢查數據是否存在
BF.EXISTS bf_taxi 1001
# 添加URL以及價格
hset hkey_1001 url http://8.130.72.40/index.html amount 4999

 

4. 訪問

http://test2.hi-whales.com:8888/template?id=1001   ,如下圖,大功告成

 

訪問:http://test2.hi-whales.com:8888/template?id=1002

 

 

三. 壓測

 

 

 

 

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鵬飛)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 聲     明1 : 如有錯誤,歡迎討論,請勿謾罵^_^。
  • 聲     明2 : 原創博客請在轉載時保留原文鏈接或在文章開頭加上本人博客地址,否則保留追究法律責任的權利。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章