LAMP架構演進到LAMPGC,再演進到LNMLGC(linux+nginx+mysql+lua+gearman+C)
來自: http://blog.csdn.net//jiao_fuyou/article/details/35985843
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 本文由用戶 ck3552 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!