关于openresty的全局初始化问题

604 views
Skip to first unread message

hxj...@gmail.com

unread,
Mar 9, 2015, 2:32:40 AM3/9/15
to open...@googlegroups.com
Hi
   我想对于应用做一些全局性的初始化,例如 对redis 执行一个lua脚本的 script load  命令
   这样的初始化只做一次就好了,我需要在什么地方处理呢?

   init_by_lua init_by_lua_file  都是针对单次 http req 请求的吧?

Lance

unread,
Mar 9, 2015, 3:52:06 AM3/9/15
to open...@googlegroups.com

Lance

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Yichun Zhang (agentzh)

unread,
Mar 9, 2015, 4:04:43 PM3/9/15
to openresty
Hello!

2015-03-08 23:32 GMT-07:00 hxj137:
> 我想对于应用做一些全局性的初始化,例如 对redis 执行一个lua脚本的 script load 命令
> 这样的初始化只做一次就好了,我需要在什么地方处理呢?
>

逻辑上最合适的地方是在 init_by_lua 这样的上下文中。但这个上下文目前尚不允许
cosocket,所以你需要其他的折衷做法,比如放在你的 nginx init 脚本中,或者在 init_by_lua
中直接使用第三方的使用阻塞 IO 的 redis 客户端 Lua 库。

> init_by_lua init_by_lua_file 都是针对单次 http req 请求的吧?
>

当然不是。请仔细阅读文档:

https://github.com/openresty/lua-nginx-module#init_by_lua

Regards,
-agentzh

hxj...@gmail.com

unread,
Mar 9, 2015, 10:47:11 PM3/9/15
to open...@googlegroups.com
感谢回复, 是的 最近刚开始接触到openresty,所以文档读的不仔细,我会去好好读一下
目前了解到 通过ngx.timer.at 来执行的方式 由于context 的原因 不能执行在init_by_lua

我错误的认为 init_by_lua 是针对单次http req 的,是因为 我在 init_by_lua 中定义了一个全局变量 
例如 varstatic = 3

然后在某个请求中
content_by_lua '
varstatic varstatic +1
ngx.say("varstatic ",varstatic )
';

然后我怎么刷新  值都会是4
我以前没有接触过lua 也许是很简单的问题造成的,或者是缓存?  造成的我对 init_by_lua 的错误理解
那么 这是为什么呢?

在 2015年3月10日星期二 UTC+8上午4:04:43,agentzh写道:

hxj...@gmail.com

unread,
Mar 9, 2015, 11:25:22 PM3/9/15
to open...@googlegroups.com

init_worker_by_lua  会执行很多次吧 对于 worker 的启动来说


在 2015年3月9日星期一 UTC+8下午3:52:06,Lance Li写道:

Yichun Zhang (agentzh)

unread,
Mar 10, 2015, 2:36:48 AM3/10/15
to openresty
Hello!

2015-03-09 19:47 GMT-07:00 hxj137:
> 我错误的认为 init_by_lua 是针对单次http req 的,是因为 我在 init_by_lua 中定义了一个全局变量
> 例如 varstatic = 3
>
> 然后在某个请求中
> content_by_lua '
> varstatic = varstatic +1
> ngx.say("varstatic ",varstatic )
> ';
>
> 然后我怎么刷新 值都会是4

注意 nginx 一般会配置多个 worker 进程。各个 worker 进程都有自己独立的 Lua VM 实例。init_by_lua
只是运行在 worker 进程 fork 之前的 master 进程里面的 Lua VM 中而已。所以 init_by_lua 里面创建的
Lua 全局变量对各个 worker 进程而言只有只读的价值。

如果你要在所有 worker 进程间共享数据,应该使用 lua_shared_dict.

进一步地,应该尽量避免使用 Lua 全局变量,见这里的官方文档中的讨论:

https://github.com/openresty/lua-nginx-module#lua-variable-scope

init_worker_by_lua 配合 ngx.timer.at() 和共享内存字典也可以做复杂的初始化工作,只是要小心处理 race
condition 的问题,即在 timer 完成初始化前,worker 可能已经开始处理请求。

Regards,
-agentzh

zheng.x...@zte.com.cn

unread,
Mar 10, 2015, 2:45:52 AM3/10/15
to open...@googlegroups.com
章大侠,发错人了哦,这个不是我咨询的



"Yichun Zhang (agentzh)" <age...@gmail.com>
发件人:  open...@googlegroups.com

2015-03-10 14:36


收件人
openresty <open...@googlegroups.com>
抄送
主题
Re: [openresty] 关于openresty的全局初始化问题


--
--
邮件来自列表“openresty”,专用于技术讨 !

订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档:
http://groups.google.com/group/openresty

--------------------------------------------------------
ZTE Information Security Notice: The information contained in this mail (and any attachment transmitted herewith) is privileged and confidential and is intended for the exclusive use of the addressee(s).  If you are not an intended recipient, any disclosure, reproduction, distribution or other dissemination or use of the information contained is strictly prohibited.  If you have received this mail in error, please delete it and notify us immediately.




--------------------------------------------------------
ZTE Information Security Notice: The information contained in this mail (and any attachment transmitted herewith) is privileged and confidential and is intended for the exclusive use of the addressee(s).  If you are not an intended recipient, any disclosure, reproduction, distribution or other dissemination or use of the information contained is strictly prohibited.  If you have received this mail in error, please delete it and notify us immediately.


hxj...@gmail.com

unread,
Mar 10, 2015, 4:52:08 AM3/10/15
to open...@googlegroups.com
明戏了  多谢~~

lua_shared_dict 我已经在使用, 不过在 init_by_lua_file 中如果设置一个初始化的值
比如 share:set("test",1) ,在其他访问路径进行更改
那么 在  nginx.conf 中 lua_code_cache off;  开启的情况下 ,感觉每次init_by_lua_file 都运行了一样
test 的值 每次都恢复为 1 

当我 更改  lua_code_cache on; 后 才变得正常与符合预期

在 2015年3月10日星期二 UTC+8下午2:36:48,agentzh写道:
Reply all
Reply to author
Forward
0 new messages