分發層nginx,lua應用,會將商品id,商品店鋪id,都轉發到後端的應用nginx
nginx+lua+java多級緩存流程
修改分發層nginx
- 分發層nginx
eshop-cache03<192.168.0.108>
,vi /usr/hello/hello.conf
location /product {
default_type 'text/html';
# lua_code_cache off;
content_by_lua_file /usr/hello/lua/distribute.lua;
}
- 修改
lua
腳本:vi /usr/hello/lua/distribute.lua
local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]
local shopId = uri_args["shopId"]
local host = {"192.168.0.106", "192.168.0.107"}
local hash = ngx.crc32_long(productId)
local index = (hash % 2) + 1
local backend = "http://"..host[index]
local requestPath = uri_args["requestPath"]
requestPath = "/"..requestPath.."?productId="..productId.."&shopId="..shopId
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri(backend, {
method = "GET",
path = requestPath,
keepalive=false
})
if not resp then
ngx.say("request error :", err)
return
end
ngx.say(resp.body)
httpc:close()
- 重新加載nginx配置:
# 驗證配置
/usr/servers/nginx/sbin/nginx -t
# 啓動nginx
/usr/servers/nginx/sbin/nginx
# 重載配置
/usr/servers/nginx/sbin/nginx -s reload
配置應用層nginx
-
應用層nginx
的lua腳本接收到之前分發層nginx
分發的請求 -
獲取請求參數中的商品id:
productId
,以及商品店鋪id:shopId
; -
根據商品id和商品店鋪id,在nginx本地緩存中嘗試獲取數據
-
如果在
nginx本地緩存
中沒有獲取到數據,那麼就到redis分佈式緩存服務
中獲取數據,獲取到了數據後,還要設置到nginx本地緩存中
但是這裏有個問題,建議不要用nginx+lua直接去獲取redis數據,因爲
openresty
沒有太好的redis cluster的支持包,所以建議是發送http請求到緩存數據生產服務,由該服務提供一個http接口
- 緩存數據生產服務可以基於
redis cluster api
從redis中直接獲取數據,並返回給nginx
應用層nginx
會發送http請求
到後端的緩存數據服務
,所以eshop-cache01
和eshop-cache02
也要先引入lua http lib
包
cd /usr/hello/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
-
如果緩存數據生產服務沒有在
redis分佈式緩存
中沒有獲取到數據,那麼就在自己本地ehcache
中獲取數據,返回數據給nginx,也要設置到nginx本地緩存中 -
如果ehcache本地緩存都沒有數據,那麼就需要去原始的服務中拉去數據,該服務會從mysql中查詢,拉去到數據之後,返回給nginx,並重新設置到ehcache和redis中
後面會講分佈式緩存重建併發衝突的問題和解決方案
應用層nginx
最終利用獲取到的數據,動態渲染網頁模板
eshop-cache01
和eshop-cache02
引入lua
包
cd /usr/hello/lualib/resty/
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua
mkdir /usr/hello/lualib/resty/html
cd /usr/hello/lualib/resty/html
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua
vi /usr/hello/hello.conf
:配置模板位置
set $template_location "/templates";
set $template_root "/usr/hello/templates";
- 創建頁面模板目錄和文件
mkdir /usr/hello/templates
vi product.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品詳情頁</title>
</head>
<body>
商品id: {* productId *}<br/>
商品名稱: {* productName *}<br/>
商品圖片列表: {* productPictureList *}<br/>
商品規格: {* productSpecification *}<br/>
商品售後服務: {* productService *}<br/>
商品顏色: {* productColor *}<br/>
商品大小: {* productSize *}<br/>
店鋪id: {* shopId *}<br/>
店鋪名稱: {* shopName *}<br/>
店鋪等級: {* shopLevel *}<br/>
店鋪好評率: {* shopGoodCommentRate *}<br/>
</body>
</html>
- 配置使用nginx的本地緩存
vi /usr/servers/nginx/conf/nginx.conf
# nginx配置全局本地緩存名稱:my_cache,內存大小:128m
lua_shared_dict my_cache 128m;
- 將渲染後的網頁模板作爲http響應,返回給
分發層nginx
vi /usr/hello/hello.conf
location /product {
default_type 'text/html';
content_by_lua_file /usr/hello/lua/product.lua;
}
vi /usr/hello/lua/product.lua
腳本:
-- 獲取請求參數
local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]
local shopId = uri_args["shopId"]
-- 獲取nginx緩存
local cache_ngx = ngx.shared.my_cache
local productCacheKey = "product_info_"..productId
local shopCacheKey = "shop_info_"..shopId
local productCache = cache_ngx:get(productCacheKey)
local shopCache = cache_ngx:get(shopCacheKey)
-- 如果nginx本地緩存沒有,發送請求到緩存服務
if productCache == "" or productCache == nil then
local http = require("resty.http")
local httpc = http.new()
-- 此處ip地址爲你java服務部署或測試啓動地址
local resp, err = httpc:request_uri("http://192.168.0.113:8080",{
method = "GET",
path = "/getProductInfo?productId="..productId,
-- lua lib庫bug,必須設置下面參數
keepalive=false
})
productCache = resp.body
-- 設置到nginx本地緩存中,過期時間10分鐘
cache_ngx:set(productCacheKey, productCache, 10 * 60)
end
if shopCache == "" or shopCache == nil then
local http = require("resty.http")
local httpc = http.new()
local resp, err = httpc:request_uri("http://192.168.0.113:8080",{
method = "GET",
path = "/getShopInfo?shopId="..shopId,
keepalive=false
})
shopCache = resp.body
cache_ngx:set(shopCacheKey, shopCache, 10 * 60)
end
-- 商品信息和店鋪信息轉成json對象
local cjson = require("cjson")
local productCacheJSON = cjson.decode(productCache)
local shopCacheJSON = cjson.decode(shopCache)
local context = {
productId = productCacheJSON.id,
productName = productCacheJSON.name,
productPrice = productCacheJSON.price,
productPictureList = productCacheJSON.pictureList,
productSpecification = productCacheJSON.specification,
productService = productCacheJSON.service,
productColor = productCacheJSON.color,
productSize = productCacheJSON.size,
shopId = shopCacheJSON.id,
shopName = shopCacheJSON.name,
shopLevel = shopCacheJSON.level,
shopGoodCommentRate = shopCacheJSON.goodCommentRate
}
-- 渲染到模板
local template = require("resty.template")
template.render("product.html", context)
到這裏
應用層nginx
配置已經結束。
- 重新加載nginx配置:
注意以上配置兩個應用層nginx都要配置:
eshop-cache01
和eshop-cache02
# 驗證配置
/usr/servers/nginx/sbin/nginx -t
# 啓動nginx
/usr/servers/nginx/sbin/nginx
# 重載配置
/usr/servers/nginx/sbin/nginx -s reload
緩存服務Java代碼
-
可以查看34. 【實戰】基於kafka+ehcache+redis完成緩存數據生產服務的開發與測試,生成測試redis數據
-
項目地址參考:0. 【緩存高可用微服務實戰】資料總結
切換到相應分支:
測試
- 從
分發層nginx
即eshop-cache03 [192.168.0.108]
發出訪問獲取商品信息和商家信息請求: - 商品請求:
http://192.168.0.108/product?requestPath=product&productId=1&shopId=1
- 如果報錯,可以查看日誌
cat /usr/servers/nginx/logs/error.log
我這裏是因爲redis中沒有商家信息緩存,現在也沒有做查詢數據庫操作,lua解碼轉json對象時報錯了。
-
成功請求響應:
-
第一次訪問的時候,在
nginx本地緩存
中是取不到的,會發送http請求
到後端的緩存服務
裏去獲取,會從redis中獲取 -
拿到數據以後,會放到
nginx本地緩存
裏面去,過期時間是之前設置的10分鐘
-
然後將所有數據渲染到模板中,返回模板
-
以後再來訪問的時候,就會直接從nginx本地緩存區獲取數據了
總結
- 緩存數據生產 -> 有數據變更 -> 主動更新兩級緩存(ehcache+redis)-> 緩存維度化拆分
- 分發層nginx + 應用層nginx -> 自定義
流量分發
策略提高緩存命中率 nginx shared dict
緩存 -> 緩存服務 -> redis -> ehcache -> 渲染html模板 -> 返回頁面
如果數據在
nginx -> redis -> ehcache
三級緩存都不在了,可能就是被LRU
清理掉了
這個時候緩存服務會重新拉去數據,去更新到ehcache和redis中,由此會引發下一篇中的
分佈式的緩存重建的併發問題
。