上传文件至 /
This commit is contained in:
commit
10ab049cf0
3 changed files with 140 additions and 0 deletions
88
openrestyProxyBackend.lua
Normal file
88
openrestyProxyBackend.lua
Normal file
|
@ -0,0 +1,88 @@
|
|||
--[[
|
||||
Created by: wangsuipeng
|
||||
Edit: 20240125
|
||||
Last Modify: 20240126
|
||||
version: 1.1
|
||||
Description:
|
||||
脚本主要用于实现获取redis中的链接。
|
||||
--]]
|
||||
|
||||
-- 参数配置
|
||||
-- 默认upstream
|
||||
local default_backend = "danceaiStream"
|
||||
|
||||
-- redis
|
||||
local redis_host = "60.204.148.84"
|
||||
local redis_port = 6379
|
||||
local redis_pwd = "xm!redis123"
|
||||
local redis_db = 15
|
||||
|
||||
-- 需要获取的zset名称
|
||||
local redis_zset = "danceai"
|
||||
|
||||
-- 连接池最大空闲时间
|
||||
-- 单位为ms
|
||||
local redis_pool_idle_time = 10000
|
||||
-- 连接池大小
|
||||
local redis_pool_size = 100
|
||||
-- redis超时时间
|
||||
-- 单位为ms
|
||||
local redis_timeout =1000
|
||||
|
||||
local redis = require("resty.redis")
|
||||
-- 关闭redis连接
|
||||
local function close_redis(red)
|
||||
if not red then
|
||||
return
|
||||
end
|
||||
--释放连接(连接池实现)
|
||||
local pool_max_idle_time = redis_pool_idle_time
|
||||
local pool_size = redis_pool_size
|
||||
local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "set redis keepalive error : ", err)
|
||||
return ngx.say("redis server error")
|
||||
--ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) -- 500
|
||||
end
|
||||
ngx.log(ngx.INFO,"close redis success")
|
||||
end
|
||||
|
||||
-- 创建连接实例
|
||||
local r = redis:new()
|
||||
-- 超时时间
|
||||
r:set_timeout(redis_timeout)
|
||||
-- 连接redis
|
||||
local ok, err = r:connect(redis_host, redis_port)
|
||||
-- 判断是否连接成功
|
||||
-- 若不成功,使用行配置的upstream
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, "connect redis error : ", err)
|
||||
ngx.var.backend = default_backend
|
||||
return
|
||||
--ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) -- 500
|
||||
end
|
||||
|
||||
-- 验证
|
||||
local rs,err = r:auth(redis_pwd)
|
||||
if not rs then
|
||||
ngx.log(ngx.ERR, "auth redis error : ", err)
|
||||
ngx.var.backend = default_backend
|
||||
return
|
||||
--ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) -- 500
|
||||
end
|
||||
-- 切换到相应db
|
||||
r:select(redis_db)
|
||||
-- 查询zset
|
||||
local data,err = r:zrange(redis_zset,0,0)
|
||||
-- 判断是否查询到
|
||||
-- 若查询失败或结果为空,则使用自行配置的upstream
|
||||
-- 否则,使用查询到的链接
|
||||
if not data or #data == 0 then
|
||||
ngx.log(ngx.WARN, "get backend error : ", err)
|
||||
ngx.var.backend = default_backend
|
||||
else
|
||||
for _,backend in pairs(data) do
|
||||
ngx.var.backend = backend
|
||||
end
|
||||
end
|
||||
close_redis(r)
|
Loading…
Add table
Add a link
Reference in a new issue