Openretry+lua 二次封装数据和接口聚合

1. 学习lua语法:https://www.w3cschool.cn/lua/

外加一个http:https://github.com/ledgetech/lua-resty-http

2. nginx: https://www.w3cschool.cn/nginx/

3. 学习Openresty:https://openresty-reference.readthedocs.io/en/latest/Lua_Nginx_API

以下个人知识共享

书写的接口聚合和二次封装,如果有更好的方式,欢迎提出,

# 并发合并请求;不区分大小写;
  location ~* ^(/merge) {	
		set $uris "/a,/b";
		set $keys "a,b";
        content_by_lua_block {
		  local mergerequest = require "mergegetrequest";
		  mergerequest.merge();
		}

    }
location ~* ^(/tansfer) {
        content_by_lua_block {
		    local source_key = {"a", "b", "c"}
            local target_key = {"aa", "bb", "cc"}
            local httptransfer = require("azcommon.httpgettransfer")
            -- 里面是进行replace替换的方法,如果 比较复杂的 需要转json 另外开个方法
            httptransfer.transfer(source_key, target_key)
		}
		
    }
httpgettransfer={}

function httpgettransfer.transfer(source_key, target_key)
	-- ngx.log(ngx.INFO, "params:", tostring(args)) 
	-- ngx.log(ngx.INFO, "headers:", tostring(receive_headers)) 
	-- 引入http请求 
	local http = require "resty.http" 
	-- 常用方法封装
	local common = require("azcommon.commonutils")
	-- 当前请求的参数
	local args = ngx.req.get_uri_args()
	-- 当前的headers
	local receive_headers = ngx.req.get_headers()

	--2、组合url请求Get请求 并获取参数 
	--内网地址
	local url = ngx.var.url


	-- url = commonutils.concat_url(args, url)
	-- ngx.log(ngx.INFO, "Current Url is :", url) 
	local httpc = http.new()
	-- ms
	-- https://github.com/ledgetech/lua-resty-http#set_timeouts
	httpc:set_timeouts(8000, 8000, 8000)
	local res, err = httpc:request_uri(url, { 
		method = "GET", 
		headers = receive_headers,
		query = args
	}) 
	if err == nil then
		-- 这里进行数据的重新封装
		--local cjson = require "cjson"
		--local currentData = cjson.decode(res.body);
		--local returnData = {}
		--returnData["mytotal"] = currentData["total"]
		--returnData["mylist"] = currentData["list"]
		
		--ngx.say(cjson.encode(returnData))
		-- 替换操作 加上双引号和封号匹配度更高 缺点,会替换文本中的数据
		local resStr = res.body
		-- 遍历数组进行替换
		for i = 1, #source_key do  
			-- ngx.log(ngx.INFO, "source value:", common.parse_json_key(source_key[i])) 
			
			resStr = string.gsub(resStr,common.parse_json_key(source_key[i]),common.parse_json_key(target_key[i]))
		end 
		
		ngx.status = res.status
		ngx.header["Content-Type"] = "application/json; charset=utf-8"
		ngx.say(resStr)
	else
		ngx.log(ngx.ERR, "err:", err) 
		ngx.status = 500
		ngx.say(err)
	end
	http:close()
end
 
return httpgettransfer
 
-- 合并请求
mergegetrequest={}

function mergegetrequest.merge()
	local common = require("azcommon.commonutils")
	--加载 json 库
	local json = require "cjson"
	-- 获取localtion set 的参数
	local uris = commonutils.string_split(ngx.var.uris,",")
	local keys = commonutils.string_split(ngx.var.keys,",")

	--获取请求方式
	local request_method = ngx.var.request_method
	--判断请求方式
	if request_method ~= "GET" then
		ngx.status = 400
		ngx.say("only GET")
		return
	end
	--获取请求参数值
	local params= ngx.req.get_uri_args()

	local list = {}
	-- 遍历合并请求
	for i=1, #uris do
		-- ngx.log(ngx.INFO, "uri value:", uris[i]) 
		local tmp = {uris[i],{args=params,method=ngx.HTTP_GET}}
		table.insert(list, tmp)
	end
	--发送子请求 请求超时 在各自请求中设置
	local response = {ngx.location.capture_multi(list)}
	--合并响应
	local data = {}
	local status = 500
	for num,resp in pairs(response) do
		-- 先是200
		if resp.status == 200 then status = 200 end
		-- 如果所有报错 用最后一个的状态
		if resp.status ~= 200 and status ~= 200 then status = resp.status end
		resp = json.decode(resp["body"])
		data[keys[num]] = resp
	end
	--响应到客户端
	ngx.status = status
	ngx.header["Content-Type"] = "application/json; charset=utf-8"
	ngx.say(json.encode(data))
end

return mergegetrequest

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值