c语言sscanf函数的用法是什么
311
2022-09-04
OpenResty部署nginx及nginx+lua
因为用nginx+lua去开发,所以会选择用最流行的开源方案,就是用OpenResty
nginx+lua打包在一起,而且提供了包括redis客户端,mysql客户端,-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- --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
(2)nginx+lua开发的hello world
vi /usr/servers/nginx/conf/nginx.conf在"/usr/servers/lualib/?.lua;;"; lua_package_cpath "/usr/servers/lualib/?.so;;"; /usr/servers/nginx/conf下,创建一个lua.confserver { listen 80; server_name _; } 在nginx.conf的lua.conf;验证配置是否正确:/usr/servers/nginx/sbin/nginx -t在lua.conf的server部分添加:location /lua { default_type 'text/html'; content_by_lua 'ngx.say("hello world")'; } /usr/servers/nginx/sbin/nginx -t 重新nginx加载配置/usr/servers/nginx/sbin/nginx -s reload 访问/usr/servers/nginx/conf/lua/test.luangx.say("hello world"); 修改lua.conflocation /lua { default_type 'text/html'; content_by_lua_file conf/lua/test.lua; }
查看异常日志tail -f /usr/servers/nginx/logs/error.log(3)工程化的nginx+lua项目结构项目工程结构hello hello.conf lua hello.lua lualib *.lua *.so放在/usr/hello目录下/usr/servers/nginx/conf/nginx.confworker_processes 2; error_log logs/error.log; events { worker_connections 1024; } { include mime.types; default_type text/html; lua_package_path "/usr/hello/lualib/?.lua;;"; lua_package_cpath "/usr/hello/lualib/?.so;;"; include /usr/hello/hello.conf; } /usr/hello/hello.confserver { listen 80; server_name _; location /lua { default_type 'text/html'; lua_code_cache off; content_by_lua_file /usr/example/lua/test.lua; } }
用eshop-cache01和eshop-cache02作为应用层nginx服务器,用eshop-cache03作为分发层nginx
在eshop-cache03,也就是分发层nginx中,编写lua脚本,完成基于商品id的流量分发策略
步骤:
1、获取请求参数,比如productId2、对productId进行hash3、hash值对应用服务器数量取模,获取到一个应用服务器4、利用lib包
cd /usr/hello/lualib/resty/ wget wget 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) + 1backend = "requestPath = uri_args["requestPath"]requestPath = "/"..requestPath.."?productId="..productIdlocal = require("resty.= resp, err = method = "GET", path = requestPath})if not resp then ngx.say("request error: ", err) returnendngx.say(resp.body)-s reload
1、应用nginx的lua脚本接收到请求
2、获取请求参数中的商品id,以及商品店铺id
3、根据商品id和商品店铺id,在nginx本地缓存中尝试获取数据
4、如果在nginx本地缓存中没有获取到数据,那么就到redis分布式缓存中获取数据,如果获取到了数据,还要设置到nginx本地缓存中
但是这里有个问题,建议不要用nginx+lua直接去获取redis数据
因为openresty没有太好的redis cluster的支持包,所以建议是发送cluster api从redis中直接获取数据,并返回给nginx
cd /usr/hello/lualib/resty/ wget wget /usr/hello/lualib/resty/wget /usr/hello/lualib/resty/htmlcd /usr/hello/lualib/resty/htmlwget $template_location "/templates"; set $template_root "/usr/hello/templates";mkdir /usr/hello/templatesvi product.htmlproduct id: {* productId *}
product name: {* productName *}
product picture list: {* productPictureList *}
product specification: {* productSpecification *}
product service: {* productService *}
product color: {* productColor *}
product size: {* productSize *}
shop id: {* shopId *}
shop name: {* shopName *}
shop level: {* shopLevel *}
shop good cooment rate: {* shopGoodCommentRate *}
8、将渲染后的网页模板作为my_cache 128m;
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 = require("resty. local = local resp, err = method = "GET", path = "/getProductInfo?productId="..productId }) productCache = resp.body cache_ngx:set(productCacheKey, productCache, 10 * 60)endif shopCache == "" or shopCache == nil then local = require("resty. local = local resp, err = 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)
第一次访问的时候,其实在nginx本地缓存中是取不到的,所以会发送-> 有数据变更 -> 主动更新两级缓存(ehcache+redis)-> 缓存维度化拆分
分发层nginx + 应用层nginx -> 自定义流量分发策略提高缓存命中率
nginx shared dict缓存 -> 缓存服务 -> redis -> ehcache -> 渲染html模板 -> 返回页面
如果你的数据在nginx -> redis -> ehcache三级缓存都不在了,可能就是被LRU清理掉了
这个时候缓存服务会重新拉去数据,去更新到ehcache和redis中
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~