1,构建前准备
名称 | 说明 | 地址 |
openresty-1.17.8.1.tar.gz | openresty 安装包 | OpenResty® - 中文官方站 |
ngx_cache_purge-2.3.tar.gz | 清理nginx缓存模块(不用可不装) | FRiCKLE Labs / nginx / ngx_cache_purge |
lua-resty-redis-cluster-master.zip | rediscluster模块,redis集群使用 | GitHub - cuiweixie/lua-resty-redis-cluster: a openresty redis cluster client |
2,编写Dockerfile文件
FROM centos
ADD openresty-1.17.8.1.tar.gz /
ADD ngx_cache_purge-2.3.tar.gz /
ADD lua-resty-redis-cluster/ /lua-resty-redis-cluster
RUN yum -y install perl gcc make pcre-devel openssl openssl-devel
RUN cd /openresty-1.17.8.1 && ./configure --prefix=/opt/openresty \
--with-luajit \
--add-module=/ngx_cache_purge-2.3 \
--without-http_redis2_module && make && make install && \
ln -sf /dev/stdout /opt/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /opt/openresty/nginx/logs/error.log
RUN cp /opt/openresty/luajit/include/luajit-2.1/*.h /usr/include/ && cd /lua-resty-redis-cluster && make && make install && cp /lua-resty-redis-cluster/lib/resty/rediscluster.lua /opt/openresty/lualib/resty/rediscluster.lua && cd lib && gcc redis_slot.c -fPIC -shared -o /opt/openresty/site/lualib/libredis_slot.so
ENV LUA_PATH=/lua/?.lua;;
WORKDIR /opt/openresty/nginx/sbin
ENTRYPOINT ["/opt/openresty/nginx/sbin/nginx","-c","/opt/openresty/nginx/conf/nginx.conf","-g","daemon off;"]
说明:
FROM centos
使用centos镜像
ADD openresty-1.17.8.1.tar.gz /
ADD ngx_cache_purge-2.3.tar.gz /
ADD lua-resty-redis-cluster/ /lua-resty-redis-cluster将openresty安装包,nginx缓存清理模块,redis集群源码文件夹添加到容器内
RUN yum -y install perl gcc make pcre-devel openssl openssl-devel
安装openresty依赖
RUN cd /openresty-1.17.8.1 && ./configure --prefix=/opt/openresty \
--with-luajit \
--add-module=/ngx_cache_purge-2.3 \
--without-http_redis2_module && make && make install && \
ln -sf /dev/stdout /opt/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /opt/openresty/nginx/logs/error.logcd /openresty-1.17.8.1 打开安装目录
--prefix: 安装地址
--add-module=/ngx_cache_purge-2.3 安装nginx缓存清理模块,不用可缓存清理可不写
--without-http_redis2_module redis模块
make && make install 编译并安装
ln -sf /dev/stdout /opt/openresty/nginx/logs/access.log && \
ln -sf /dev/stderr /opt/openresty/nginx/logs/error.log nginx输出日志到docker容器日志文件
RUN cp /opt/openresty/luajit/include/luajit-2.1/*.h /usr/include/ && cd /lua-resty-redis-cluster && make && make install && cp /lua-resty-redis-cluster/lib/resty/rediscluster.lua /opt/openresty/lualib/resty/rediscluster.lua && cd lib && gcc redis_slot.c -fPIC -shared -o /opt/openresty/site/lualib/libredis_slot.so
cp /opt/openresty/luajit/include/luajit-2.1/*.h /usr/include/ 解决这个报错
ENV LUA_PATH=/lua/?.lua;; lua脚本存放地址
WORKDIR /opt/openresty/nginx/sbin
ENTRYPOINT ["/opt/openresty/nginx/sbin/nginx","-c","/opt/openresty/nginx/conf/nginx.conf","-g","daemon off;"]
设置工作地址,以及启动openresty
mysql lua工具类
--MySQL查询操作,封装成一个模块
--Java操作MySqL
--导入依赖包
local mysql = require "resty.mysql"
--配置数据源链接
local props = {
host = "192.168.18.200",
port = 3306,
database = "demo",
user = "root",
password = "123456"
}
--创建一个对象
local mysqldb = {}
--查询数据库
function mysqldb.query(sql)
--创建链接
local db = mysql:new()
--设置超时时间
db:set_timeout(10000)
db:connect(props)
--配置编码格式
db:query("SET NAMES utf8")
--查询数据库 "select * from activity_info where id=1"
local result = db:query(sql)
--关闭链接
db:close()
--返回结果集
return result
end
return mysqldb
redisclster工具类
--操作Redis集群,封装成一个模块
--引入依赖库
local redis_cluster = require "resty.rediscluster"
--配置Redis集群链接信息
local config = {
name = "test",
serv_list = {
{ip="192.168.18.204", port = 7001},
{ip="192.168.18.204", port = 7002},
{ip="192.168.18.204", port = 7003},
{ip="192.168.18.204", port = 7004},
{ip="192.168.18.204", port = 7005},
{ip="192.168.18.204", port = 7006},
},
idle_timeout = 1000,
pool_size = 10000,
}
--定义一个对象
local lredis = {}
--创建set()添加数据方法
function lredis.set(key,value)
--1)打开链接
local red = redis_cluster:new(config)
red:init_pipeline()
--2)执行命令【set】
red:set(key,value)
red:commit_pipeline()
--3)关闭链接
red:close()
end
--创建查询数据get()
function lredis.get(key)
--1)打开链接
local red = redis_cluster:new(config)
red:init_pipeline()
--2)执行命令【set】
red:get(key)
local result = red:commit_pipeline()
--3)关闭链接
red:close()
--4)返回结果集
return result
end
return lredis
nginx 配置
pcre_jit on;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 代理缓存配置
proxy_cache_path /data/cache levels=1:2 keys_zone=openresty_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost;
location /info {
#启用缓存openresty_cache
proxy_cache openresty_cache;
##针对指定请求缓存
##proxy_cache_methods GET;
##设置指定请求会缓存
proxy_cache_valid 200 304 10s;
##最少请求1次才会缓存
proxy_cache_min_uses 3;
##如果并发请求,只有第1个请求会去服务器获取数据
##proxy_cache_lock on;
##唯一的key
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://192.168.18.142:8089;
}
location ~ /purge(/.*) {
# 清理缓存
proxy_cache_purge openresty_cache $host$1$is_args$args;
}
location /lua {
#default_type text/plain;
content_by_lua_file /lua/main.lua;
}
}
client_body_temp_path /data/openresty/nginx-client-body;
proxy_temp_path /data/openresty/nginx-proxy;
fastcgi_temp_path /data/openresty/nginx-fastcgi;
uwsgi_temp_path /data/openresty/nginx-uwsgi;
scgi_temp_path /data/openresty/nginx-scgi;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
nginx配置中/lua/main.lua代码
ngx.header.content_type="application/json;charset=utf8"
-- 引入工具类
local cjson = require("cjson")
local mysql = require("mysql")
local lrredis = require("redis")
-- redis存储 get为读取
lrredis.set("k","v")
-- mysql读取查询
local rel = mysql.query("select * from shop")
-- json返回
ngx.say(cjson.encode(rel))
docker容器启动命令
docker run -d -p 889:80 -v /home/openrestry/lua/:/lua -v /home/openrestry/nginx.conf:/opt/openresty/nginx/conf/nginx.conf -v /home/openrestry/data:/data --name opentry2 openresty:t