12-11 7 views
- openresty提供nginx和lua环境
- resty-redis-cluster链接redis集群
- nginx配置
- lua代码
- lua代码解析图
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/ |
nginx配置
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 |
upstream www.oldversion.deppon.com { server 192.168.218.97:8083 max_fails=3 weight=1 fail_timeout=30s; jvm_route $cookie_JSESSIONID|sessionid reverse; } upstream www.newversion.deppon.com { server 192.168.218.98:8083 max_fails=3 weight=1 fail_timeout=30s; jvm_route $cookie_JSESSIONID|sessionid reverse; } upstream www.version.deppon.com { server 192.168.218.97:8083 max_fails=3 weight=1 fail_timeout=30s; server 192.168.218.98:8083 max_fails=3 weight=1 fail_timeout=30s; jvm_route $cookie_JSESSIONID|sessionid reverse; } lua_package_path "/opt/openresty/lualib/?.lua;"; lua_package_cpath "//opt/openresty/lualib/?.so;"; lua_shared_dict redis_cluster_slot_locks 100k; server { listen 8080; server_name version.deppon.com; charset utf-8; open_log_file_cache max=1000 inactive=60s; access_log /opt/nginx_log1/nginxtest_access.log access; error_log /opt/nginx_log1/nginxtest_error.log info; location @old { proxy_pass http://www.oldversion.deppon.com; } location @new { proxy_pass http://www.newversion.deppon.com; } location @default { proxy_pass http://www.version.deppon.com; } location ~ /cnInterface/(.*) { default_type "text/html"; 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 |
cat /opt/lua/content.lua 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 uri_args = ngx.req.get_uri_args() local request_method = ngx.var.request_method if "GET" == request_method then ngx.exec("@default") else ngx.req.read_body() local args = ngx.req.get_post_args() local redis = require "resty.redis" local red = redis:new() red:set_timeout(1000) local hasOld = nil local hasNew = nil hasNew = red_c:sismember("dopnew", args["ecCompanyId"]) hasOld = red_c:sismember("dopold", args["ecCompanyId"]) if hasOld == 0 and hasNew == 1 then --ngx.say("恭喜你,我们现在相中你了,请走新版本") ngx.exec("@new") end if hasOld == 1 and hasNew == 0 then --ngx.say("对不起,我们抛弃了你,你还是回到老版本吧") ngx.exec("@old") end if hasOld == 1 and hasNew == 1 then --ngx.say("你可以走老版本也利用走新版本") ngx.exec("@default") end if hasOld == 0 and hasNew == 0 then --ngx.say("你可以走老版本也可以走新版本") ngx.exec("@default") end end |
lua代码解析图
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!