Lua在nginx环境中读取redis的demo(01)

    xiaoxiao2022-07-02  120

    我使用的环境是 openresty-1.15.8.1-win64 为运行环境;编辑器是 idea项目结构

    lua.conf : 

    server { listen 8033; server_name _; location /lua { default_type 'text/html'; content_by_lua_file testlua/TestSearch.lua; } }

    nginx.config: worker_processes  2; error_log  logs/error.log  info; events {   worker_connections  1024; } http {   default_type  application/octet-stream;   access_log  logs/access.log;   lua_package_path 'testlua/?.lua;;';   lua_package_cpath 'lualib/?.so;;';   include lua.conf;   server {     listen       12344;     server_name  localhost;     default_type text/html;     location = /favicon.ico {       log_not_found off;       access_log off;     }   } }

    TestSearch.lua 

    local function searchRedis() local redis = require("resty.redis") local json = require ("dkjson") --这个地方是我自己下载的包并且放到了lua.lib中和resty同级 ngx.header.content_type="application/json;charset=utf8" local mdn=ngx.var.arg_key local value=ngx.var.arg_value ngx.say(mdn,"",value) --创建实例 local redis_instance = redis:new() --设置超时(毫秒) redis_instance:set_timeout(3000) --建立连接 local host = "127.0.0.1" local port = 6379 local ok, err = redis_instance:connect(host, port) if not ok then ngx.say("connect to redis error : ", err) return close_redis(redis_instance) end if value ~="" then ngx.say("jinlailea") local resp, err = redis_instance:set(mdn,value); end local resp, err = redis_instance:eval("return redis.call('get', KEYS[1])", 1, mdn); ngx.say("value : ",resp) end searchRedis() local function close_redis(red) if not red then return end local ok, err = red:close() if not ok then ngx.say("close redis error : ", err) end end

    代码粘贴完毕 我的运行环境是nginx 在 idea File--》setting--》Plugins中搜索nginx support下载

    配置Ant Build 就是普通的xml文件,在项目下新建一个 **.xml就像是我整的bulid.xml 然后点击idea右侧Ant Build 就看到了

    这是发布的,发布之后,直接运行就好了

    访问连接 http://127.0.0.1:12344/

    出现上述页面,表示nginx+lua没有问题

    访问http://127.0.0.1:8033/lua?key=msg&value=MrWU,beautify

    bulid.xml文件:

    <?xml version="1.0" encoding="UTF-8"?> <project name="testlua" default="dist" basedir="."> <description> run pic-server </description> <property name="openresty-home" location="D:\bytueEnvironment\company\openresty-1.15.8.1-win64"/> <property name="conf" location="${basedir}/conf"/> <property name="src" location="${basedir}/src"/> <property name="target-conf" location="${openresty-home}/conf"/> <property name="target-src" location="${openresty-home}/${ant.project.name}"/> <target name="clean" depends=""> <delete dir="${target-conf}"/> <delete dir="${target-src}"/> <delete> <fileset dir="${openresty-home}/logs" includes="*.log"> </fileset> </delete> </target> <target name="init" depends="clean"> <mkdir dir="${target-conf}"/> <mkdir dir="${target-src}"/> </target> <target name="dist" depends="init" description="generate the distribution" > <copy todir="${target-conf}"> <fileset dir="${conf}"></fileset> </copy> <copy todir="${target-src}"> <fileset dir="${src}"></fileset> </copy> </target> </project>

    标识lua在ngx环境下可以对redis进行读写操作

    研究了好几天,看了无数网页,感谢无数网友

    最新回复(0)