28 “分发层 + 应用层” 双层nginx 架构 之 应用层实现

上一篇 “分发层 + 应用层” 双层nginx 架构 之 分发, 主要讲解了基于openResty部署 nginx 分发层,及如何提高nginx 缓存命中率,本篇继续上两篇话题,实现应用层数据缓存更新,为模板提供数据来源。

应用层 nginx 思路逻辑

  • 分发层nginx 请求应用层,nginx lua 接收请求
  • 获取请求参数
  • 根据请求参数到nginx本地缓存获取数据
  • nginx 本地缓存没有数据,请求缓存服务(数据获取
    顺序: redis > ehcache > mysql)获取数据,并设置本地缓存
  • nginx 数据组装,动态渲染网页模板,响应分发层nginx

下面利用上两篇准备好的环境,来实现上面逻辑,以下操作基于应用层nginx (192.168.0.16,192.168.0.17)来操作

为了实现以上逻辑,我们还需要为nginx 提供http、template 支持,因此需要为应用层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

创建html 模板位置,编写 html templates 模板代码

  • 创建模板路径

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/>
         商品规格: {* productSpecification *}<br/>
         商品售后服务: {* 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 里面加上以下内容:

lua_shared_dict test_cache 128m; 声明一个 大小128m 的 test_cache 对象

声明 test_cache 对象

在test 项目中配置 template 路径信息

vim /usr/local/test/test.conf
在 server 里面加上以下内容:

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

实现应用层nginx 代码逻辑

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

代码如下:

// 获取请求参数
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)
// 判断 商品缓存 是否有数据,如果没有,发送请求到商品缓存服务获取
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
      })
      // 获取请求响应数据,并设置nginx 本地缓存,过期时间为 10 分钟
      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
      })
       // 获取请求响应数据,并设置nginx 本地缓存,过期时间为 10 分钟
      shop_cache = resp.body
      cache_ngx:set(shop_cache_key, shop_cache, 10 * 60)
end
// 引入json 解析类库
local cjson = require("cjson")
// 对商品、店铺数据进行json 转换
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
}
// 引入template 解析渲染类库
local template = require("resty.template")
// 渲染模板
template.render("product.html", context)

验证应用层

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

第一次访问
第一次后端缓存服务日志
第二次访问
第二次后端缓存服务日志

通过上面可以看出,当第一次访问是,应用层nginx 缓存没有数据,故请求后端服务获取数据;第二次请求是,直接nginx 缓存取数据,没有走后端服务。

总结:用户请求nginx 后,首先从nginx 本地缓存获取数据,后端服务获取。这里我们的目的就达到了

以上就是本章内容,如有不对的地方,请多多指教,谢谢!

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

下章预告:主要讲解 分布式缓存重建并发冲突问题以及zookeeper分布式锁解决方案

作者:逐暗者 (转载请注明出处)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279

推荐阅读更多精彩内容