Openresty最佳案例 | 第7篇: 模块开发、OpenResty连接Redis
转载请标明出处:
http://blog.csdn.net/forezp/article/details/78616714
本文出自方志朋的博客
Lua模块开发
在实际的开发过程中,不可能把所有的lua代码写在一个lua文件中,通常的做法将特定功能的放在一个lua文件中,即用lua模块开发。在lualib目录下,默认有以下的lua模块。
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
在使用这些模块之前,需要在nginx的配置文件nginx.conf中的http模块加上以下的配置:
lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块
现在来简单的开发一个lua模块:
vim /usr/example/lualib/module1.lua
在module1.lua文件加上以下的代码:
local count = 0
local function hello()
count = count + 1
ngx.say("count : ", count)
end
local _M = {
hello = hello
}
return _M
开发时将所有数据做成局部变量/局部函数;通过 _M导出要暴露的函数,实现模块化封装。
在/usr/example/lua目录下创建一个test_module_1.lua 文件,在该文件中引用上面的module1.lua文件。
vim /usr/example/lua/test_module_1.lua
加上以下代码:
local module1 = require("module1")
module1.hello()
通过require(“模块名”)来加载模块,如果是多级目录,则需要通过require(“目录1.目录2.模块名”)加载。
在/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;
}
多次在浏览器上访问:http://116.196.177.123/lua_module_1,浏览器显示:
count : 1
count : 2
count : 3
...
安装redis
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
启动redis:
nohup /usr/servers/redis-3.2.6/src/redis-server /usr/servers/redis-3.2.6/redis.conf &
查看是否启动:
ps -ef |grep redis
终端显示:
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
可见redis已经启动。
lua连接redis
lua_resty_redis模块地址: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 它是一个基于cosocket API的为ngx_lua模块提供Lua redis客户端的驱动。
创建一个test_redis_basic.lua文件
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 --毫秒
local pool_size = 100 --连接池大小
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)
上面的代码很简单,通过连接池连接Redis,连接上redis后,通过set一对键值对(msg,helloword)到redis中,然后get(msg),并通过ngx.say()返回给浏览器。
vim /usr/example/example.conf,添加以下的配置代码:
location /lua_redis_basic {
default_type 'text/html';
lua_code_cache on;
content_by_lua_file /usr/example/lua/test_redis_basic.lua;
}
浏览器访问:http://116.196.177.123/lua_redis_basic
浏览器显示:
msg : hello world
lua_resty_redis支持所有的redis指令,本身Redis就支持lua语言操作。所以lua_resty_redis模块能够提高所有的redis操作的功能。
在很多时候,Redis是设置了口令的,连接时,如果需要验证口令,需要添加 local res, err = red:auth(“foobared”),示例代码如下:
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
更多请关注的官方文档https://github.com/openresty/lua-resty-redis
和开涛的博客http://jinnianshilongnian.iteye.com/blog/2187328
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
Openresty最佳案例 | 第7篇: 模块开发、OpenResty连接Redis的更多相关文章
- Openresty最佳案例 | 第8篇:RBAC介绍、sql和redis模块工具类
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616738 本文出自方志朋的博客 RBAC介绍 RBAC(Role-Based Acce ...
- Openresty最佳案例 | 第5篇:http和C_json模块
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616672 本文出自方志朋的博客 http客户端 Openresty没有提供默认的Htt ...
- Openresty最佳案例 | 第4篇:OpenResty常见的api
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616660 本文出自方志朋的博客 获取请求参数 vim /usr/example/exa ...
- Openresty最佳案例 | 第2篇:Lua入门
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616622 本文出自方志朋的博客 什么是lua Lua 是一种轻量小巧的脚本语言,用标准 ...
- Openresty最佳案例 | 第9篇:Openresty实现的网关权限控制
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616779 本文出自方志朋的博客 简介 采用openresty 开发出的api网关有很多 ...
- Openresty最佳案例 | 第3篇:Openresty的安装
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616645 本文出自方志朋的博客 我的服务器为一台全新的centos 7的服务器,所以从 ...
- Openresty最佳案例 | 第1篇:Nginx介绍
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616591 本文出自方志朋的博客 Nginx 简介 Nginx是一个高性能的Web 服务 ...
- Openresty最佳案例 | 第6篇:OpenResty连接Mysql
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616698 本文出自方志朋的博客 centos 安装mysl Centos系统下安装my ...
- Openresty最佳案例 | 汇总
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616856 本文出自方志朋的博客 目录 Openresty最佳案例 | 第1篇:Ngin ...
随机推荐
- jquery获取元素与屏幕高度距离
a. onscroll事件 scroll是css样式中overflow的一个值,意思是显示滚动条;当一个元素的实际高度超过他的最大高度是,只要设置了overflow为scroll b. $(..).s ...
- Javascript模块化编程详解
在这篇文章中,我将会回顾一下js模块化编程的基础,并且将会讲到一些真的非常值得一提的进阶话题,包括一个我认为是我自创的模式. 模块化编程是一种非常常见Javascript编程模式.它一般来说可以使得代 ...
- [转].NET Core之Entity Framework Core 你如何创建 DbContext
本文转自:http://www.cnblogs.com/tdws/p/5874212.html 本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www. ...
- 如何给swing加上alt+x和ctrl+x快捷键
1.给菜单栏上的菜单alt+x快捷键非常简单: private JMenu helpInfo = new JMenu("帮助"); helpInfo.setMnemonic(Key ...
- centos7 安装jdk、Tomcat
1.安装jdk 下载jdk: 解压:tar -zxvf filename -C /usr/local/jdk8/ 配置环境变量: vim /etc/profile 添加如下内容:JAVA_HOME根据 ...
- Java笔记之Scanner先读取一个数字,在读取一行字符串方法分析
问题:大家在学习Java读取数据的时候一般都是使用Scanner方法读取数据,但是其中有一个小问题大家可能不知道, 就是我们在使用scanner的时候如果你先读取一个数字,在读取一行带有空格的字符串, ...
- My first python application
''' Authon:WSE_Gordon This application is use for the costomer to login the application. The Costome ...
- java 判断图片的类型
// 这种方法如果遇到不是图片类型的文件会reader抛异常! public static void main(String[] args) throws IOException { // get i ...
- css rgba/hsla知识点讲解及半透明边框
一.RGBA(R,G,B,A) 参数: R:红色值.正整数 | |百分数 G:绿色值.正整数 | |百分数 B:蓝色值.正整数 | |百分数 A:Alpha透明度.取值0~1之间. 说明:此色彩模式与 ...
- C语言字符数组与字符串
研究几个案例: 输出图案: #include <stdio.h> void main() { ][] = { {', ' ', ' '}, {', ' '}, {'}, {', ' '}, ...