Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize module development and connect Redis in Openresty

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to implement module development and connect to Redis in Openresty. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Lua module development

In the actual development process, it is impossible to write all the lua code in a lua file, the usual practice is to put specific functions in a lua file, that is, using lua module development. In the lualib directory, the following lua modules are available by default.

Lualib/ ├── cjson.so ├── ngx │ ├── balancer.lua │ ├── ocsp.lua │ ├── re.lua │ ├── semaphore.lua │ ├── ssl │ │ └── session.lua │ └── ssl.lua ├── rds │ parser.so redis parser.so resty aes.lua core │ ├── base64.lua │ ├── base.lua │ ├── ctx.lua │ ├── exit.lua │ ├── hash.lua │ ├── misc.lua │ ├── regex.lua │ ├── request.lua │ ├── response.lua │ shdict.lua time. Lua │ ├── uri.lua │ ├── var.lua │ └── worker.lua ├── core.lua ├── dns │ └── resolver.lua ├── limit │ ├── conn.lua │ ├── req.lua │ └── traffic.lua ├── lock.lua ├── lrucache Pureffi.lua ├── lrucache.lua ├── md5.lua ├── memcached.lua ├── mysql.lua ├── random.lua ├── redis.lua ├── sha1.lua ├── sha224.lua ├── sha256.lua ├── sha384.lua ├── sha512.lua ├── sha.lua ├── string.lua ├── upload.lua ├── Upstream │ └── healthcheck.lua └── websocket ├── client.lua ├── protocol.lua └── server.lua

Before using these modules, you need to add the following configuration to the http module in nginx's configuration file nginx.conf:

Lua_package_path "/ usr/example/lualib/?.lua;;"; # lua module lua_package_cpath "/ usr/example/lualib/?.so;;"; # c module

Now let's simply develop a lua module:

Vim / usr/example/lualib/module1.lua

Add the following code to the module1.lua file:

Local count = 0 local function hello () count = count + 1 ngx.say ("count:", count) end local _ M = {hello = hello} return _ M

When developing, make all the data into local variables / local functions; export the functions to be exposed through _ M to achieve modular encapsulation.

Create a test_module_1.lua file in the / usr/example/lua directory and reference the above module1.lua file in that file.

Vim / usr/example/lua/test_module_1.lua

Add the following code:

Local module1 = require ("module1") module1.hello ()

Load the module through require ("module name"), or if it is a multi-level directory, you need to load it through require ("directory 1. Directory 2. Module name").

Add the following configuration to / user/example/example.conf:

Location / lua_module_1 {default_type 'text/html'; lua_code_cache on; content_by_lua_file / usr/example/lua/test_module_1.lua;}

Multiple visits on the browser: http://116.196.177.123/lua_module_1, the browser shows:

Count: 1count: 2count: 3... Install redis

Install under linux:

Cd / usr/servers

$wget http://download.redis.io/releases/redis-3.2.6.tar.gz$ tar xzf redis-3.2.6.tar.gz$ cd redis-3.2.6$ make

Start redis:

Nohup / usr/servers/redis-3.2.6/src/redis-server / usr/servers/redis-3.2.6/redis.conf &

Check to see if it starts:

Ps-ef | grep redis

Terminal display:

Root 20985 14268 0 18:49 pts/0 00:00:00 / usr/servers/redis-3.2.6/src/redis-server 127.0.0.1:6379

It can be seen that redis has been started.

Lua connection redis

Lua_resty_redis module address: https://github.com/openresty/lua-resty-redis

Lua-resty-redis-Lua redis client driver for the ngx_lua based on the cosocket API

Lua_resty_redis it is a cosocket API-based driver that provides Lua redis clients for ngx_lua modules.

Create a test_redis_basic.lua file

Vim / usr/example/lua/test_redis_basic.lua

Local function close_redis (red) if not red then return end local pool_max_idle_time = 10000-millisecond local pool_size = 100-connection pool size local ok, err = red:set_keepalive (pool_max_idle_time, pool_size) if not ok then ngx.say ("set keepalive error:" Err) end end local redis = require ("resty.redis") local red = redis:new () red:set_timeout (1000) local ip = "127.0.0.1" local port = 6379 local ok, err = red:connect (ip, port) if not ok then ngx.say ("connect to redis error:", err) return close_redis (red) end ok, err = red:set ("msg" "hello world") if not ok then ngx.say ("set msg error:", err) return close_redis (red) end local resp, err = red:get ("msg") if not resp then ngx.say ("get msg error:", err) return close_redis (red) end if resp = ngx.null then resp =''end ngx.say ("msg:", resp) close_redis (red)

The above code is simple: connect the Redis through the connection pool, connect to the redis, pass a pair of keys (msg,helloword) to the redis through the set, then get (msg), and return to the browser through ngx.say ().

Vim / usr/example/example.conf, add the following configuration code:

Location / lua_redis_basic {default_type 'text/html'; lua_code_cache on; content_by_lua_file / usr/example/lua/test_redis_basic.lua;}

Browser access: http://116.196.177.123/lua_redis_basic

The browser displays:

Msg: hello world

Lua_resty_redis supports all redis instructions, and Redis itself supports lua language operations. So the lua_resty_redis module can improve the function of all redis operations.

In many cases, the password is set for Redis. If you need to verify the password when connecting, you need to add local res, err = red:auth ("foobared"). The sample code is as follows:

Local redis = require "resty.redis" local red = redis:new () red:set_timeout (1000)-1 sec local ok, err = red:connect ("127.0.0.1", 6379) if not ok then ngx.say ("failed to connect:", err) return end local res, err = red:auth ("foobared") if not res then ngx.say ("failed to authenticate:" Err) return end, thank you for reading! On "how to achieve module development in Openresty and connect Redis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report