用openresty做后台服务器

与OpenResty及其原作者邂逅

      OpenResty是一个优秀的开源项目,作者是章亦春。官网是http://openresty.org/en/。这已经是我第二次在公司项目中使用它展开业务了。分享使用经历的时候,顺便帮春哥推广一下:

这里写图片描述

       其实,我想说的是春哥真的像知乎上传的一样,热情,专业。同时也希望大家都到邮件列表里问问题,像我一样莽莽撞撞直接私发邮件,确实显得不大矜持,汗!

我们后台选用的第三方库和文件

       我们后台采用: openresty框架+lua脚本+c函数+mongoDB数据库.这其中的技术细节我们都论证过,现把他们都链接粘出来。

  1. Lua调用mongo的驱动 https://github.com/bigplum/lua-resty-mongol
  2. OpenResty官网下载 http://openresty.org/en/download.html
  3. Lua调用C函数http://blog.csdn.net/wuxiangege/article/details/52220015

架构思想和坑

       想成为高手,得首先学会跟高手一样思考问题,我觉得这也是每一个有志向的程序员练级所必须经历的。下面是我跟架构师的工作交流时,从正面和侧面所总结的,即为什么要采用:openresty框架+lua脚本+c函数+mongoDB数据库作为后台的原因。

  • 对于openresty,我的理解是openresty = web服务器nginx + lua虚拟机。它的好处官网都有,如”能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关”。对于我们的项目,架构师看中的似乎是openresty对nginx的lua扩展,因为接下来我们会调用第三方提供的dll文件,即lua调C。在操作数据库方面,openresty提供的很多精良的数据库驱动,如memcached、mysql、redis。虽然官方没有提供mongodb,但bigplum大神已经做到了,见上方链接。经过测试,bigplum大神提供的测试文件可用,对文档的数据库、集合和文档的增删改查均可用,点个赞。
  • openresty官方已经实现了redis的驱动,我们为什么选用mongodb而不是redis?架构师是这么回答我的,1、redis太重了,mongodb稍微轻一点点,而且我们的项目是在win上部署的对内存有一些要求,架构师希望它在普通的pc上也能运行;2、项目要求数据库同步,也就是a/b/c/d/四台机器数据保持数据的一致性。在这一点上,我们采取的是mongodb的副本集。

讨论太廉价了,直接上代码吧


nginx.conf文件

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    lua_package_path '/c/Users/wuxian/Desktop/BlackHole/openresty-1.9.15.1-win32/lualib/resty/mongol/init.lua;;';


    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            #解决跨域问题
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Headers' 'Content-Type';
                return 204;
            }   
            if ($request_method = 'POST') {
                add_header 'Access-Control-Allow-Origin' '*';
            }   

            #处理业务的文件
            default_type text/html;
            content_by_lua_file './lua_script/app.lua'; 
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

app.lua文件
local mongo = require "resty.mongol"
local cjson = require "cjson.safe"

-----------------------------------------------------------------------------
--lua call c module
square = package.loadlib("./ADD.dll", "isquare")
alert  = package.loadlib("./ADD.dll", "alert")
add    = package.loadlib("./ADD.dll", "l_add")
sub    = package.loadlib("./ADD.dll", "l_sub")

-----------------------------------------------------------------------------
--Connect mongodb database
conn = mongo:new()

ok, err = conn:set_timeout(5000) 
ok, err = conn:connect("127.0.0.1", 27017)
if not ok then
    ngx.say("connect failed: "..err)
end

local db = conn:new_db_handle("tamigroup")
if not db then
    ngx.say(db)
end

local col = db:get_col("number")

-----------------------------------------------------------------------------
-- Process request data
local function post_request(data)
    local result = {}
    local request = cjson.decode(data)
    if not request or not request["cmd"] then
        result["result"] = "failed"
        result["reason"] = "no 'cmd' field or not a json struct"
        return result
    end

    local sql_str = ""

    --测试
    if request["cmd"] == "demo" then
        local result = {}
        result["result"] = "ok"
        result["reason"] = "This is a test cmd"
        return result
    end

    --插入人员
    if request["cmd"] == "add" then
        local result = {}

        local t = {}
        table.insert(t, {name=request["name"], id=request["id"]})
        r, err = col:insert(t, nil, true)
        if not r then 
            result["result"] = "insert failed: "..err
        else
            result["result"] = "ok"
        end
        return result
    end

    --删除人员
    if request["cmd"] == "delete" then
        local result = {}

        r, err = col:delete({name=request["name"]}, nil, true)
        if not r then 
            request["result"] = "delete failed: "..err
        else
            result["request"] = "ok" 
        end
        return result
    end

    --修改人员
    if request["cmd"] == "update" then
        local result = {}

        r,err = col:update({name=request["old"]},{name=request["new"]}, nil, nil, true)
        if not r then 
            request[result] = "update failed: "..err
        else
            request[result] = "ok"
        end
        return result
    end

    --查询人员
    if request["cmd"] == "find" then
        local result = {}

        r = col:find({name=result["name"]})
        r:limit(1)
        for i , v in r:pairs() do
            result["data"] = v["name"];
        end

        if not result["data"] then
            result["result"] = "He or she don't exist"
        else
            result["result"] = "ok"
        end
        return result
    end

    --调用c模块
    if request["cmd"] == "lua_call_c" then
        local result = {}
        result["data"] = square(12)
        result["add"] = add(100, 50)
        result["result"] = "ok"
        return result
    end

end

-----------------------------------------------------------------------------
-- Parse request body
ngx.req.read_body()
local method = ngx.var.request_method
if method ~= "POST" then
    local result = {}
    result["result"] = "failed"
    result["reason"] = "use POST method"
    ngx.say(cjson.encode(result))
else
    local data = ngx.req.get_body_data()
    ngx.say(cjson.encode(post_request(data)))
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

后记

      整个项目的大致思路就是这酱紫的。在这里面,我们曾调研lua在win下怎样调用c;openresty怎样驱动mongodb;mongodb搭建集群数据怎么保持一致…….也傻乎乎的给春哥和云风写过邮件等等。最终前期调研终于顺利完成,累。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值