电商平台搭建构思「电商平台组织架构」
大型电商系统架构图:
1、缓存架构Nginx本地缓存 redis分布式缓存 tomcat堆缓存
2、缓存 数据库读写模式
读的时候先从缓存读,没有再去读数据库,从数据库读到了之后写入缓存更新数据时,需要删除缓存更新数据时删除缓存原因:因为有很多时候,缓存不仅仅是数据库取出来的值,而是经过复杂的计算了的。那么更新的代价就比较大。如果更新了100次数据,但是实际只访问几次,那么每次都更新缓存就不划算了。不如等他访问的时候再计算。
3、Nginx双层缓存模型第一层是ngnix分发服务器,第二层是ngnix后端服务器,可以避免每个商品走不同得ngnix,提升ngnix本地缓存命中率
二、Nginx双层缓存架构Nginx Lua部署
1、部署openresty
mkdir -p /usr/servers cd /usr/servers/yum install -y readline-devel pcre-devel openssl-devel gccwget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz tar -xzvf ngx_openresty-1.7.7.2.tar.gz cd /usr/servers/ngx_openresty-1.7.7.2/cd bundle/LuaJIT-2.1-20150120/ make clean && make && make install ln -sf luajit-2.1.0-alpha /usr/local/bin/luajitcd bundle wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz tar -xvf 2.3.tar.gz cd bundle wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz tar -xvf v0.3.0.tar.gz cd /usr/servers/ngx_openresty-1.7.7.2 ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 make && make install cd /usr/servers/ ll/usr/servers/luajit/usr/servers/lualib/usr/servers/nginx/usr/servers/nginx/sbin/nginx -V 启动nginx: /usr/servers/nginx/sbin/nginx注意:启动遇到这个问题
[root@centos01 conf]#nginx: [error] invalid PID number “” in “/usr/servers/nginx/logs/nginx.pid”
解决方法:/usr/servers/nginx/sbin/nginx -c /usr/servers/nginx/conf/nginx.conf
2、配置ngnix lua
1.编辑nginx配置
vi /usr/servers/nginx/conf/nginx.conf
在http部分添加:
lua_package_path "/usr/hello/lualib/?.lua;;"; lua_package_cpath "/usr/hello/lualib/?.so;;"; include /usr/hello/hello.conf;创建hello.confmkdir /usr/hello编辑vi hello.conf
server { listen 80; server_name _; location /hello { default_type 'text/html'; content_by_lua_file /usr/hello/lua/hello.lua; }}编辑hello.lua
mkdir /usr/hello/luacd /usr/hello/luavi hello.lua
ngx.say("hello world");拷贝所需资源
cp -r /usr/servers/lualib/ /usr/hello重新加载配置
/usr/servers/nginx/sbin/nginx -s reload若有三台ngnix服务器,两台作为应用服务器,一台作为分发服务器。
3、分发服务器lua配置:
1、安装http包
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.lua2、编辑lua脚本
其中hostl里面换成另外两台服务器的ipvi /usr/hello/lua/hello.lua
local uri_args = ngx.req.get_uri_args()local productId = uri_args["productId"]local host = {"192.168.1.12", "192.168.1.13"}local hash = ngx.crc32_long(productId)hash = (hash % 2) 1backend = "http://"..host[hash]local method = uri_args["method"]local requestBody = "/"..method.."?productId="..productIdlocal http = require("resty.http")local httpc = http.new()local resp, err = httpc:request_uri(backend, { method = "GET", path = requestBody, keepalive=false})if not resp then ngx.say("request error :", err) returnendngx.say(resp.body)httpc:close()重启nginx
3、请求测试
修改productId的值查看效果http://192.168.1.14/hello?method=hello&productId=5
4、应用nginx服务器配置
1、下载依赖的包
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 wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.luamkdir /usr/hello/lualib/resty/htmlcd /usr/hello/lualib/resty/htmlwget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua2、修改配置
cd /usr/hello/vi hello.conf整体内容为:
server { listen 80; server_name _; set $template_location "/templates"; set $template_root "/usr/hello/templates"; location /hello { default_type 'text/html'; content_by_lua_file /usr/hello/lua/hello.lua; }}3、创建html模板:
mkdir /usr/hello/templatescd /usr/hello/templatesvi product.html<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>商品详情页</title></head><body>product id: {* productId *}<br/>product name: {* productName *}<br/>product picture list: {* productPictureList *}<br/>product specification: {* productSpecification *}<br/>product service: {* productService *}<br/>product color: {* productColor *}<br/>product size: {* productSize *}<br/>shop id: {* shopId *}<br/>shop name: {* shopName *}<br/>shop level: {* shopLevel *}<br/>shop good cooment rate: {* shopGoodCommentRate *}<br/></body></html>4、修改lua脚本
local 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_"..productIdlocal 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.31.179: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.31.179: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)5、修改nginx配置
vi /usr/servers/nginx/conf/nginx.conf在http里加入http { lua_shared_dict my_cache 128m;}6、启动后台服务器提供getProductInfo接口,访问分发的nginx服务器测试:http://192.168.1.14/hello?method=hello&productId=2&shopId=2
相关文章
- java应用程序中使用的流行电子商务框架「j2ee框架」
- 菠萝蜜北方人吃不惯「为什么北方人不能吃辣」
- 互联网背景下供应链金融模式研究「新型供应链金融」
- 内容电商为什么兴起「一切生意的本质是流量」
- 618氛围营造仓库「仓库金点子改善方案」
- 2021年做电商赚钱吗「电商提高客单价的方法」
- 短视频风潮「短视频的风口和趋势」
- 私域流量直播带货「web后端主流框架」
- 双十一物流配送的问题及解决方法「双十一物流问题案例分析」
- 任何人都可以做跨境电商吗「个人能不能做跨境电商」
- 拼多多果园猕猴桃一斤有几个「拼多多水果那么便宜怎么赚钱」
- 河北辛集:手机成\\「直播带货为乡村振兴代言」
- 辛集小伙伴「从事电商」
- 为什么出现网红书店实质「网红书店的井喷式发展引起舆论」
- 为什么微信读书开始卖纸质书「为什么读纸质的书比电子书好」
- 为什么电商从图书开始「为什么电商从图书开始」
- 中国砂石骨料网「中国砂石网」
- 砂石市场难题如何破解「2021年砂石行业发展」