Skip to content

Commit

Permalink
feat: 支持api路由指定虚拟前缀 feat: 同步补充文档 feat: 优化部分代码 feat: 调整官网样式
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtsm committed Feb 27, 2023
1 parent da52b25 commit 21abfc1
Show file tree
Hide file tree
Showing 50 changed files with 755 additions and 472 deletions.
20 changes: 11 additions & 9 deletions balance/tl_ops_balance_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ function _M:tl_ops_balance_core_filter(ctx)
-- 服务节点配置列表
local service_list_str, _ = cache_service:get(tl_ops_constant_service.cache_key.service_list);
if not service_list_str then
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty)
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty, "")
return
end
local service_list_table = cjson.decode(service_list_str);
if not service_list_table and type(service_list_table) ~= 'table' then
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty)
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty, "")
return
end

Expand Down Expand Up @@ -75,7 +75,7 @@ function _M:tl_ops_balance_core_filter(ctx)
node, node_state, node_id, host, rule_match_mode = tl_ops_balance_core_body.tl_ops_balance_body_service_matcher(service_list_table)
if not node then
-- 无匹配
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", balance_mode, tl_ops_constant_balance.cache_key.mode_empty)
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", balance_mode, tl_ops_constant_balance.cache_key.mode_empty, "")
return
end
end
Expand All @@ -86,13 +86,13 @@ function _M:tl_ops_balance_core_filter(ctx)
if rule_match_mode and rule_match_mode == api_match_mode.api then
-- 域名负载
if host == nil or host == '' then
tl_ops_err_content:err_content_rewrite_to_balance("", "nil", balance_mode, tl_ops_constant_balance.cache_key.host_empty)
tl_ops_err_content:err_content_rewrite_to_balance("", "nil", balance_mode, tl_ops_constant_balance.cache_key.host_empty, "")
return
end

-- 域名匹配
if host ~= "*" and host ~= ngx.var.host then
tl_ops_err_content:err_content_rewrite_to_balance("", "pass", balance_mode, tl_ops_constant_balance.cache_key.host_pass)
tl_ops_err_content:err_content_rewrite_to_balance("", "pass", balance_mode, tl_ops_constant_balance.cache_key.host_pass, "")
return
end
end
Expand All @@ -106,7 +106,7 @@ function _M:tl_ops_balance_core_filter(ctx)
local token_result = tl_ops_limit_fuse_token_bucket.tl_ops_limit_token( node.service, node_id)
if not token_result or token_result == false then
balance_count:tl_ops_balance_count_incr_fail(node.service, node_id)
tl_ops_err_content:err_content_rewrite_to_balance("", "t-limit", balance_mode, tl_ops_constant_balance.cache_key.token_limit)
tl_ops_err_content:err_content_rewrite_to_balance("", "t-limit", balance_mode, tl_ops_constant_balance.cache_key.token_limit, "")
return
end
end
Expand All @@ -116,7 +116,7 @@ function _M:tl_ops_balance_core_filter(ctx)
local leak_result = tl_ops_limit_fuse_leak_bucket.tl_ops_limit_leak( node.service, node_id)
if not leak_result or leak_result == false then
balance_count:tl_ops_balance_count_incr_fail(node.service, node_id)
tl_ops_err_content:err_content_rewrite_to_balance("", "l-limit", balance_mode, tl_ops_constant_balance.cache_key.leak_limit)
tl_ops_err_content:err_content_rewrite_to_balance("", "l-limit", balance_mode, tl_ops_constant_balance.cache_key.leak_limit, "")
return
end
end
Expand All @@ -137,7 +137,7 @@ function _M:tl_ops_balance_core_filter(ctx)
end
shared:incr(limit_req_fail_count_key, 1)

tl_ops_err_content:err_content_rewrite_to_balance(node.service .. ":" .. node.name, "offline", balance_mode, tl_ops_constant_balance.cache_key.offline)
tl_ops_err_content:err_content_rewrite_to_balance(node.service .. ":" .. node.name, "offline", balance_mode, tl_ops_constant_balance.cache_key.offline, "")
return
end

Expand All @@ -155,6 +155,7 @@ function _M:tl_ops_balance_core_balance(ctx)
local tlops_ups_mode = ctx.tlops_ups_mode
local tlops_ups_node = ctx.tlops_ups_node
local tlops_ups_node_id = ctx.tlops_ups_node_id
local tlops_ups_api_prefix = ngx.var.tlops_ups_api_prefix;

if not tlops_ups_mode or not tlops_ups_node or not tlops_ups_node_id then
return
Expand All @@ -173,7 +174,8 @@ function _M:tl_ops_balance_core_balance(ctx)
ngx.header[tl_ops_constant_balance.proxy_server] = tlops_ups_node.service .. ":" .. tlops_ups_node.name;
ngx.header[tl_ops_constant_balance.proxy_state] = "online"
ngx.header[tl_ops_constant_balance.proxy_mode] = tlops_ups_mode

ngx.header[tl_ops_constant_balance.proxy_prefix] = tlops_ups_api_prefix

local ok, err = ngx_balancer.set_current_peer(tlops_ups_node.ip, tlops_ups_node.port)
if ok then
ngx_balancer.set_timeouts(3, 60, 60)
Expand Down
30 changes: 20 additions & 10 deletions balance/tl_ops_balance_core_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local tl_ops_constant_health = require("constant.tl_ops_constant_health")
local shared = ngx.shared.tlopsbalance
local find = ngx.re.find


-- 处理匹配逻辑
local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
local match_mode = obj.match_mode
Expand All @@ -21,13 +22,14 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
end
-- 正则匹配模式
if match_mode == tl_ops_match_mode.reg or match_mode == tl_ops_match_mode.regi or match_mode == tl_ops_match_mode.regid then
local from, to , _ = find(request_uri , obj.url , match_mode);
local from, to , _ = find(request_uri , (obj.fake_prefix .. obj.url) , match_mode);
if from and to then
local sub = string.sub(request_uri, from, to)
if sub then
if not matcher or not matcher.url then
matcher = obj
end
-- 规则最长匹配优先
if matcher.url and #sub > #matcher.url then
matcher = obj
end
Expand All @@ -36,7 +38,7 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
end
-- 全文匹配
if match_mode == tl_ops_match_mode.all then
if request_uri == obj.url then
if request_uri == (obj.fake_prefix .. obj.url) then
matcher = obj;
end
end
Expand All @@ -45,23 +47,19 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
end

-- 获取命中的api路由项
local tl_ops_balance_api_get_matcher_rule = function(api_list_table, rule, rule_match_mode)
local tl_ops_balance_api_get_matcher_rule = function(api_list_table, rule, rule_match_mode, request_uri)
local matcher = nil;
local matcher_list = api_list_table[rule]

if not matcher_list then
return nil
end

-- 获取当前url
local request_uri = tl_ops_utils_func:get_req_uri();

-- 获取当前host
local cur_host = ngx.var.host;

for i, obj in pairs(matcher_list) do
repeat

if rule_match_mode == tl_ops_constant_balance_api.mode.host then
-- 如果是优先host规则匹配,先剔除不属于当前host的规则
if obj.host == nil or obj.host == '' then
Expand Down Expand Up @@ -109,15 +107,18 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
if not api_list_table then
return nil, nil, nil, nil, rule_match_mode
end

-- 获取当前url
local request_uri = tl_ops_utils_func:get_req_uri();

-- 根据路由当前策略进行路由, 返回正则命中的api
if api_rule == tl_ops_constant_balance_api.rule.point then
matcher = tl_ops_balance_api_get_matcher_rule(
api_list_table, tl_ops_constant_balance_api.rule.point, rule_match_mode
api_list_table, tl_ops_constant_balance_api.rule.point, rule_match_mode, request_uri
);
elseif api_rule == tl_ops_constant_balance_api.rule.random then
matcher = tl_ops_balance_api_get_matcher_rule(
api_list_table, tl_ops_constant_balance_api.rule.random, rule_match_mode
api_list_table, tl_ops_constant_balance_api.rule.random, rule_match_mode, request_uri
);
end

