Lua / Openresty: work with redis master-slave replication and redis sentinel


We use lua-resty-redis-connector

Install lua-resty-redis-connector

luarocks install lua-resty-redis-connector

Example of usage:

	location = /test/sentinel {
		default_type 'text/html';
		content_by_lua_block {
		
			local rc = require("resty.redis.connector").new({
				connect_timeout = 50,
				send_timeout = 5000,
				read_timeout = 5000,
				keepalive_timeout = 30000
			})
		
			local redis, err = rc:connect{
				url = "sentinel://redis-test:a/2",
				sentinels = {
					{ host = "5.161.72.222", port = 26379 },
					{ host = "5.161.61.47", port = 26379 },
					{ host = "5.161.97.150", port = 26379 }
				}
			}
			
			if not redis then
				ngx.say("failed to connect: ", err)
				return
			end
			 
			local ok
			ok, err = redis:set("dog", "an animal")
			if not ok then
				ngx.say("failed to set dog: ", err)
				return
			end

			ngx.say("set result: ", ok)

			local res, err = redis:get("dog")
			if not res then
				ngx.say("failed to get dog: ", err)
				return
			end
			
			ngx.say(res)
			  
		}
	}

Online demo: http://5.161.72.222/test/sentinel

1 Comment

Leave a Reply