Skip to content

Commit

Permalink
feat: 支持静态资源路由配置 feat: 补充文档 feat: 优化部分代码和注释
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtsm committed Mar 5, 2023
1 parent f7bf676 commit eea9876
Show file tree
Hide file tree
Showing 30 changed files with 884 additions and 70 deletions.
27 changes: 20 additions & 7 deletions balance/tl_ops_balance_core_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ local cjson = require("cjson.safe");
local cache_api = require("cache.tl_ops_cache_core"):new("tl-ops-balance-api");
local tl_ops_utils_func = require("utils.tl_ops_utils_func");
local tl_ops_constant_balance_api = require("constant.tl_ops_constant_balance_api");
local tl_ops_match_mode = require("constant.tl_ops_constant_comm").tl_ops_match_mode;
local tl_ops_constant_health = require("constant.tl_ops_constant_health")
local shared = ngx.shared.tlopsbalance
local find = ngx.re.find

local tl_ops_constant_comm = require("constant.tl_ops_constant_comm");
local tl_ops_constant_health = require("constant.tl_ops_constant_health");
local tl_ops_match_mode = tl_ops_constant_comm.tl_ops_match_mode;
local tl_ops_api_type = tl_ops_constant_comm.tl_ops_api_type;
local shared = ngx.shared.tlopsbalance;
local find = ngx.re.find;

-- 处理匹配逻辑
local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
Expand Down Expand Up @@ -81,6 +82,7 @@ end


local tl_ops_balance_api_service_matcher = function(service_list_table)

local matcher = nil
local node = nil

Expand Down Expand Up @@ -153,15 +155,26 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
local node_state , _ = shared:get(key)


-- 静态页面代理路径
local api_type = matcher.api_type
if api_type and api_type == tl_ops_api_type.page then
ngx.req.set_uri_args({
url = request_uri
})
ngx.req.set_uri("/pageproxy", true)
return
end

-- 需要重写url
local rewrite_url = matcher.rewrite_url
if rewrite_url and rewrite_url ~= '' then
ngx.req.set_uri(rewrite_url, false)
return
end

