亿级流量电商详情页系统实战-31.应用层nginx缓存实现
1.分发到应用层nginx流程
应用nginx的lua脚本接收到请求
获取请求参数中的商品id,以及商品店铺id
根据商品id和商品店铺id,在nginx本地缓存中尝试获取数据
如果在nginx本地缓存中没有获取到数据,那么就到redis分布式缓存中获取数据,如果获取到了数据,还要设置到nginx本地缓存中
但是这里有个问题,建议不要用nginx+lua直接去获取redis数据。因为openresty没有太好的redis cluster的支持包,所以建议是发送http请求到缓存数据生产服务,由该服务提供一个http接口,缓存数生产服务可以基于redis cluster api从redis中直接获取数据,并返回给nginx。
如果缓存数据生产服务没有在redis分布式缓存中没有获取到数据,那么就在自己本地ehcache中获取数据,返回数据给nginx,也要设置到nginx本地缓存中
如果ehcache本地缓存都没有数据,那么就需要去原始的服务中拉去数据,该服务会从mysql中查询,拉去到数据之后,返回给nginx,并重新设置到ehcache和redis中
nginx最终利用获取到的数据,动态渲染网页模板
2.引入相关依赖
#cd /usr/servers/lualib/resty/
#wget https://codechina.csdn.net/mirrors/pintsized/lua-resty-http/-/tree/master/lib/resty/http_headers.lua
#wget https://codechina.csdn.net/mirrors/pintsized/lua-resty-http/-/tree/master/lib/resty/http.lua #wget https://github.com.com/bungle/lua-resty-template/master/lib/resty/template.lua
#mkdir /usr/servers/lualib/resty/html
#cd /usr/servers/lualib/resty/html
#wget https://github.com/bungle/lua-resty-template/blob/master/lib/resty/template/html.lua
3.规划lua项目结构
/usr/lua/eshop
eshopeshop.conf lua product.luatemplatesproduct.htmllualib *.lua*.so
4.在nginx中配置eshop.conf
# vi /usr/servers/nginx/conf/nginx.confworker_processes 2; error_log logs/error.log; events { worker_connections 1024;
} http { include mime.types; default_type text/html; lua_package_path "/usr/servers/lualib/?.lua;;"; lua_package_cpath "/usr/servers/lualib/?.so;;"; lua_shared_dict my_cache 128m;include /usr/lua/eshop/eshop.conf;
}
5.配置eshop.conf
#mkdir -p /usr/lua/eshop/
#vi /usr/lua/eshop/eshop.conf
server { listen 80; server_name _; set $template_location "/templates"; set $template_root "/usr/lua/eshop/templates";location /product{ default_type 'text/html'; content_by_lua_file /usr/lua/eshop/lua/proudct.lua; }
}
6.编写product.html
#mkdir -p /usr/lua/eshop/templates
#vi /usr/lua/eshop/templates/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>
7.编写produc.lua
# mkdir -p /usr/lua/eshop/lua
# vi /usr/lua/eshop/lua/produc.lualocal uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]
local shopId = uri_args["shopId"]local cache_ngx = ngx.shared.my_cachelocal productCacheKey = "product_info_"..productId
local shopCacheKey = "shop_info_"..shopIdlocal productCache = cache_ngx:get(productCacheKey)
local shopCache = cache_ngx:get(shopCacheKey)if productCache == "" or productCache == nil thenlocal http = require("resty.http")local httpc = http.new()local resp, err = httpc:request_uri("http://192.168.9.253:8080",{method = "GET",path = "/getProductInfo?productId="..productId})productCache = resp.bodycache_ngx:set(productCacheKey, productCache, 10 * 60)
endif shopCache == "" or shopCache == nil thenlocal http = require("resty.http")local httpc = http.new()local resp, err = httpc:request_uri("http://192.168.9.253:8080",{method = "GET",path = "/getShopInfo?shopId="..shopId})shopCache = resp.bodycache_ngx:set(shopCacheKey, shopCache, 10 * 60)
endlocal 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)
8.nginx动态生效
/usr/servers/nginx/sbin/nginx -s reload
9.测试
http://192.168.135.135/eshop?method=product&productId=1&shopId=1
亿级流量电商详情页系统实战-31.应用层nginx缓存实现相关推荐
- 亿级流量电商详情页系统实战:缓存架构+高可用服务架构+微服务架构
<缓存架构+高可用服务架构+微服务架构>深入讲解了亿级流量电商详情页系统的完整大型架构.同时最重要的是,在完全真实的大型电商详情页系统架构下,全流程实战了整套微服务架构,包含了基于领域驱动 ...
- 大型电商架构亿级流量电商详情页系统实战--redis基础票
亿级流量电商详情页系统实战-缓存架构+高可用服务架构+微服务架构 01_高并发高可用复杂系统中的缓存架构有哪些东西 (1)如何让redis集群支撑几十万QPS高并发+99.99%高可用+TB级海量数据 ...
- 19.亿级流量电商详情页系统实战---总结
文章目录 1.亿级流量电商网站的商品详情页系统架构 2.redis企业级集群架构 3.多级缓存架构设计 4.数据库+缓存双写一致性解决方案 5.缓存维度化拆分解决方案 6.缓存命中率提升解决方案 7. ...
- 大型电商架构亿级流量电商详情页系统--实战 服务降级
86_电商网站的商品详情页缓存服务业务背景以及框架结构说明 我们这个课程,基于hystrix,如何来构建高可用的分布式系统的架构,项目实战 模拟真实业务的这么一个小型的项目,来全程贯穿,用这个项目中的 ...
- 大型电商架构亿级流量电商详情页系统实战-缓存架构+高可用服务架构+微服务架构(七)
文章目录 八十九.高并发场景下恐怖的缓存雪崩现象以及导致系统全盘崩溃的后果 九十.缓存雪崩的基于事前+事中+事后三个层次的完美解决方案 九十一.基于hystrix完成对redis访问的资源隔离以避免缓 ...
- 大型电商架构亿级流量电商详情页系统--实战 缓存同步,热点key统计 降级
35 我们之前的三十讲,主要是在讲解redis如何支撑海量数据.高并发读写.高可用服务的架构,redis架构 redis架构,在我们的真正类似商品详情页读高并发的系统中,redis就是底层的缓存 ...
- 6.亿级流量电商详情页系统实战
1.上亿流量的商品详情页系统的多级缓存架构 很多人以为,做个缓存,其实就是用一下redis,访问一下,就可以了,简单的缓存 做复杂的缓存,支撑电商复杂的场景下的高并发的缓存,遇到的问题,非常非常之多, ...
- 1.亿级流量电商详情页系统实战--主节点部署redis
文章目录 实现流程图 缓存架构范围 集群环境搭建 1.在虚拟机中安装CentOS 2.配置网络 3. 配置hosts 4. 关闭防火墙 5.配置yum 6. 在每个CentOS中都安装Java和Per ...
- 亿级流量电商详情页系统实战-1.小型电商网站的商品详情页的页面静态化架构以及其缺陷
1.电商网站按规模分类 电商网站里,大概可以说分成两种: 小型电商 简单的一种架构方案,页面静态化的方案 大型电商 复杂的一套架构,大电商,国内排名前几的电商,大型的详情页架构页面静态化,全量的页面静 ...
最新文章
- PingCode新成员Goals开放内测!
- Redefine:Change in the Changing World
- WPF数据绑定、多个元素
- 详解Windows消息分类以及WM_COMMAND与WM_NOTIFY的区别,以及模拟发送控件通知消息
- python就业班 miniweb框架_mini-web框架
- wincc7的常用c语言,wincc几个常用c语言编程-20210324073153.docx-原创力文档
- java核心技术 下载 网盘_【资源分享】某宝买的40000GB游戏,有你想要的游戏哦,可单独保存或下载...
- [全程建模]rose工具如何打开两个mdl文件问题
- dlib 人脸特征点检测
- 软件工程领域权威期刊
- CDA学习之Pandas - 十分钟搞定Pandas / Pandas秘籍
- Error connecting to node kafka1:9092 (id: 0 rack: null) ,marathon docker镜像添加hosts
- 如何用软件测试交易系统的胜率,通达信官网程序交易测试
- 学日语就是一种煎熬!
- uni-app---第三方登录
- 【FXCG】传奇人物保罗•都铎•琼斯(二)
- Java课程设计-实验室预约管理系统
- 设计 一个高性能爬虫系统
- 云服务器密码忘了_云服务器 root 密码忘记了怎么办
- Oulipo(欧力波)(经典kmp模板题) HDU-1686