ITeye博客

第四章 Lua模块开发

jinnianshilongnian · 2015-02-27 10:02 · 跟我学Nginx+Lua开发 · 企业架构 · Lua · nginx · Markdown

在实际开发中,不可能把所有代码写到一个大而全的lua文件中,需要进行分模块开发;而且模块化是高性能Lua应用的关键。使用require第一次导入模块后,所有Nginx 进程全局共享模块的数据和代码,每个Worker进程需要时会得到此模块的一个副本(Copy-On-Write),即模块可以认为是每Worker进程共享而不是每Nginx Server共享;另外注意之前我们使用init_by_lua中初始化的全局变量是每请求复制一个;如果想在多个Worker进程间共享数据可以使用ngx.shared.DICT或如Redis之类的存储。

 

在/usr/example/lualib中已经提供了大量第三方开发库如cjson、redis客户端、mysql客户端:

cjson.so

resty/

   aes.lua

   core.lua

   dns/

   lock.lua

   lrucache/

   lrucache.lua

   md5.lua

   memcached.lua

   mysql.lua

   random.lua

   redis.lua

   ……

 

 

需要注意在使用前需要将库在nginx.conf中导入:

    #lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
    lua_package_path "/usr/example/lualib/?.lua;;";  #lua 模块
    lua_package_cpath "/usr/example/lualib/?.so;;";  #c模块 

使用方式是在lua中通过如下方式引入

local cjson = require(“cjson”)
local redis = require(“resty.redis”) 

接下来我们来开发一个简单的lua模块。

vim /usr/example/lualib/module1.lua
local count = 0
local function hello()
   count = count + 1
   ngx.say("count : ", count)
end

local _M = {
   hello = hello
}

return _M

开发时将所有数据做成局部变量/局部函数;通过 _M导出要暴露的函数,实现模块化封装。

 

接下来创建test_module_1.lua

vim /usr/example/lua/test_module_1.lua
local module1 = require("module1")

module1.hello()

 使用 local var = require("模块名"),该模块会到lua_package_path和lua_package_cpath声明的的位置查找我们的模块,对于多级目录的使用require("目录1.目录2.模块名")加载。

 

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://192.168.1.2/lua_module_1进行测试,会得到类似如下的数据,count会递增

count : 1

count :2

……

count :N

 

此时可能发现count一直递增,假设我们的worker_processes  2,我们可以通过kill -9 nginx worker process杀死其中一个Worker进程得到count数据变化。

 

 

假设我们创建了vim /usr/example/lualib/test/module2.lua模块,可以通过local module2 = require("test.module2")加载模块

 

基本的模块开发就完成了,如果是只读数据可以通过模块中声明local变量存储;如果想在每Worker进程共享,请考虑竞争;如果要在多个Worker进程间共享请考虑使用ngx.shared.DICT或如Redis存储。

