lua get reused time_OpenResty(Nginx+Lua+Redis)高并发最佳实践

c5b81cf64731336571ddcbf14287960f.gif

首先,安装OpenResty和redis。

下载OpenResty,地址:http://openresty.org/cn/download.html。

a4b84b8cb2e64126d06820b8888e6e36.png

点击另存为,通过浏览器就可以下载。

下载后,解压,如下图:

265f9667db32ac88c267e9be0375e43a.png

6729a8a86422bb3a76dec578a29b9de6.png

安装win 版本的redis,可以网上搜下。解压图,如下:

1199da1afbf69a76cbfd1ed81bd31d0d.png

cmd进入目录,运行redis-server.exe,

02b6668d4a7a4d36fe841542c24d21db.png

打开客户端,redis.cli.exe,存入数据,如下:

8a374b10506e3e41a4f34c0df7a421aa.png

其次,lua开发

在lualib\下建立testcode文件夹,然后建立文件lua文件。config_constant.lua,

config = {}config.redisConfig = {    redis_a = { -- your connection name         --ip        host = '127.0.0.1',        --端口        port = 6379,        --密码        -- pass = '123456789',        --超时时间,如果是测试环境debug的话,这个值可以给长一点;如果是正式环境,可以设置为200        timeout = 120000,        --redis的库        database = 0,    },--    redis_b = {--    --        host = '127.0.0.1',--    --        port = 6379,--    --       pass = '',--    --        timeout = 200,--    --        database = 0,--    --    },    }return config

redis_factory.lua 

local redis_factory = function(h)        local h           = h    h.redis           = require('resty.redis')    h.cosocket_pool   = {max_idel = 10000, size = 10000}    h.commands        = {        "append",            "auth",              "bgrewriteaof",        "bgsave",            "bitcount",          "bitop",        "blpop",             "brpop",        "brpoplpush",        "client",            "config",        "dbsize",        "debug",             "decr",              "decrby",        "del",               "discard",           "dump",        "echo",        "eval",              "exec",              "exists",        "expire",            "expireat",          "flushall",        "flushdb",           "get",               "getbit",        "getrange",          "getset",            "hdel",        "hexists",           "hget",              "hgetall",        "hincrby",           "hincrbyfloat",      "hkeys",        "hlen",        "hmget",             "hmset",             "hscan",        "hset",        "hsetnx",            "hvals",             "incr",        "incrby",            "incrbyfloat",       "info",        "keys",        "lastsave",          "lindex",            "linsert",        "llen",              "lpop",              "lpush",        "lpushx",            "lrange",            "lrem",        "lset",              "ltrim",             "mget",        "migrate",        "monitor",           "move",              "mset",        "msetnx",            "multi",             "object",        "persist",           "pexpire",           "pexpireat",        "ping",              "psetex",            "psubscribe",        "pttl",        "publish",           "punsubscribe",      "pubsub",        "quit",        "randomkey",         "rename",            "renamenx",        "restore",        "rpop",              "rpoplpush",         "rpush",        "rpushx",            "sadd",              "save",        "scan",              "scard",             "script",        "sdiff",             "sdiffstore",        "select",            "set",               "setbit",        "setex",             "setnx",             "setrange",        "shutdown",          "sinter",            "sinterstore",        "sismember",         "slaveof",           "slowlog",        "smembers",          "smove",             "sort",        "spop",              "srandmember",       "srem",        "sscan",        "strlen",            "subscribe",         "sunion",        "sunionstore",       "sync",              "time",        "ttl",        "type",              "unsubscribe",       "unwatch",        "watch",             "zadd",              "zcard",        "zcount",            "zincrby",           "zinterstore",        "zrange",            "zrangebyscore",     "zrank",        "zrem",              "zremrangebyrank",   "zremrangebyscore",        "zrevrange",         "zrevrangebyscore",  "zrevrank",        "zscan",        "zscore",            "zunionstore",       "evalsha",        -- resty redis private command        "set_keepalive",     "init_pipeline",     "commit_pipeline",              "array_to_hash",     "add_commands",      "get_reused_times",    }    -- connect    -- @param table connect_info, e.g { host="127.0.0.1", port=6379, pass="", timeout=1000, database=0}    -- @return boolean result    -- @return userdata redis_instance    h.connect = function(connect_info)        local redis_instance = h.redis:new()        redis_instance:set_timeout(connect_info.timeout)        if not redis_instance:connect(connect_info.host, connect_info.port) then             return false, nil        end        if connect_info.pass ~= '' then            redis_instance:auth(connect_info.pass)        end        redis_instance:select(connect_info.database)        return true, redis_instance    end    -- spawn_client    -- @param table h, include config info    -- @param string name, redis config name    -- @return table redis_client    h.spawn_client = function(h, name)        local self = {}                self.name           = ""        self.redis_instance = nil        self.connect        = nil        self.connect_info   = {            host = "",   port = 0,    pass = "",             timeout = 0, database = 0        }        -- construct        self.construct = function(_, h, name)            -- set info            self.name         = name            self.connect      = h.connect            self.connect_info = h[name]            -- gen redis proxy client            for _, v in pairs(h.commands) do                self[v] = function(self, ...)                    -- instance test and reconnect                      if (type(self.redis_instance) == 'userdata: NULL' or type(self.redis_instance) == 'nil') then                        local ok                        ok, self.redis_instance = self.connect(self.connect_info)                        if not ok then return false end                    end                    -- get data                    local vas = { ... }                    return self.redis_instance[v](self.redis_instance, ...)                end            end            return true        end        -- do construct        self:construct(h, name)         return self    end         local self = {}    self.pool  = {} -- redis client name pool    -- construct    -- you can put your own construct code here.    self.construct = function()        return    end    -- spawn    -- @param string name, redis database serial name    -- @return boolean result    -- @return userdata redis    self.spawn = function(_, name)        if self.pool[name] == nil then            ngx.ctx[name] = h.spawn_client(h, name)             self.pool[name] = true            return true, ngx.ctx[name]        else            return true, ngx.ctx[name]        end    end    -- destruct    -- @return boolean allok, set_keepalive result    self.destruct = function()        local allok = true        for name, _ in pairs(self.pool) do            local ok, msg = ngx.ctx[name].redis_instance:set_keepalive(                h.cosocket_pool.max_idel, h.cosocket_pool.size            )            if not ok then allok = false end         end        return allok    end    -- do construct    self.construct()             return selfendreturn redis_factory

