配置openresty使用lua并发请求API
背景现在搞的系统前后端分离,采用 restful 风格设计 API. Lua语法lua特殊点 一.数据类型 nil 空 boolean 布尔 number 数字 string 字符串 table 表 local corp = { name = “kang”,age = 20 } function 函数 local function foo() local x=10; local y=20; return x+y; end; local a=foo; a(); 二.表达式 算术运算符 关系运算符 逻辑运算符 字符连接 也可以用 string 库的函数 string.format 连接字符串. string.format(“%s-%s”,”hello”,”word”); hello-word 推荐使用 table.concat()来进行字符串拼接.因为 lua 中字符串是只读的.连接操作会创建一个新的字符串.性能损耗大. 三.表达式 1.单个 if 分支 x=10 if x>0 then print(x); end; 2.两个分支 if-else x=10 if x>0 then print(0); else print(1) end; 3.多个分支 if-elseif-else score=10 if score==10 then print(1) elseif score==11 then print(2) else print(3) end; 4.whie 控制结构 todo end; x=1 sum=0 while x<=5 do sum = sum+x; x = x+1; end; 5.repeat 控制结构 repeat todo until 条件==假 6.for todo end; for i=1,5,1 do print(i); end; 泛型 local a= {“a”,”b”,”c”} for i,v in ipairs(a) do print(i,v) end; lua 的基础库提供了 ipairs. 这是一个用于遍历数组的迭代器函数. 函数 函数的定义 function name (arc) todo end; name = function (arc) end; 没有 local 修饰的都是全局变量.因此函数声明局部函数也要用上 local local function name (arc) todo end; 函数名还可以是 table 数量类型中某个字段. function stu.getName() todo end; 函数的参数 在调用函数的时候,若形参个数和实参个数不同时,Lua 会自动调整实参个数。调整规则:若实参个数大于形参个数,从左向右,多余的实参被忽略;若实参个数小于形参个数,从左向右,没有被实参初始化的形参会被初始化为 nil。 变长参数 local function func( ... ) local tmp = {…} for k,v in pairs(tmp) do print(v) end; end; 函数的返回值 loca s,e = string.find(“hello word”,”llo"); 返回多个值时.值之间用”,”隔开. 调整规则: 若返回值个数大于接收变量的个数,多余的返回值会被忽略掉; 若返回值个数小于参数个数,从左向右,没有被返回值初始化的变量会被初始化为 nil。 注意:当一个函数有一个以上返回值,且函数调用不是一个列表表达式的最后一个元素,那么函数调用只会产生一个返回值,也就是第一个返回值。 local function init() -- init 函数 返回两个值 1 和 "lua" return 1,"lua" end local x,y,z = init(),2 -- init 函数的位置不在最后,此时只返回 1 print(x,z) -->output 1 2 nil local a,b,c = 2,init() -- init 函数的位置在最后,此时返回 1 和 "lua" print(a,c) -->output 2 1 lua 模块 定义个模块 my.lua local foo={} local function getname() return "Lucy" end function foo.greeting() print("hello " .. getname()) end return foo 定义个 main.lua.调用 my.lua模块 local fp = require("my") fp.greeting() -->output: hello Lucy 元表 setmetatable(table,metatable) local mytable = {} local metatable = {} setmetatable(mytable,metatable); or mytable = setmetatable({},{}) 修改表的操作符行为 local table_1 = {10,20,30}; local table_2 = {40,50,60}; local metatable = {}; metatable.__add = function (self,another) local sum = {}; for k,v in pairs(self) do table.insert(sum,v); end; for k,v in pairs(another) do table.insert(sum,v); end return sum; end; setmetatable(table_1,metatable); table_3 = table_1+table_2; for k,v in pairs(table_3) do print(v) end 除了加法可以被重载之外,Lua 提供的所有操作符都可以被重载: 元方法 含义 元方法 含义 组合设计安装 openresty 推荐使用官网教程 配置 nginx 使用 lua location /kang { add_header content-type application/json; content_by_lua_file lua/kang.lua; } lua代码 --加载 json 库 local json = require "cjson"; --获取请求方式 local request_method = ngx.var.request_method; --判断请求方式 if request_method == "GET" then ngx.say(json.encode({"only post"})); return; end; --读取 post 参数.表单需要是 x-www-form-urlencoded ngx.req.read_body(); local api_p = ngx.req.get_post_args(); --拼接子请求 local list = {}; for api,p in pairs(api_p) do local tmp = {api,{args=p,method=ngx.HTTP_GET}}; table.insert(list,tmp); end; --发送子请求 local response = {ngx.location.capture_multi(list)}; --合并响应 local data = {}; for num,resp in pairs(response) do resp = json.decode(resp["body"]); data[resp["uri"]] = resp; end; --响应到客户端 ngx.say(json.encode(data)); 坑 配置 location location /api { #记录下子请求的请求方式,uri set $my_uri $uri; set $my_method $echo_request_method; root /var/www/html; index index.html index.htm; try_files $uri $uri/ /index.php?$query_string; access_log /usr/local/openresty/nginx/logs/access.log main; } location ~ .php { root /var/www/html/laravel/public; include fastcgi_params; #fastcgi_pass unix:/home/wenba/php/var/run/php.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^((?U).+.php)(/?.+)$; fastcgi_index index.php; fastcgi_param REQUEST_URI $my_uri; fastcgi_param REQUEST_METHOD $my_method; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 请求 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |