Openresty(Lua+Nginx)实践
简介:
OpenResty(也称为 ngx_openresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。
OpenResty 致力于将你的服务器端应用完全运行于 Nginx 服务器中,充分利用 Nginx 的事件模型来进行非阻塞 I/O 通信。不仅仅是和 HTTP 客户端间的网络通信是非阻塞的,与MySQL、PostgreSQL、Memcached 以及 Redis 等众多远方后端之间的网络通信也是非阻塞的。
————章亦春
Lua基础数据类型
Lua表达式
Lua控制结构
Lua函数
函数调用的时候可以传参,形参(a,b)与实参(x,y)在使用上需要注意,在使用函数时实参若大于形参则多余的参数为nli(空)
Local function test(a,b) local temp = a
a = b
b = temp
print(a,b)
end
local x = "hello"
local y = 20
print(x, y)
swap(x, y) --调用swap函数
print(x, y) --调用swap函数后,x和y的值并没有交换 -->output
hello 20
20 hello
hello 20
Openrsty连接redis连接池封装(connect,port)+auth 使用
--这是大神们封装好的redis连接池,在配置中修改ip和端口即可 文件放到 Openresty/lualib/resty/redis_iresty.lua local redis_c = require "resty.redis" local ok, new_tab = pcall(require, "table.new")
if not ok or type(new_tab) ~= "function" then
new_tab = function (narr, nrec) return {} end
end local _M = new_tab(0, 155)
_M._VERSION = '0.01' local commands = {
"append", "auth", "bgrewriteaof",
"bgsave", "bitcount", "bitop",
"blpop", "brpop",
"brpoplpush", "client", "config",
"dbsize",
"debug", "decr", "decrby",
"del", "discard", "dump",
"echo",
"eval", "exec", "exists",
"expire", "expireat", "flushall",
"flushdb", "get", "getbit",
"getrange", "getset", "hdel",
"hexists", "hget", "hgetall",
"hincrby", "hincrbyfloat", "hkeys",
"hlen",
"hmget", "hmset", "hscan",
"hset",
"hsetnx", "hvals", "incr",
"incrby", "incrbyfloat", "info",
"keys",
"lastsave", "lindex", "linsert",
"llen", "lpop", "lpush",
"lpushx", "lrange", "lrem",
"lset", "ltrim", "mget",
"migrate",
"monitor", "move", "mset",
"msetnx", "multi", "object",
"persist", "pexpire", "pexpireat",
"ping", "psetex", "psubscribe",
"pttl",
"publish", --[[ "punsubscribe", ]] "pubsub",
"quit",
"randomkey", "rename", "renamenx",
"restore",
"rpop", "rpoplpush", "rpush",
"rpushx", "sadd", "save",
"scan", "scard", "script",
"sdiff", "sdiffstore",
"select", "set", "setbit",
"setex", "setnx", "setrange",
"shutdown", "sinter", "sinterstore",
"sismember", "slaveof", "slowlog",
"smembers", "smove", "sort",
"spop", "srandmember", "srem",
"sscan",
"strlen", --[[ "subscribe", ]] "sunion",
"sunionstore", "sync", "time",
"ttl",
"type", --[[ "unsubscribe", ]] "unwatch",
"watch", "zadd", "zcard",
"zcount", "zincrby", "zinterstore",
"zrange", "zrangebyscore", "zrank",
"zrem", "zremrangebyrank", "zremrangebyscore",
"zrevrange", "zrevrangebyscore", "zrevrank",
"zscan",
"zscore", "zunionstore", "evalsha"
} local mt = { __index = _M } local function is_redis_null( res )
if type(res) == "table" then
for k,v in pairs(res) do
if v ~= ngx.null then
return false
end
end
return true
elseif res == ngx.null then
return true
elseif res == nil then
return true
end return false
end -- change connect address as you need
function _M.connect_mod( self, redis )
redis:set_timeout(self.timeout)
return redis:connect("1.1.1.1", 6379)
end function _M.set_keepalive_mod( redis )
-- put it into the connection pool of size 100, with 60 seconds max idle time
return redis:set_keepalive(60000, 1000)
end function _M.init_pipeline( self )
self._reqs = {}
end function _M.commit_pipeline( self )
local reqs = self._reqs if nil == reqs or 0 == #reqs then
return {}, "no pipeline"
else
self._reqs = nil
end local redis, err = redis_c:new()
if not redis then
return nil, err
end local ok, err = self:connect_mod(redis)
if not ok then
return {}, err
end redis:init_pipeline()
for _, vals in ipairs(reqs) do
local fun = redis[vals[1]]
table.remove(vals , 1) fun(redis, unpack(vals))
end local results, err = redis:commit_pipeline()
if not results or err then
return {}, err
end if is_redis_null(results) then
results = {}
ngx.log(ngx.WARN, "is null")
end
-- table.remove (results , 1) self.set_keepalive_mod(redis) for i,value in ipairs(results) do
if is_redis_null(value) then
results[i] = nil
end
end return results, err
end function _M.subscribe( self, channel )
local redis, err = redis_c:new()
if not redis then
return nil, err
end local ok, err = self:connect_mod(redis)
if not ok or err then
return nil, err
end local res, err = redis:subscribe(channel)
if not res then
return nil, err
end res, err = redis:read_reply()
if not res then
return nil, err
end redis:unsubscribe(channel)
self.set_keepalive_mod(redis) return res, err
end local function do_command(self, cmd, ... )
if self._reqs then
table.insert(self._reqs, {cmd, ...})
return
end local redis, err = redis_c:new()
if not redis then
return nil, err
end local ok, err = self:connect_mod(redis)
if not ok or err then
return nil, err
end local fun = redis[cmd]
local result, err = fun(redis, ...)
if not result or err then
-- ngx.log(ngx.ERR, "pipeline result:", result, " err:", err)
return nil, err
end if is_redis_null(result) then
result = nil
end self.set_keepalive_mod(redis) return result, err
end for i = 1, #commands do
local cmd = commands[i]
_M[cmd] =
function (self, ...)
return do_command(self, cmd, ...)
end
end function _M.new(self, opts)
opts = opts or {}
local timeout = (opts.timeout and opts.timeout * 1000) or 1000
local db_index= opts.db_index or 0 return setmetatable({
timeout = timeout,
db_index = db_index,
_reqs = nil }, mt)
end return _M
我的调用,生成uuid后存储到redis里 :
ngx.req.read_body()
local args= ngx.req.get_uri_args()
for k,v in pairs(args) do
local mac = v
ngx.say(mac)
end function guid()
local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
local tb={}
for i=1,32 do
table.insert(tb,seed[math.random(1,16)])
end
local sid=table.concat(tb)
return string.format('%s-%s-%s-%s-%s',
string.sub(sid,1,8),
string.sub(sid,9,12),
string.sub(sid,13,16),
string.sub(sid,17,20),
string.sub(sid,21,32)
)
end --print('execute_time='..tostring(os.clock()-start_time))
local redis = require "resty.redis_iresty"
local red = redis:new()
redis:auth("password") local s=0
--local start_time=os.clock()
while s<50 do
s=s+1
local uuid = guid()
local ok, err = redis:hset("lbs",uuid,"1")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say(s,uuid)
end
认为Openresty完全可以替代nginx
Openresty(Lua+Nginx)实践的更多相关文章
- 【原创】大叔经验分享(77)openresty(nginx+lua)发http请求
openresty(nginx+lua)发http请求 利用location+proxy_pass间接实现 location ^~ /test/http { internal; proxy_pass ...
- openresty(nginx+lua)初识
1.新增项目配置文件: vim /usr/example/example1.conf --将以下内容加入example1.conf server { listen 80; server_name _; ...
- openresty+lua在反向代理服务中的玩法
openresty+lua在反向代理服务中的玩法 phith0n · 2015/06/02 10:35 0x01 起因 几天前学弟给我介绍他用nginx搭建的反代,代理了谷歌和维基百科. 由此我想到了 ...
- openresty+lua+kafka方案与Tomcat接口并发度对比分析
1.openresty+lua+kafka 1.1 openresty+lua+kafka方案 之前的项目基于nginx反向代理后转发到Tomcat的API接口进行业务处理,然后将json数据打入ka ...
- Openresty+Lua+Kafka实现日志实时采集
简介 在很多数据采集场景下,Flume作为一个高性能采集日志的工具,相信大家都知道它.许多人想起Flume这个组件能联想到的大多数都是Flume跟Kafka相结合进行日志的采集,这种方案有很多他的优点 ...
- Openresty Lua协程调度机制
写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...
- OpenResty Lua钩子调用完整流程
前面一篇文章介绍了Openresty Lua协程调度机制,主要关心的是核心调度函数run_thread()内部发生的事情,而对于外部的事情我们并没有涉及.本篇作为其姊妹篇,准备补上剩余的部分.本篇将通 ...
- openresty + lua 1、openresty 连接 mysql,实现 crud
最近开发一个项目,公司使用的是 openresty + lua,所以就研究了 openresty + lua.介绍的话,我就不多说了,网上太多了. 写这个博客主要是记录一下,在学习的过程中遇到的一些坑 ...
- 用lua nginx module搭建一个二维码
用lua nginx module搭建一个二维码(qr code)生成器 作者 vinoca 發布於 2014年10月31日 如果有VPS,或者开源的路由器,安装一个nginx,添加lua-nginx ...
- linux环境中,openssl升级及openresty中nginx基于新版本openssl重新编译
需求说明: 最近在对系统进行安全扫描的时候,出现了openssl版本的问题,建议对openssl版本进行升级,在此记录下升级过程. 环境说明: 操作系统:RHEL 6.6 升级操作过程: 1.下载最新 ...
随机推荐
- 从CMDB查询云平台组件或者IP简单脚本
#!/bin/bash#author xiaoweige#todo: ip -- > ingredient or ingredient -- > ip #todo: get the ip ...
- 你知道CAN/RS-485总线为什么要隔离吗?
您在使用CAN或RS-485总线进行调试时,是否遇到过偶尔通信出错?或者接收不到数据?一直正常使用的总线,突然出现大范围的错误,或者节点损坏?您还在为这些问题不知所措,摸不着头脑吗?使用总线隔离,或许 ...
- 【转】枚举enum学习小记
原帖: http://hi.baidu.com/yuleishou/item/caacae872190031ec216272f 表示在vs2008下实验了一下,有些东西和原帖的还是不一样的,都贴在这里 ...
- JavaEE笔记(二)
查询load()和get()的区别 # 以下查询都是根据id查询 // Load和Get都会在第一次查询的是创建一个一级缓存查询语句 // 下一次查询的时候从缓存中查询是否有缓存的语句 // 如果有只 ...
- Noip前的大抱佛脚----考场配置
(global-linum-mode t) (global-set-key (kbd "RET") 'newline-and-indent) (setq default-tab-w ...
- VC编译连接选项详解
VC编译连接选项详解 大家可能一直在用VC开发软件,但是对于这个编译器却未必很了解.原因是多方面的.大多数情况下,我们只停留在“使用”它,而不会想去“了解”它.因为它只是一个工具,我们宁可把更多的精力 ...
- Kubernetes学习之路(十五)之Ingress和Ingress Controller
目录 一.什么是Ingress? 1.Pod 漂移问题 2.端口管理问题 3.域名分配及动态更新问题 二.如何创建Ingress资源 三.Ingress资源类型 1.单Service资源型Ingres ...
- [PLC]ST语言三:OUT/OUT_T/OUT_C/OUT_C-C32
一:OUT/OUT_T/OUT_C/OUT_C-C32 说明:简单的顺控指令不做其他说明. 控制要求:无 编程梯形图: 结构化编程ST语言: (*OUT(EN,D);*) ...
- PHP 用户密码加密函数password_hash
传统的用户名和密码都采用加盐的方式存储加密信息,盐值也需要存储. 自PHP5.5.0之后,新增加了密码散列算法函数(password_hash),password_hash() 使用足够强度的单向散列 ...
- 树莓派3b添加python时间同步脚本
树莓派没有电池,因此断电后系统时间会停止,直到你开机后又继续计时,所以会造成系统时间和实际时间有很大的误差. 因为项目需要用到本地时间,精度要求不高不想折腾(如果需要高精度,需要安装ntp),所以考虑 ...