手记

nginx+lua+template+cache

扬帆起航 继续前进 1

1.nginx命中率低解决方案
分发层----算法------应用层

2.nginx+lua。最流行的开源方式OpenResty,提供了大量组件

OpenResty的安装:https://blog.csdn.net/qqLK123/article/details/80498786
OpenResty地址:https://openresty.org/download/openresty-1.13.6.2.tar.gz
注意安装依赖包:yum –y install pcre-devel openssl-devel readline-devel gcc

命令集合:
mkdir -p /usr/servers
cd /usr/servers/
安装依赖包:
yum install  readline-devel pcre-devel openssl-devel gcc
下载openresty:
wget https://openresty.org/download/openresty-1.13.6.1.tar.gz  
tar -xzvf openresty-1.13.6.1.tar.gz  
cd /usr/servers/openresty-1.13.6.1/

cd bundle/LuaJIT-2.1-20150120/  
make clean && make && make install  
ln -sf luajit-2mkdira /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  
安装openresty:
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

nginx+lua开发简单的hello world

遇到的问题:
yum不好使了,参考博客:https://blog.csdn.net/u012965373/article/details/51313313
yum命令:http://man.linuxde.net/yum

3.nginx+lua实现分发层的开发
获取请求信息
local uri_args = ngx.req.get_uri_args()
local productId = uri_args["productId"]
算法
local hosts = {"192.168.31.187", "192.168.31.19"}
local hash = ngx.crc32_long(productId)
local index = (hash % 2) + 1
backend = "http://"..hosts[index]

local requestPath = uri_args["requestPath"]
requestPath = "/"..requestPath.."?productId="..productId
分发
local http = require("resty.http")
local httpc = http.new()

local resp, err = httpc:request_uri(backend,{
method = "GET",
path = requestPath
})

if not resp then
ngx.say("request error: ", err)
return
end

ngx.say(resp.body)

httpc:close()

4.nginx+lua+template+cache 应用层的开发

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 = "productinfo"..productId
local shopCacheKey = "shopinfo"..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.bodycache_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.bodycache_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)

防火墙了解下!!!

redis推荐博客:
redis集群部署及踩过的坑: https://blog.csdn.net/lsxf_xin/article/details/79442198
redis cluster管理工具redis-trib.rb详解:https://blog.csdn.net/huwei2003/article/details/50973967

java——home: /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

redis集群后续问题:
RubyGems使用RubyChina镜像:https://blog.csdn.net/chinazgr/article/details/52932391
export SSL_CERT_FILE=/usr/local/cacert.pem

pathmunge () {
case ":${PATH}:" in
:"$1":)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}

set $template_location "/templates";set $template_root "/usr/hello/templates";

1人推荐
随时随地看视频
慕课网APP