-- 需要转发到服务具体路径
-- 需要转发到服务下的具体路径
local fake_prefix = matcher.fake_prefix
if fake_prefix and matcher.fake_prefix ~= '' then
if fake_prefix and fake_prefix ~= '' then
-- 通过虚拟前缀截取后缀
local fake_sub = string.sub(request_uri, #fake_prefix + 1, #request_uri)
if fake_sub then
Expand Down
2 changes: 1 addition & 1 deletion bin/install_centeros.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/"
TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf"
TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;"
TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;"
TL_OPS_VER="v3.2.0"
TL_OPS_VER="v3.3.0"

echo_msg(){
cur_time=$(date "+%Y-%m-%d %H:%M:%S")
Expand Down
2 changes: 1 addition & 1 deletion bin/install_ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/"
TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf"
TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;"
TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;"
TL_OPS_VER="v3.2.0"
TL_OPS_VER="v3.3.0"

echo_msg(){
cur_time=$(date "+%Y-%m-%d %H:%M:%S")
Expand Down
8 changes: 8 additions & 0 deletions conf/tl_ops_manage.conf
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ server {
}
}

location = /pageproxy {
set $pageproxy "";
alias $pageproxy;
rewrite_by_lua_block {
tlops:tl_ops_process_init_rewrite('onlyplugin');
}
}

location /balanceerr/ {
content_by_lua_block {
tlops:tl_ops_process_init_content();
Expand Down
3 changes: 3 additions & 0 deletions constant/tl_ops_constant_balance_api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local match_mode = require("constant.tl_ops_constant_comm").tl_ops_match_mode;
local api_type = require("constant.tl_ops_constant_comm").tl_ops_api_type;

-- api 默认列表
local tl_ops_constant_balance_api = {
Expand All @@ -23,6 +24,7 @@ local tl_ops_constant_balance_api = {
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
api_type = api_type.api, -- 当前uri规则的类型
},
random = {
id = 1,
Expand All @@ -32,6 +34,7 @@ local tl_ops_constant_balance_api = {
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
api_type = api_type.api, -- 当前uri规则的类型
}
},
rule = {-- api rule 策略
Expand Down
14 changes: 14 additions & 0 deletions constant/tl_ops_constant_comm.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
local tl_ops_constant_comm = {
tl_ops_status = {-- service status 状态
-- 在线
online = 0,
-- 下线
offline = 1,
-- 错误
error = 2
},
tl_ops_rt = {-- rt 返回值
-- 成功
ok = 0,
-- 错误
error = -1,
-- 参数有误
args_error = -2,
-- 找不到
not_found = -3,
-- 解析错误
parse_error = -4
},
tl_ops_match_mode = {-- api匹配模式
Expand All @@ -20,6 +28,12 @@ local tl_ops_constant_comm = {
regi = "joi",
-- 最长字符串匹配
regid = "joid"
},
tl_ops_api_type = {-- api类型
-- 接口类型
api = "api",
-- 文件资源
page = "page"
}
};

Expand Down
11 changes: 11 additions & 0 deletions doc/change.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@

## 事务更新日程

2023-03-05

1. 支持静态资源路由配置

2. 同步补充文档

3. 优化部分代码

4. 事务日程调整


2023-02-27

1. 支持api路由指定虚拟前缀
Expand Down
2 changes: 1 addition & 1 deletion err/tl_ops_err_content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- @author iamtsm
-- @email [email protected]

local tl_ops_err_content_core = require(".err.tl_ops_err_content_core");
local tl_ops_err_content_core = require("err.tl_ops_err_content_core");

local _M = {}

Expand Down
2 changes: 0 additions & 2 deletions gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@

* [多规则匹配](code/balance/README_RULE.md)

* [多模式匹配](code/balance/README_RULE.md)

* [路由统计](code/balancecount/README.md)

* [动态配置](code/dynamic/README.md)
Expand Down
3 changes: 1 addition & 2 deletions gitbook/code/pageproxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ location xxx {

#### 动态调整路径

还有一个好处就是支持动态调整静态资源路径,当然,前提是你已经支持了可视化操作修改路径

还有一个好处就是支持动态调整静态资源路径,v-3.3.0以上已支持动态配置静态资源路由

## 实现

Expand Down
26 changes: 26 additions & 0 deletions gitbook/usage/balance/README_RULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ tl_ops_match_mode = {-- api匹配模式
}
```

## API路由虚拟前缀

![图片](https://qnproxy.iamtsm.cn/WechatIMG50.png "图片")

这个场景也是有小伙伴反馈的,目前已支持在路由后将请求转发到具体服务下的具体路径,比如有这样几个规则

`/open-api/order-api/*` - `订单服务节点1`

`/open-api/product-api/*` - `产品服务节点1`

`/open-api/member-api/*` - `会员服务节点1`

那么对应的 /open-api 作为虚拟前缀, /order-api/* 作为订单服务下的所有接口,这样可以方便的统一多个服务的对外统一接口


## 静态资源路由

目前支持静态文件资源路由配置,需要结合page_proxy插件开启使用

首先需要配置好api路由策略,其次在插件配置页面设置好对应路由需要分发的静态资源路径即可。


## 在文件中的配置

在文件中的配置需注意的是,如果`sync`插件为开启状态时,会有后台任务同步文件中的配置数据至store中,且是根据 `id` 来判定是否需要执行同步逻辑。所以需要保证文件中的配置的数据的id是具有唯一性的标识字段。
Expand All @@ -67,6 +89,8 @@ point = {
node = 0, -- 当前url路由到的service下的node的索引
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
api_type = api_type.api, -- 当前uri规则的类型
}
},
random = {
Expand All @@ -77,6 +101,8 @@ random = {
service = "tlops-demo", -- 当前url路由到的service
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
api_type = api_type.api, -- 当前uri规则的类型
}
},
```
Expand Down
2 changes: 1 addition & 1 deletion plugins/tl_ops_page_proxy/export_set_router.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- tl_ops_set_page_proxy_export
-- en : set export page_proxy config
-- zn : 更新log_analyze插件配置管理
-- zn : 更新page_proxy插件配置管理
-- @author iamtsm
-- @email [email protected]

Expand Down
30 changes: 30 additions & 0 deletions plugins/tl_ops_page_proxy/get_router.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- tl_ops_page_proxy_get
-- en : get page_proxy config list
-- zn : 获取page_proxy配置列表
-- @author iamtsm
-- @email [email protected]

local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy");
local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant");
local tl_ops_rt = tlops.constant.comm.tl_ops_rt;
local tl_ops_utils_func = tlops.utils
local cjson = require("cjson.safe");
cjson.encode_empty_table_as_object(false)


local Router = function()

local list_str, _ = cache:get(constant.cache_key.list);
if not list_str or list_str == nil then
tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.not_found, "not found list", _);
return;
end


local res_data = {}
res_data[constant.cache_key.list] = cjson.decode(list_str)

tl_ops_utils_func:set_ngx_req_return_ok(tl_ops_rt.ok, "success", res_data);
end

return Router
83 changes: 83 additions & 0 deletions plugins/tl_ops_page_proxy/page_proxy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-- tl_ops_page_proxy
-- en : page_proxy
-- zn : 静态页面代理实现
-- @author iamtsm
-- @email [email protected]

local constant = require("plugins.tl_ops_page_proxy.tl_ops_plugin_constant");
local cache = require("cache.tl_ops_cache_core"):new("tl-ops-page-proxy");
local tlog = require("utils.tl_ops_utils_log"):new("tl_ops_plugin_page_proxy");
local env = tlops.env
local cjson = require("cjson.safe")
cjson.encode_empty_table_as_object(false)

local _M = {
_VERSION = '0.01',
}
local mt = { __index = _M }


-- 加载静态页面路由配置
local get_page_proxy_cache = function( request_uri, api )
local list_str, _ = cache:get(constant.cache_key.list);
if not list_str or list_str == nil then
return nil
end

local list = cjson.decode(list_str)
if not list then
return nil
end

for i = 1, #list do
local data = list[i]
if ngx.re.find(api, data.api, 'jo') then
return data
end
end

return nil
end

-- 核心逻辑
function _M:page_proxy_core(ctx)

-- 请求uri
local request_uri = ctx.request_uri
-- 参数
local args = ngx.req.get_uri_args()

if ngx.re.find(request_uri, "/tlopsmanage/", 'jo') then
ngx.var.tlopsmanage = env.path.tlopsmanage
return true, "ok"
end

if ngx.re.find(request_uri, "/website/", 'jo') then
ngx.var.website = env.path.website
return true, "ok"
end

-- 静态页面代理路径
if args.url then
local match = get_page_proxy_cache(request_uri, args.url);
if match and match.path then
ngx.var.pageproxy = match.path .. args.url;
tlog:dbg("page_proxy match : ",match, ", args : ", args, ",request_uri : ",request_uri)
else
tlog:dbg("page_proxy not match : ",match, ", args : ", args, ",request_uri : ",request_uri)
ngx.exit(404)
return true, "ok"
end
end

tlog:dbg("page_proxy plugin ok ")

return true, "ok"
end


function _M:new()
return setmetatable({}, mt)
end

return _M
Loading

0 comments on commit eea9876

Please sign in to comment.