Expand All @@ -142,7 +143,6 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
end
-- 服务内随机
elseif api_rule == tl_ops_constant_balance_api.rule.random then
local request_uri = tl_ops_utils_func:get_req_uri();
math.randomseed(#request_uri)
node_id = tonumber(math.random(0,1) % #service_list_table[matcher.service]) + 1
node = service_list[node_id]
Expand All @@ -158,6 +158,16 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
if rewrite_url and rewrite_url ~= '' then
ngx.req.set_uri(rewrite_url, false)
end

-- 需要转发到服务具体路径
local fake_prefix = matcher.fake_prefix
if fake_prefix and matcher.fake_prefix ~= '' then
-- 通过虚拟前缀截取后缀
local fake_sub = string.sub(request_uri, #fake_prefix + 1, #request_uri)
if fake_sub then
ngx.var.tlops_ups_api_prefix = fake_sub
end
end

return node, node_state, node_id, host, rule_match_mode
end
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.1.0"
TL_OPS_VER="v3.2.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.1.0"
TL_OPS_VER="v3.2.0"

echo_msg(){
cur_time=$(date "+%Y-%m-%d %H:%M:%S")
Expand Down
7 changes: 5 additions & 2 deletions conf/tl_ops_manage.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ server {

charset utf8;

location / {
# lua_code_cache off;

location / {
set $tlops_ups_api_prefix "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
Expand All @@ -36,7 +39,7 @@ server {
proxy_set_header X-CLIENT-VERIFY $ssl_client_verify;
add_header Access-Control-Allow-Headers *;
add_header Access-Control-Allow-Methods *;
proxy_pass http://tlopsmanage;
proxy_pass http://tlopsmanage$tlops_ups_api_prefix;

rewrite_by_lua_block {
tlops:tl_ops_process_init_rewrite();
Expand Down
1 change: 1 addition & 0 deletions constant/tl_ops_constant_balance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local tl_ops_constant_balance = {
proxy_server = "Tl-Proxy-Server", -- 请求头标记
proxy_state = "Tl-Proxy-State",
proxy_mode = "Tl-Proxy-Mode",
proxy_prefix = "Tl-Proxy-Prefix",
api = {
list = {
point = tl_ops_constant_balance_api.point,
Expand Down
2 changes: 2 additions & 0 deletions constant/tl_ops_constant_balance_api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local tl_ops_constant_balance_api = {
node = 0, -- 当前url路由到的service下的node的索引
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
},
random = {
id = 1,
Expand All @@ -30,6 +31,7 @@ local tl_ops_constant_balance_api = {
service = "tlops-demo", -- 当前url路由到的service
host = "tlops1.com", -- 当前url处理的域名范围
rewrite_url = "", -- 当前url重写后的url
fake_prefix = "", -- 当前uri规则的虚拟前缀
}
},
rule = {-- api rule 策略
Expand Down
5 changes: 3 additions & 2 deletions err/tl_ops_err_content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ end

-- uri重写,实现错误内容自定义
-- 重写到balance err内容处理
function _M:err_content_rewrite_to_balance(server, state, mode, err)
function _M:err_content_rewrite_to_balance(server, state, mode, err, prefix)
ngx.req.set_uri_args({
type = "balance",
cache_key = err,
mode = mode,
state = state,
server = server
server = server,
prefix = prefix
})
ngx.req.set_uri("/balanceerr/", true)
end
Expand Down
1 change: 1 addition & 0 deletions err/tl_ops_err_content_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local tl_ops_err_content_balance_handler = function(args)
ngx.header[constant_balance.proxy_server] = args.server
ngx.header[constant_balance.proxy_state] = args.state
ngx.header[constant_balance.proxy_mode] = args.mode
ngx.header[constant_balance.proxy_prefix] = args.prefix

local str = cache_balance:get(args.cache_key)
if not str then
Expand Down
2 changes: 1 addition & 1 deletion gitbook/README_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

## 插件模块接口

除了默认模块的配置外,项目还支持插件自行提供对外API管理接口,典型的例子就是, `SSL插件` 提供的 `get_ssl.lua``set_ssl.lua`
除了默认模块的配置外,项目还支持插件自行提供对外API管理接口
9 changes: 2 additions & 7 deletions gitbook/README_DEV.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 开发指引

tl-ops-manage总体设计比较简单,对于稍微熟悉lua的同学来说,也可以快速上手,进行二次开发以贴合业务,我从以下几个方面来引导需要进行二次开发或者参与项目维护的同学
tl-ops-manage总体设计比较简单,对于稍微了解lua的同学来说,也可以快速上手,进行二次开发以贴合业务,我从以下几个方面来引导需要进行二次开发或者参与项目维护的同学


### 明确需求
Expand All @@ -15,9 +15,4 @@ tl-ops-manage总体设计比较简单,对于稍微熟悉lua的同学来说,

测试是一个必不可少的步骤,测试的完整性决定了你写的代码的可用性,一般的功能可以通过日志和模拟操作进行调试,更完善一些的可以通过编写一些test:ngixn用例( 需要学习一些前置试用知识 )。

如果有可能,可以对功能进行单独压测和链路压测。提供记录一些性能瓶颈,以便于后续优化。


---------------

##### 下面将以tl-ops-manage项目结构进行说明
如果有可能,可以对功能进行单独压测和链路压测。提供记录一些性能瓶颈,以便于后续优化。
43 changes: 35 additions & 8 deletions gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

* [插件开发流程](usage/plugin/README.md)

* [动态证书配置](usage/ssl/README.md)

* [集群部署配置](usage/cluster/README.md)


* [tl-ops-manage-源码解析](README_CODE.md)

Expand All @@ -53,6 +49,8 @@

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

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

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

* [动态配置](code/dynamic/README.md)
Expand Down Expand Up @@ -82,21 +80,51 @@

* [页面代理插件](code/pageproxy/README.md)

* [使用配置](usage/pageproxy/README.md)

* [动态证书插件](code/ssl/README.md)

* [同步器插件](code/sync/README.md)
* [使用配置](usage/ssl/README.md)

* [登陆认证插件](code/auth/README.md)

* [使用配置](usage/auth/README.md)

* [跨域设置插件](code/cors/README.md)

* [使用配置](usage/cors/README.md)

* [日志分析插件](code/loganalyze/README.md)

* [使用配置](usage/loganalyze/README.md)

* [链路追踪插件](code/tracing/README.md)

* [使用配置](usage/tracing/README.md)

* [自检调试插件](code/healthdebug/README.md)

* [使用配置](usage/healthdebug/README.md)

* [同步预热插件](code/sync/README.md)

* [使用配置](usage/sync/README.md)

* [字段同步](code/sync/README_FIELDS.md)

* [数据同步](code/sync/README_DATA.md)

* [耗时告警插件](code/timealert/README.md)

* [使用配置](usage/timealert/README.md)

* [日志告警](code/timealert/README_LOG.md)

* [邮件告警](code/timealert/README_EMIAL.md)

* [集群管理插件](code/cluster/README_CLUSTER.md)
* [集群节点插件](code/cluster/README_CLUSTER.md)

* [使用配置](usage/cluster/README.md)

* [主从节点](code/cluster/README_NODE.md)

Expand All @@ -121,5 +149,4 @@
* [提交PR](dev/README_PR.md)



* [常见问题解答](qa/README.md)
* [常见问题解答](qa/README.md)
Loading

0 comments on commit 21abfc1

Please sign in to comment.