Lua
Lua 入门
Lua 环境安装
Lua 基本语法
Nginx添加Lua扩展模块
编译安装Nginx
安装前准备
对于nginx编译安装需要先安装编译 的工具,然后再安装nginx依赖
yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
下载nginx
#获取nginx,官方地址
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#解压(/root/lua/nginx-1.12.2)
tar -zxvf nginx-1.12.2.tar.gz
安装
第一步是配置,第二步是编译安装
配置 nginx
#进入到nginx 解压目录
cd nginx-1.12.2
#配置nginx
#--prefix 指定安装的目录
#/usr/local/nginx 是安装目录,不能和自己下载的文件目录重了
#./configure --prefix=/usr/local/nginx
#带ssl stub_status模块 添加strem模块 –with-stream,这样就能传输tcp协议了
#http_stub_status_module 状态监控
#http_ssl_module 配置https
#stream 配置tcp得转发
#http_gzip_static_module 压缩
#http_sub_module 替换请求
./configure \
--prefix=/usr/local/nginx --with-http_stub_status_module \
--with-http_ssl_module --with-stream -\
-with-http_gzip_static_module --with-http_sub_module
编译安装
#编译安装
make && make install
验证
安装成功后,会在./configure --prefix=/usr/local/nginx,指定的目录/usr/local/nginx创建4个 文件夹。具体功能下面有介绍。
#启动 nginx服务
/usr/local/nginx/sbin/nginx
#停止服务
/usr/local/nginx/sbin/nginx -s stop
#重启服务
/usr/local/nginx/sbin/nginx -s reload
#查看启动情况
ps -ef|grep nginx
#查看是否启动成功(ip需要替换为自己的服务器ip)
curl 192.168.1.100
#查看端口情况
netstat -ano|grep 80
#查看nginx版本
./sbin/nginx -V
二、 Nginx 添加lua模块
安装LuaJit、ngx_devel_kit (NDK)、ngx_lua
1、编译安装LuaJit
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar xzvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit
2、下载 ngx_devel_kit (NDK)
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
tar xzvf v0.3.0.tar.gz
3、下载 ngx_lua
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.11.tar.gz
tar xvf v0.10.11.tar.gz
4、复制ngx_devel_kit (NDK)和ngx_lua解压文件
cp -r lua-nginx-module-0.10.11/ ngx_devel_kit-0.3.0/ /root/lua/nginx-1.12.2/lua/
查看之前编译参数
./nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module
集成添加lua
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0# 进入nginx源码解压目录中执行重新编译
./configure \
--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-http_gzip_static_module --with-http_sub_module --add-module=/root/lua/nginx-1.12.2/lua/ngx_devel_kit-0.3.0/ --add-module=/root/lua/nginx-1.12.2/lua/lua-nginx-module-0.10.11 --with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib"
# 编译(如果是第一次安装nginx需要执行make && make install,不需要在执行备份)
make
# 备份
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
/usr/local/nginx/sbin/nginx –s stop
cp objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
# 检查模块是否添加上
nginx -V
三、 测试Lua
# 编辑nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location /lua {
set $test "hello,world";
content_by_lua '
ngx.header.content_type="text/plain"
ngx.say(ngx.var.test)';
}
}
四、 Nginx调用lua模块指令
Nginx的可插拔模块化加载执行,共11个处理阶段
set_by_lua 设置nginx变量,可以实现复杂的赋值逻辑
set_by_lua_file
access_by_lua 请求访问阶段处理,用于访问控制
access_by_lua_file
content_by_lua 内容处理器,接收请求处理并输出响应
content_by_lua_file
ngx.var nginx变量
ngx.req.get headers 获取请求头
ngx.req.get_uri_args 获取url请求参数
ngx.redirect 重定向
ngx.print 输出响应内容体
ngx.say 通ngx.print,但是会最后输出一个换行符
ngx.header 输出响应头
Nginx添加Lua扩展模块
– 参考文件 Nginx添加Lua扩展模块.docx
Nginx+Lua+Redis高性能后端接口
Lua没有中央仓库的概念 ->openrestry
一、 需求分析
类似网易云课堂首页的广告数据显示:
二、 广告缓存架构概述
流程:
- 查询Ngin缓存,如果有缓存则直接将缓存中的广告数据返回
- 如果Nginx缓存中没有广告数据,则通过Lua脚本查询Redis,如果Redis中有数据,则将数据存入到Nginx的缓存并返回查询到广告数据。
- 如果Redis中没有缓存数据,则此时会通过Lua脚本查询MySQL,如果MySQL有数据,则将数据存入到Redis中并返回查询到数据。
三、 广告表结构
广告分类表
CREATE TABLE tb_content_category
(
id
bigint NOT NULL AUTO_INCREMENT,
name
varchar(50) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
广告表
CREATE TABLE tb_content
(
id
bigint NOT NULL AUTO_INCREMENT,
category_id
bigint NOT NULL,
title
varchar(200) DEFAULT NULL,
url
varchar(500) DEFAULT NULL,
pic
varchar(300) DEFAULT NULL,
status
varchar(1) DEFAULT NULL,
sort_order
int DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
四、 代码实现
缓存预热
缓存预热就是将数据提前加入到缓存中,当数据发生变更,再将最新的数据更新到缓存。
实现思路
定义请求:用于查询MySQL数据库中的数据更新到redis中。
连接Mysql 按照广告分类ID读取广告列表,转换为json字符串。
连接Redis将广告列表json字符串存入redis 。
实现步骤
定义请求:请求地址:/content_update 请求参数:id – 广告分类id 返回值:json
在/root/lua目录下创建content_update.lua 实现连接Mysql 查询数据 并存储到Redis中。
content_update.lua
ngx.header.content_type=“application/json;charset=utf-8”;
local id= ngx.req.get_uri_args()[“id”];–提取参数
local cjson= require(“cjson”); --加载cjson模块
local mysql= require(“resty.mysql”);–加载mysql模块
local redis= require(“resty.redis”);–加载redis模块
–读取mysql中的数据
local db= mysql:new();
local props = {
host = “192.168.1.101”,
port = 3306,
database =“mybatis”,
user =“root”,
password =“root”
}
db:connect(props);
local select_sql=“select pic,url from tb_content where status = ‘1’ and category_id=”…id…" order by sort_orde
r ";
local res=db:query(select_sql);
db:close();
–写入redis
local red= redis:new();
red:connect(“192.168.1.100”,6379);
red:set(“content_”…id,cjson.encode(res));
red:close();
ngx.say("{falg:true,code:20000}");
修改 /usr/local/openresty/nginx/conf/nginx.conf 文件
location /content_update {
content_by_lua_file /root/lua/content_update.lua;
}
重新启动nginx并测试:http://192.168.1.102/content_update?id=1
广告缓存读取
实现思路
定义请求:用户根据广告分类的ID 获取广告的列表
从Nginx缓存获取,获取到直接返回广告数据,否则执行下一步
从Redis中获取广告数据,获取到则将广告数据存入Nginx缓存中
实现步骤
定义请求:请求地址:/content_read 请求参数:id – 广告分类id 返回值:json
在/root/lua目录下创建content_read.lua 实现查询nginx缓存数据
content_read.lua
ngx.header.content_type=“application/json;charset=utf-8”;
–提取参数:广告分类id
local id= ngx.req.get_uri_args()[“id”];
–加载本地缓存模块
local cache_ngx= ngx.shared.dis_cache;
–从本地缓存获取广告数据
local content_cache = cache_ngx:get(“content_”…id);
–如果本地缓存没有数据
if content_cache =="" or content_cache == nil then
–引入redis模块
local redis= require(“resty.redis”);
–实例化redis对象
local red = redis:new();
–建立连接
red:connect(“192.168.33.133”,6379);
–从redis取值
local data= red:get(“content_”…id);
–关闭连接
red:close();
ngx.say(data);
–存入本地缓存
cache_ngx:set(“content_”…id , data);
else
ngx.say(content_cache);
end
修改 /usr/local/openresty/nginx/conf/nginx.conf 文件
定义lua缓存命名空间(在http几点下)
lua_shared_dict dis_cache 128m;
location /content_read {
content_by_lua_file /root/lua/content_read.lua;
}
重新启动nginx并测试:http://192.168.1.102/content_read?id=1