电商架构技术干货(一)

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第18天,点击查看活动详情

一、架构讲解

大型电商系统架构图:

TIM截图20190920000455.png 1、缓存架构 nginx本地缓存+redis分布式缓存+tomcat堆缓存

2、缓存+数据库读写模式

  1. 读的时候先从缓存读,没有再去读数据库,从数据库读到了之后写入缓存
  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 gcc

wget 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/luajit

cd 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;
复制代码
  1. 创建hello.conf
mkdir /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/lua
cd /usr/hello/lua
vi 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.lua 
复制代码

2、编辑lua脚本

其中hostl里面换成另外两台服务器的ip
vi /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) + 1
backend = "http://"..host[hash]

local method = uri_args["method"]
local requestBody = "/"..method.."?productId="..productId

local 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)
    return
end

ngx.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.lua

mkdir /usr/hello/lualib/resty/html
cd /usr/hello/lualib/resty/html
wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua
复制代码

2、修改配置

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/templates
cd /usr/hello/templates

vi 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_cache

local productCacheKey = "product_info_"..productId
local shopCacheKey = "shop_info_"..shopId

local productCache = cache_ngx:get(productCacheKey)
local shopCache = cache_ngx:get(shopCacheKey)

if productCache == "" or productCache == nil then
	local 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.body
	cache_ngx:set(productCacheKey, productCache, 10 * 60)
end

if shopCache == "" or shopCache == nil then
	local 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.body
	cache_ngx:set(shopCacheKey, shopCache, 10 * 60)
end

local 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

猜你喜欢

转载自juejin.im/post/7154572798055153700