redistest.lua

--平台公共的配置文件常量local config = require "testcode.config_constant"--redis连接池工厂local redis_factory = require('testcode.redis_factory')(config.redisConfig) -- import config when construct--获取redis的连接实例local ok, redis_a = redis_factory:spawn('redis_a')--用于接收前端数据的对象local args=nil--获取前端的请求方式 并获取传递的参数   local request_method = ngx.var.request_method--判断是get请求还是post请求并分别拿出相应的数据if"GET" == request_method then        args = ngx.req.get_uri_args()elseif "POST" == request_method then        ngx.req.read_body()        args = ngx.req.get_post_args()        --兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug        if (args == nil or args.data == null) then                args = ngx.req.get_uri_args()        endend--获取前端传递的keylocal key = args.key--在redis中获取key对应的值local va = redis_a:get(key)--响应前端ngx.say('{"key":"'..key..'","va":"'..va..'"}')

再次,修改nginx配置

 #user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #                  '$status $body_bytes_sent "$http_referer" '    #                  '"$http_user_agent" "$http_x_forwarded_for"';    #access_log  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location /redis {      default_type text/html;      #这里的lua文件的路径为绝对路径,请根据自己安装的实际路径填写      #记得斜杠是/这个,从window中拷贝出来的是\这样,这样是有问题的,务必注意      content_by_lua_file E:/study/openresty-1.17.8.2-win64/lualib/testcode/redistest.lua;    }            location / {            root   html;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}          }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

最后,运行nginx。

检查配置

dc060ee97ad837cc4308e322cba9bbed.png

运行:

ca799f41111ace70c8e9ecc3b45200c4.png

访问:

d6c927f45364da7f05959cff0842d10b.png

小帮助:如果你的浏览器不能浏览json,请安装插件。

6dab6c0b032a0733cf78b0f66c005b8b.png

到这里就玩了,写文章很辛苦,如果对您有用,欢迎打赏,然后分享一下下面的公众号,多谢。

797576881824b0d99eb55c79906c013d.png

版权声明:原创,侵权必究

投稿信箱:714343803@qq.com(欢迎您原创投稿)

责任编辑:牛郎弹琴  QQ:714343803

⊙网站: http://www.laomn.com

你若喜欢,别忘了点个【】和【哦 0ea45716c4d39c257367079f4581d2cd.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值