更新 Others/根据gpu使用率调度/获取redis中链接的lua脚本.md
This commit is contained in:
parent
113cb0b49d
commit
cd785fef93
1 changed files with 168 additions and 0 deletions
|
@ -1,2 +1,170 @@
|
||||||
```lua
|
```lua
|
||||||
|
--[[
|
||||||
|
Created by: wangsuipeng
|
||||||
|
Edit: 20240125
|
||||||
|
Last Modify: 20240126
|
||||||
|
version: 1.1
|
||||||
|
Description:
|
||||||
|
脚本主要用于实现获取redis中的链接。
|
||||||
|
--]]
|
||||||
|
|
||||||
|
-- 参数配置
|
||||||
|
local redis_host = "60.204.148.84"
|
||||||
|
local redis_port = 6379
|
||||||
|
local redis_pwd = "xm!redis123"
|
||||||
|
local redis_db = 15
|
||||||
|
|
||||||
|
local redis_zset = "danceai"
|
||||||
|
local server_err_msg = "redis server error"
|
||||||
|
|
||||||
|
local redis = require("resty.redis")
|
||||||
|
-- 关闭redis连接
|
||||||
|
local function close_redis(red)
|
||||||
|
if (not red) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
--释放连接(连接池实现)
|
||||||
|
local pool_max_idle_time = 10000 --毫秒
|
||||||
|
local pool_size = 100 --连接池大小
|
||||||
|
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(server_err_msg)
|
||||||
|
--ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) -- 500
|
||||||
|
end
|
||||||
|
ngx.log(ngx.INFO,"close redis success")
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 创建连接实例
|
||||||
|
local r = redis:new()
|
||||||
|
-- 超时时间
|
||||||
|
r:set_timeout(1000)
|
||||||
|
-- 连接redis
|
||||||
|
local ok, err = r:connect(redis_host, redis_port)
|
||||||
|
-- 判断是否连接成功
|
||||||
|
-- 若不成功,使用行配置的upstream
|
||||||
|
if (not ok) then
|
||||||
|
ngx.log(ngx.ERR, "connect redis error : ", err)
|
||||||
|
close_redis(r)
|
||||||
|
ngx.var.backend = "danceaiStream"
|
||||||
|
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 = "danceaiStream"
|
||||||
|
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 = "danceaiStream"
|
||||||
|
else
|
||||||
|
for _,backend in pairs(data) do
|
||||||
|
ngx.var.backend = backend
|
||||||
|
end
|
||||||
|
end
|
||||||
|
close_redis(r)
|
||||||
|
[root@ops lb]# vim lb.lua
|
||||||
|
[root@ops lb]# systemctl restart openresty
|
||||||
|
[root@ops lb]# cat lb.lua
|
||||||
|
--[[
|
||||||
|
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