一、架构讲解 大型电商系统架构图: 1、缓存架构 nginx本地缓存redis分布式缓存tomcat堆缓存 2、缓存数据库读写模式读的时候先从缓存读,没有再去读数据库,从数据库读到了之后写入缓存更新数据时,需要删除缓存 更新数据时删除缓存原因:因为有很多时候,缓存不仅仅是数据库取出来的值,而是经过复杂的计算了的。那么更新的代价就比较大。 如果更新了100次数据,但是实际只访问几次,那么每次都更新缓存就不划算了。不如等他访问的时候再计算。 3、Nginx双层缓存模型 第一层是ngnix分发服务器,第二层是ngnix后端服务器,可以避免每个商品走不同得ngnix,提升ngnix本地缓存命中率二、Nginx双层缓存架构 NginxLua部署 1、部署openrestymkdirpusrserverscdusrserversyuminstallyreadlinedevelpcredevelopenssldevelgccwgethttp:openresty。orgdownloadngxopenresty1。7。7。2。tar。gztarxzvfngxopenresty1。7。7。2。tar。gzcdusrserversngxopenresty1。7。7。2cdbundleLuaJIT2。120150120makecleanmakemakeinstalllnsfluajit2。1。0alphausrlocalbinluajitcdbundlewgethttps:github。comFRiCKLEngxcachepurgearchive2。3。tar。gztarxvf2。3。tar。gzcdbundlewgethttps:github。comyaoweibinnginxupstreamcheckmodulearchivev0。3。0。tar。gztarxvfv0。3。0。tar。gzcdusrserversngxopenresty1。7。7。2。configureprefixusrserverswithhttprealipmodulewithpcrewithluajitaddmodule。bundlengxcachepurge2。3addmodule。bundlenginxupstreamcheckmodule0。3。0j2makemakeinstallcdusrserversllusrserversluajitusrserverslualibusrserversnginxusrserversnginxsbinnginxV启动nginx:usrserversnginxsbinnginx 注意:启动遇到这个问题 〔rootcentos01conf〕 nginx:〔error〕invalidPIDnumberinusrserversnginxlogsnginx。pid 解决方法:usrserversnginxsbinnginxcusrserversnginxconfnginx。conf 2、配置ngnixlua 1。编辑nginx配置 viusrserversnginxconfnginx。conf 在http部分添加:luapackagepathusrhellolualib?。;;luapackagecpathusrhellolualib?。;;includeusrhellohello。创建hello。confmkdirusrhello 编辑vihello。confserver{listen80;locationhello{contentbyluafileusrhelloluahello。}} 编辑hello。lua mkdirusrhellolua cdusrhellolua vihello。luangx。say(helloworld); 拷贝所需资源cprusrserverslualibusrhello 重新加载配置usrserversnginxsbinnginxsreload 若有三台ngnix服务器,两台作为应用服务器,一台作为分发服务器。 3、分发服务器lua配置: 1、安装http包cdusrhellolualibrestywgethttps:raw。githubusercontent。compintsizedluarestyhttpmasterlibrestyhttpheaders。luawgethttps:raw。githubusercontent。compintsizedluarestyhttpmasterlibrestyhttp。lua 2、编辑lua脚本 其中hostl里面换成另外两台服务器的ip viusrhelloluahello。lualocaluriargsngx。req。geturiargs()localproductIduriargs〔productId〕localhost{192。168。1。12,192。168。1。13}localhashngx。crc32long(productId)hash(hash2)1backendhttp:。。host〔hash〕localmethoduriargs〔method〕localrequestBody。。method。。?productId。。productIdlocalhttprequire(resty。http)localhttpchttp。new()localresp,errhttpc:requesturi(backend,{methodGET,pathrequestBody,keepalivefalse})ifnotrespthenngx。say(requesterror:,err)returnendngx。say(resp。body)httpc:close() 重启nginx 3、请求测试 修改productId的值查看效果 http:192。168。1。14hello?methodhelloproductId5 4、应用nginx服务器配置 1、下载依赖的包cdusrhellolualibrestywgethttps:raw。githubusercontent。compintsizedluarestyhttpmasterlibrestyhttpheaders。luawgethttps:raw。githubusercontent。compintsizedluarestyhttpmasterlibrestyhttp。luawgethttps:raw。githubusercontent。combungleluarestytemplatemasterlibrestytemplate。luamkdirusrhellolualibrestyhtmlcdusrhellolualibrestyhtmlwgethttps:raw。githubusercontent。combungleluarestytemplatemasterlibrestytemplatehtml。lua 2、修改配置cdusrhellovihello。conf 整体内容为:server{listen80;locationhello{contentbyluafileusrhelloluahello。}} 3、创建html模板:mkdirusrhellotemplatescdusrhellotemplatesviproduct。htmlhtmlheadmetahttpequivContentTcharsetUTF8title商品详情页titleheadbodyproductid:{productId} productname:{productName} productpicturelist:{productPictureList} productspecification:{productSpecification} productservice:{productService} productcolor:{productColor} productsize:{productSize} shopid:{shopId} shopname:{shopName} shoplevel:{shopLevel} shopgoodcoomentrate:{shopGoodCommentRate} bodyhtml 4、修改lua脚本localuriargsngx。req。geturiargs()localproductIduriargs〔productId〕localshopIduriargs〔shopId〕localcachengxngx。shared。mycachelocalproductCacheKeyproductinfo。。productIdlocalshopCacheKeyshopinfo。。shopIdlocalproductCachecachengx:get(productCacheKey)localshopCachecachengx:get(shopCacheKey)ifproductCacheorproductCachenilthenlocalhttprequire(resty。http)localhttpchttp。new()localresp,errhttpc:requesturi(http:192。168。31。179:8080,{methodGET,pathgetProductInfo?productId。。productId})productCacheresp。bodycachengx:set(productCacheKey,productCache,1060)endifshopCacheorshopCachenilthenlocalhttprequire(resty。http)localhttpchttp。new()localresp,errhttpc:requesturi(http:192。168。31。179:8080,{methodGET,pathgetShopInfo?shopId。。shopId})shopCacheresp。bodycachengx:set(shopCacheKey,shopCache,1060)endlocalcjsonrequire(cjson)localproductCacheJSONcjson。decode(productCache)localshopCacheJSONcjson。decode(shopCache)localcontext{productIdproductCacheJSON。id,productNameproductCacheJSON。name,productPriceproductCacheJSON。price,productPictureListproductCacheJSON。pictureList,productSpecificationproductCacheJSON。specification,productServiceproductCacheJSON。service,productColorproductCacheJSON。color,productSizeproductCacheJSON。size,shopIdshopCacheJSON。id,shopNameshopCacheJSON。name,shopLevelshopCacheJSON。level,shopGoodCommentRateshopCacheJSON。goodCommentRate}localtemplaterequire(resty。template)template。render(product。html,context) 5、修改nginx配置viusrserversnginxconfnginx。conf在http里加入http{luashareddictmycache128m;} 6、启动后台服务器 提供getProductInfo接口,访问分发的nginx服务器测试:http:192。168。1。14hello?methodhelloproductId2shopId2