- 今日推薦
- 特別關注
電商平臺搭建構思「電商平臺組織架構」
大型電商系統架構圖:
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年砂石行業發展」