电商平台搭建构思「电商平台组织架构」
今天给大家普及一下电商平台搭建构思「电商平台组织架构」相关知识,最近很多在问电商平台搭建构思「电商平台组织架构」,希望能帮助到您。
一、架构讲解大型电商系统架构图:
1、缓存架构Nginx本地缓存 redis分布式缓存 tomcat堆缓存
2、缓存 数据库读写模式
读的时候先从缓存读,没有再去读数据库,从数据库读到了之后写入缓存更新数据时,需要删除缓存更新数据时删除缓存原因:因为有很多时候,缓存不仅仅是数据库取出来的值,而是经过复杂的计算了的。那么更新的代价就比较大。如果更新了100次数据,但是实际只访问几次,那么每次都更新缓存就不划算了。不如等他访问的时候再计算。
3、Nginx双层缓存模型第一层是ngnix分发服务器,第二层是ngnix后端服务器,可以避免每个商品走不同得ngnix,提升ngnix本地缓存命中率
二、Nginx双层缓存架构Nginx Lua部署
1、部署openresty
mkdir -p /usr/serverscd /usr/servers/yum install -y readline-devel pcre-devel openssl-devel gccwget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gztar -xzvf ngx_openresty-1.7.7.2.tar.gzcd /usr/servers/ngx_openresty-1.7.7.2/cd bundle/LuaJIT-2.1-20150120/make clean && make && make installln -sf luajit-2.1.0-alpha /usr/local/bin/luajitcd bundlewget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gztar -xvf 2.3.tar.gzcd bundlewget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gztar -xvf v0.3.0.tar.gzcd /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/ -j2make && 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.luawget 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 thenngx.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.luawget 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