openstry lua redis实现负载均衡

时间:2023-03-09 19:36:26
openstry lua redis实现负载均衡

需求:

通过URI地址http://10.0.0.148/test2?uuid=123的uuid参数值的第一位,去实现redis的负载均衡

若uuid第一位为1,那么去10.0.0.148的redis,若uuid第一位为2,那么去10.0.0.248的redis

测试的时候148的redis里给foo赋值kkk(./redis-cli   set foo kkk),248的不赋值

这里10.0.0.148既是nginx服务器,也是redis服务器

方法一:通过加载lua文件的方式

test2.lua

--使用require来加载模块名--
local redis = require "resty.redis" --创建一个对象--
local cache = redis.new() --获取URI里的参数uuid
local arg = ngx.var.arg_uuid --截取参数值的第一个字符
local uuid_f = string.sub(arg,,) --将第一个参数值转换为整形
uuid_f=tonumber(uuid_f) --通过列表定义redis的IP
local numbers = {[]="10.0.0.148",[]="10.0.0.248"} --根据uuid的不同参数值去连接不同的redis服务器
local ok,err = cache.connect(cache,numbers[uuid_f],'') --判断是否能连接上
if not ok then
ngx.say("failed to connect:",err)
return
end --测试获取redis的foo变量的值
local res = cache:get("foo")

if res==ngx.null then
ngx.say("This is null")
else
ngx.say(res)
end cache:close()

在nginx.conf中添加如下配置:

location /test2 {
default_type text_plain;
content_by_lua_file /usr/local/test2.lua;
}

测试结果:

浏览器访问:http://10.0.0.148/test2?uuid=123   那么将会去10.0.0.148的redis获取
                 http://10.0.0.148/test2?uuid=223   那么将会去10.0.0.248的redis获取
openstry lua redis实现负载均衡
openstry lua redis实现负载均衡
方法二:在nginx的配置文件里直接写 init_by_lua定义全局变量  在localcation /test2 下的为局部变量以及判断
#user  nobody;
worker_processes ; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; sendfile on; keepalive_timeout ; init_by_lua '
redis = require "resty.redis"
numbers = {[""]="10.0.0.148",[""]="10.0.0.248"}
'; server {
listen ;
server_name localhost; location / {
root html;
index index.html index.htm;
} location /echo {
default_type text/plain;
echo hello lua;
} location /test {
default_type text_plain;
content_by_lua 'ngx.say("Hi lile")';
}
location /re {
default_type text_plain;
content_by_lua '
local headers = ngx.req.get_headers()
local ip = headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
ngx.say(ip.."#"..os.time().."")
';
} location /test2 {
#default_type application/json;
default_type text_plain;
content_by_lua '
local redis_client = redis.new()
local arg = ngx.var.arg_uuid
local uuid_f = string.sub(arg,,)
local ok,err=redis_client.connect(redis_client,numbers[uuid_f],"")
if not ok then
ngx.say("failed to connect:",err)
return
end local res = redis_client:get("foo") if res==ngx.null then
ngx.say("This is null")
else
ngx.say(res)
end redis_client:close()
';
} error_page /50x.html;
location = /50x.html {
root html;
}
}
}

总结:

从昨天开始弄这个的时候,完全没接触过啊,redis 是数据库都不知道,只知道这个名字,lua也是第一次听啊,nginx一直想好好研究一下,但是都没行动过,然后就使劲的在网上找资料,发现基本上都一样,弄的很复杂很复杂,今天早上来,不知道为啥,突然就大脑很清醒,想的很简单,没想到反而这样可以,可能是昨晚睡得比较好,精神也好,最近被达康书记给迷倒了,每天熬夜追剧,话说真的睡眠真的可以让你精神倍儿好,心情也会好。nginx通过lua,根据自己的规则去访问redis服务器,首先我肯定得用lua实现nginx与redis的连接,所以就有那个加载模块,创建对象,连接这几个步骤,这相当于只有你想连上redis,你就得这么做,然后我要根据参数来定规则,我肯定得获取这个参数的值,然后进行转换....当然,这只是事后的想法,现在都不相信我居然弄了这个,虽然过两天,这些又忘了,但是至少我知道有这个东西,知道怎么分析问题,而不是一个劲的去搜怎么做。

比较好的资料:http://www.cnblogs.com/wangxusummer/p/4309007.html  ngx_lua模块
                   http://www.cnblogs.com/huligong1234/p/4163832.html 对请求进行限制,这个带来了灵感
                   http://www.runoob.com/lua/lua-tutorial.html   lua基础语法  不会的在这查就好