评论(6)

  1. #6 楼 · javaDog_li · 2018-07-11 14:34

    kang19870415 writes:

    这一章内容我尝试了无数遍也没有成功,后来将模块包中mysql.lua,改成sql.lua也没成功,但直接加入mysql.lua就成功。 这内容是不是少了什么步骤?

    如果是导lua package ,有问题, 看看是不是 把导包语句写到了http 里,我开始写到了nginx.conf 头部 是不行的

  2. #5 楼 · main616 · 2016-09-12 23:48

    main616 writes:

    假设我们的worker_processes 2, 我们可以通过kill -9 nginx worker process杀死其中一个Worker进程得到count数据变化。

    after kill a processor nginx start a new processor

    assume 2 worker_processor workerA & workerB after curl localhost:8080 workerA process load the lua do curl again now count=2

    ==================kill workerA you will get count=1 ,reload lua => the log 2016/09/12 23:40:47 [notice] 9583#0: start worker processes 2016/09/12 23:40:47 [notice] 9583#0: start worker process 9584 2016/09/12 23:40:47 [notice] 9583#0: start worker process 9585 2016/09/12 23:41:00 [debug] 9584#0: *1 [lua] a_module.lua:2: ======start loading====== 2016/09/12 23:41:00 [debug] 9584#0: *1 [lua] a_module.lua:5: hello(): count=1 2016/09/12 23:41:00 [info] 9584#0: *1 client 127.0.0.1 closed keepalive connection 2016/09/12 23:41:05 [debug] 9584#0: *2 [lua] a_module.lua:5: hello(): count=2 2016/09/12 23:41:05 [info] 9584#0: *2 client 127.0.0.1 closed keepalive connection 2016/09/12 23:41:30 [notice] 9583#0: signal 17 (SIGCHLD) received 2016/09/12 23:41:30 [alert] 9583#0: worker process 9584 exited on signal 9 2016/09/12 23:41:30 [notice] 9583#0: start worker process 9607 2016/09/12 23:41:30 [notice] 9583#0: signal 29 (SIGIO) received 2016/09/12 23:41:37 [debug] 9585#0: *3 [lua] a_module.lua:2: ======start loading====== 2016/09/12 23:41:37 [debug] 9585#0: *3 [lua] a_module.lua:5: hello(): count=1 2016/09/12 23:41:37 [info] 9585#0: *3 client 127.0.0.1 closed keepalive connection

    ==================kill workerB you will get count=3 ,not reload => the log 2016/09/12 23:43:19 [notice] 9676#0: start worker processes 2016/09/12 23:43:19 [notice] 9676#0: start worker process 9677 2016/09/12 23:43:19 [notice] 9676#0: start worker process 9678 2016/09/12 23:43:31 [debug] 9677#0: *1 [lua] a_module.lua:2: ======start loading====== 2016/09/12 23:43:31 [debug] 9677#0: *1 [lua] a_module.lua:5: hello(): count=1 2016/09/12 23:43:31 [info] 9677#0: *1 client 127.0.0.1 closed keepalive connection 2016/09/12 23:43:44 [debug] 9677#0: *2 [lua] a_module.lua:5: hello(): count=2 2016/09/12 23:43:44 [info] 9677#0: *2 client 127.0.0.1 closed keepalive connection 2016/09/12 23:44:02 [notice] 9676#0: signal 17 (SIGCHLD) received 2016/09/12 23:44:02 [alert] 9676#0: worker process 9678 exited on signal 9 2016/09/12 23:44:02 [notice] 9676#0: start worker process 9701 2016/09/12 23:44:02 [notice] 9676#0: signal 29 (SIGIO) received 2016/09/12 23:44:05 [debug] 9677#0: *3 [lua] a_module.lua:5: hello(): count=3 2016/09/12 23:44:05 [info] 9677#0: *3 client 127.0.0.1 closed keepalive connection

    file a_module.lua local count = 0 ngx.log(ngx.DEBUG, "======start loading======") local function hello() count = count + 1 ngx.log(ngx.DEBUG, "count="..count) ngx.say("count:",count) end

    local _M = { hello = hello }

    return _M

    file hello.lua local a_module = require('a_module') a_module.hello()

    conf/nginx.conf location / { default_type text/html; content_by_lua_file lua/hello.lua; }

  3. #4 楼 · main616 · 2016-09-12 23:43

    假设我们的worker_processes 2, 我们可以通过kill -9 nginx worker process杀死其中一个Worker进程得到count数据变化。

    after kill a processor nginx start a new processor

    assume 2 worker_processor workerA & workerB after curl localhost:8080 workerA process load the lua do curl again now count=2

    ==================kill workerA you will get count=1 ,reload lua => the log 2016/09/12 23:40:47 9583#0: start worker processes 2016/09/12 23:40:47 9583#0: start worker process 9584 2016/09/12 23:40:47 9583#0: start worker process 9585 2016/09/12 23:41:00 9584#0: *1 a_module.lua:2: ======start loading====== 2016/09/12 23:41:00 9584#0: *1 a_module.lua:5: hello(): count=1 2016/09/12 23:41:00 9584#0: *1 client 127.0.0.1 closed keepalive connection 2016/09/12 23:41:05 9584#0: *2 a_module.lua:5: hello(): count=2 2016/09/12 23:41:05 9584#0: *2 client 127.0.0.1 closed keepalive connection 2016/09/12 23:41:30 9583#0: signal 17 (SIGCHLD) received 2016/09/12 23:41:30 9583#0: worker process 9584 exited on signal 9 2016/09/12 23:41:30 9583#0: start worker process 9607 2016/09/12 23:41:30 9583#0: signal 29 (SIGIO) received 2016/09/12 23:41:37 9585#0: *3 a_module.lua:2: ======start loading====== 2016/09/12 23:41:37 9585#0: *3 a_module.lua:5: hello(): count=1 2016/09/12 23:41:37 9585#0: *3 client 127.0.0.1 closed keepalive connection

    ==================kill workerB you will get count=3 ,not reload => the log 2016/09/12 23:43:19 9676#0: start worker processes 2016/09/12 23:43:19 9676#0: start worker process 9677 2016/09/12 23:43:19 9676#0: start worker process 9678 2016/09/12 23:43:31 9677#0: *1 a_module.lua:2: ======start loading====== 2016/09/12 23:43:31 9677#0: *1 a_module.lua:5: hello(): count=1 2016/09/12 23:43:31 9677#0: *1 client 127.0.0.1 closed keepalive connection 2016/09/12 23:43:44 9677#0: *2 a_module.lua:5: hello(): count=2 2016/09/12 23:43:44 9677#0: *2 client 127.0.0.1 closed keepalive connection 2016/09/12 23:44:02 9676#0: signal 17 (SIGCHLD) received 2016/09/12 23:44:02 9676#0: worker process 9678 exited on signal 9 2016/09/12 23:44:02 9676#0: start worker process 9701 2016/09/12 23:44:02 9676#0: signal 29 (SIGIO) received 2016/09/12 23:44:05 9677#0: *3 a_module.lua:5: hello(): count=3 2016/09/12 23:44:05 9677#0: *3 client 127.0.0.1 closed keepalive connection

  4. #3 楼 · 漫听风月 · 2016-03-25 13:45

    访问如http://192.168.1.2/lua_module_1进行测试,会得到类似如下的数据,count会递增 这个不会递增吧,一直是1

  5. #2 楼 · newboy2004 · 2015-12-20 14:50

    location /lua_module_1 { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/example/lua/test_module_1.lua; }

    导入这个重新reload nginx 无法访问

  6. #1 楼 · kang19870415 · 2015-10-20 10:46

    这一章内容我尝试了无数遍也没有成功,后来将模块包中mysql.lua,改成sql.lua也没成功,但直接加入mysql.lua就成功。 这内容是不是少了什么步骤?

评论功能维护中,当前仅展示历史评论。