龙空技术网

电商架构实战(一)

青杠山上 233

前言:

现时朋友们对“nginxrespbody”大体比较珍视,朋友们都想要剖析一些“nginxrespbody”的相关内容。那么小编在网上收集了一些对于“nginxrespbody””的相关文章,希望我们能喜欢,同学们一起来学习一下吧!

一、架构讲解

大型电商系统架构图:

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   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   tar -xvf 2.3.tar.gz  cd bundle  wget   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.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   wget  

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) + 1backend = ";..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的值查看效果

4、应用nginx服务器配置

1、下载依赖的包

cd /usr/hello/lualib/resty/  wget   wget  wget  /usr/hello/lualib/resty/htmlcd /usr/hello/lualib/resty/htmlwget 

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/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 then	local http = require("resty.http")	local httpc = http.new()	local resp, err = httpc:request_uri(";,{  		method = "GET",  		path = "/getProductInfo?productId="..productId	})	productCache = resp.body	cache_ngx:set(productCacheKey, productCache, 10 * 60)endif shopCache == "" or shopCache == nil then	local http = require("resty.http")	local httpc = http.new()	local resp, err = httpc:request_uri(";,{  		method = "GET",  		path = "/getShopInfo?shopId="..shopId	})	shopCache = resp.body	cache_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服务器测试:

标签: #nginxrespbody