LAMP架构演进到LAMPGC,再演进到LNMLGC(linux+nginx+mysql+lua+gearman+C)

LAMP是一个大众的架构了,linux+apache+mysql+php

在我们系统的架构中,做了进一步的演进,linux+apahce+mysql+php+gearman+C

php作页面的展示

核心业务逻辑由C语言实现,php通过gearman中间件调用C任务


由于apache在高并发方面不太给力,因此在需要高并发的场景中,我们进一步演进,linux+nginx+mysql+php+lua+gearman+C

页面部分由nginx+fastcgi+php-fpm来展示

高并发的业务调用由nginx+lua+gearman+C来实现


在这里重点介绍nginx怎样调用gearman中间件

先看下apache+php调用gearman的情况,同步一调用一gearman任务,假如这个任务要3S钟,那么当前这个apache的httpd进程就会被阻塞,它无法为其它客户端服务了


nginx在高并发异步调用的性能够强,这地球人都知道了

lua的协程,也可以实现并发的异步调用

再来看看nginx+lua调用gearman的实现:

nginx同样是一个worker,worker进程里通过lua协程调用gearman任务,即使任务要3S钟,在这3S钟内这个worker进程还是可以为其它客户端服务,这依赖于nginx的epoll的事件触发非阻塞调用和lua协程异步调用的优势,这种方式可以实现高并发的业务调用。


下面给出lua-nginx-module的安装及简单配置过程:

1、需要的源码文件
shell> ls
drwxr-xr-x  6 1000 1000    4096 Feb 26 03:14 LuaJIT-2.0.3
drwxrwxr-x 10 root root    4096 Jun  1  2014 lua-nginx-module-0.9.8
drwxr-xr-x  9 1001 1001    4096 Feb 18 03:14 nginx-1.7.2
drwxrwxr-x  9 root root    4096 Sep 26 02:43 ngx_devel_kit-0.2.19
drwxr-xr-x  8 1169 1169    4096 Feb 26 03:17 pcre-8.21

2、安装luajit
http://luajit.org/download/LuaJIT-2.0.3.tar.gz

shell> cd LuaJIT-2.0.3
shell> make
shell> make install

因为安装在缺省路径,所以LuaJIT对应的lib,include均在/usr/local目录里。

3、编译nginx
https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.9.8
https://codeload.github.com/simpl/ngx_devel_kit/tar.gz/v0.2.19


shell> ./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.2.19 --add-module=../lua-nginx-module-0.9.8 --with-pcre=../pcre-8.21

4、启动nginx
shell> cd /usr/local/nginx/sbin
shell> ./nginx
./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
提示找不到luajit库文件

shell> echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
shell> ldconfig

shell> ./nginx
成功

5、nginx应用
将下列配置加入到/usr/local/nginx/conf/nginx.conf中:
        location /lua {
            set $test "hello, world.";
            content_by_lua '
                ngx.header.content_type = "text/plain";
                ngx.say(ngx.var.test);
            ';
        }

shell> ./nginx -s reload

浏览顺访问: http://172.16.18.114/lua
显示:hello, world.

大功造成!

6、安装lua-resty-gearman模块
https://github.com/zhhchen/lua-resty-gearman/tree/master/lib/resty

7、nginx通过lua脚本调用gearman
在nginx.conf的http段增加:
lua_package_path "/usr/local/lua-resty-gearman/lib/?.lua;;";

在server下增加:
        location /test {
            content_by_lua '
                local gearman = require "gearman"
                local gm = gearman:new()

                gm:set_timeout(1000)    -- 1 sec

                ngx.header.content_type = "text/plain"

                local ok, err = gm:connect("172.16.18.162", 4730)
                if not ok then
                    ngx.say("failed to connect: ", err)
                    return
                end

                ok, err = gm:submit_job("reverse", "abcdef")
                -- submit_job,submit_job_bg,submit_job_high,submit_job_high_bg,submit_job_low,submit_job_low_bg are supported
                -- submit_job(function_name, workload[, unique])

                if not ok then
                    ngx.say("failed to submit job: ", err)
                    return
                else
                    ngx.say(ok)
                end

                -- put it into the connection pool of size 100,
                -- with 0 idle timeout
                local ok, err = gm:set_keepalive(0, 100)
                if not ok then
                    ngx.say("failed to set keepalive: ", err)
                    return
                end

                -- or just close the connection right away:
                -- local ok, err = gm:close()
                -- if not ok then
                --     ngx.say("failed to close: ", err)
                --     return
                -- end
            ';
        }

浏览顺访问: http://172.16.18.114/test
显示:fedcba

你可能感兴趣的:(nginx,lua,gearman)