12-11 4 views
- openresty提供nginx和lua环境
- resty-redis-cluster链接redis集群
- lua-resty-cookie操作cookie模块
- nginx配置
- lua代码
- lua代码解析图
- 后续将介绍:lua+openresty操作post方法的参数实现不通参数走不通通道的灰度发布
openresty提供nginx和lua环境
1 2 3 4 |
cd openresty-1.13.6.2 ./configure --prefix=/opt/openresty gmake gmake install |
resty-redis-cluster链接redis集群
1 2 3 4 |
unzip resty-redis-cluster-master.zip cd resty-redis-cluster-master/lib/ gcc redis_slot.c -fPIC -shared -o redis_slot.so cp rediscluster.lua redis_slot.so /opt/openresty/lualib/ |
lua-resty-cookie操作cookie模块
1 2 3 |
unzip lua-resty-cookie-master.zip cd lua-resty-cookie-master/lib/resty/ cp -r cookie.lua /opt/openresty/lualib/resty/ |
nginx配置
1 2 3 4 5 6 7 8 9 10 11 |
cat /opt/openresty/nginx/conf/nginx.conf http { lua_package_path "/opt/openresty/lualib/?.lua;"; lua_package_cpath "//opt/openresty/lualib/?.so;"; lua_shared_dict redis_cluster_slot_locks 100k; location /test { default_type "text/html"; charset gbk; content_by_lua_file "/opt/lua/content.lua"; } |
lua代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
local ck = require "resty.cookie" local cjson = require "cjson" local p = ngx.req.get_uri_args() local Cookies = "_ati=" .. p['_ati'] .."; com.itnotebooks.lib=" .. p['com.itnotebooks.lid'] .. "; SESSION=" .. p['SESSION'] .. "; expires=" .. ngx.cookie_time(ngx.time() + 86400) local _ati = p['_ati'] local itnotebooks= p['com.itnotebooks.lid'] local SESSION = p['SESSION'] local cookie, err = ck:new() if not cookie then ngx.log(ngx.ERR, err) return end -- set cookie key Name local ok, err = cookie:set({ key = "Name", value = "liwanwei", expires = "Wed, 09 Jun 2021 10:18:14 GMT" }) if not ok then ngx.log(ngx.ERR, err) return end -- set cookie key _ati local ok, err = cookie:set({ key = "_ati", value = _ati, }) if not ok then ngx.log(ngx.ERR, err) return end -- set cookie key com.itnotebooks.lid local ok, err = cookie:set({ key = "com.itnotebooks.lid", value = itnotebooks, }) if not ok then ngx.log(ngx.ERR, err) return end -- set cookie key SEESION local ok, err = cookie:set({ key = "SESSION", value = SESSION, }) if not ok then ngx.log(ngx.ERR, err) return end ngx.say("<br />") ngx.say(cjson.encode(p)) ngx.say("<br />") ngx.say("_ati: " .. p['_ati']) ngx.say("<br />") ngx.say("com.itnotebooks.lid: " .. p['com.itnotebooks.lid']) ngx.say("<br />") ngx.say("SESSION: " .. p['SESSION']) ngx.say("<br />") -- get all cookies local fields, err = cookie:get_all() if not fields then ngx.log(ngx.ERR, err) return end for k, v in pairs(fields) do ngx.say(k, " => ", v) ngx.say("<br />") end -- get cookie SESSION local field, err = cookie:get("SESSION") if not field then ngx.log(ngx.ERR, err) return end ngx.say("GET SESSION :") ngx.say("<br />") ngx.say(field) local hashkey = "redisson_spring_session:" .. field ngx.say("<br />") ngx.say("HASHKEY :") ngx.say("<br />") ngx.say(hashkey) local config = { name = "testCluster", enableSlaveRead = true, serv_list = { { ip = "10.9.15.32", port = 6388 }, { ip = "10.9.15.38", port = 6379 }, { ip = "10.9.15.33", port = 6383 }, { ip = "10.9.15.36", port = 6389 }, { ip = "10.9.15.34", port = 6382 }, { ip = "10.9.15.35", port = 6382 } }, keepalive_timeout = 60000, keepalive_cons = 1000, connection_timout = 1000, max_redirection = 5, -- auth = "pass" } local redis_cluster = require "rediscluster" local red_c = redis_cluster:new(config) local key = "\"GENERIC_WEB_CUSTOMER\"" local customer, err = red_c:hget(hashkey, key) if not customer then ngx.log(ngx.ERR, "err: ", err) else ngx.say("<br />") ngx.say(customer) data = cjson.decode(customer) ngx.say("<br />") ngx.say("Get Username: ") ngx.say(data.username) ngx.say("<br />") end } |
lua代码解析图
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!
写的真细致