Skip to content

Commit 21abfc1

Browse files
committed
feat: 支持api路由指定虚拟前缀 feat: 同步补充文档 feat: 优化部分代码 feat: 调整官网样式
1 parent da52b25 commit 21abfc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+755
-472
lines changed

balance/tl_ops_balance_core.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ function _M:tl_ops_balance_core_filter(ctx)
3939
-- 服务节点配置列表
4040
local service_list_str, _ = cache_service:get(tl_ops_constant_service.cache_key.service_list);
4141
if not service_list_str then
42-
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty)
42+
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty, "")
4343
return
4444
end
4545
local service_list_table = cjson.decode(service_list_str);
4646
if not service_list_table and type(service_list_table) ~= 'table' then
47-
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty)
47+
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", "", tl_ops_constant_balance.cache_key.service_empty, "")
4848
return
4949
end
5050

@@ -75,7 +75,7 @@ function _M:tl_ops_balance_core_filter(ctx)
7575
node, node_state, node_id, host, rule_match_mode = tl_ops_balance_core_body.tl_ops_balance_body_service_matcher(service_list_table)
7676
if not node then
7777
-- 无匹配
78-
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", balance_mode, tl_ops_constant_balance.cache_key.mode_empty)
78+
tl_ops_err_content:err_content_rewrite_to_balance("", "empty", balance_mode, tl_ops_constant_balance.cache_key.mode_empty, "")
7979
return
8080
end
8181
end
@@ -86,13 +86,13 @@ function _M:tl_ops_balance_core_filter(ctx)
8686
if rule_match_mode and rule_match_mode == api_match_mode.api then
8787
-- 域名负载
8888
if host == nil or host == '' then
89-
tl_ops_err_content:err_content_rewrite_to_balance("", "nil", balance_mode, tl_ops_constant_balance.cache_key.host_empty)
89+
tl_ops_err_content:err_content_rewrite_to_balance("", "nil", balance_mode, tl_ops_constant_balance.cache_key.host_empty, "")
9090
return
9191
end
9292

9393
-- 域名匹配
9494
if host ~= "*" and host ~= ngx.var.host then
95-
tl_ops_err_content:err_content_rewrite_to_balance("", "pass", balance_mode, tl_ops_constant_balance.cache_key.host_pass)
95+
tl_ops_err_content:err_content_rewrite_to_balance("", "pass", balance_mode, tl_ops_constant_balance.cache_key.host_pass, "")
9696
return
9797
end
9898
end
@@ -106,7 +106,7 @@ function _M:tl_ops_balance_core_filter(ctx)
106106
local token_result = tl_ops_limit_fuse_token_bucket.tl_ops_limit_token( node.service, node_id)
107107
if not token_result or token_result == false then
108108
balance_count:tl_ops_balance_count_incr_fail(node.service, node_id)
109-
tl_ops_err_content:err_content_rewrite_to_balance("", "t-limit", balance_mode, tl_ops_constant_balance.cache_key.token_limit)
109+
tl_ops_err_content:err_content_rewrite_to_balance("", "t-limit", balance_mode, tl_ops_constant_balance.cache_key.token_limit, "")
110110
return
111111
end
112112
end
@@ -116,7 +116,7 @@ function _M:tl_ops_balance_core_filter(ctx)
116116
local leak_result = tl_ops_limit_fuse_leak_bucket.tl_ops_limit_leak( node.service, node_id)
117117
if not leak_result or leak_result == false then
118118
balance_count:tl_ops_balance_count_incr_fail(node.service, node_id)
119-
tl_ops_err_content:err_content_rewrite_to_balance("", "l-limit", balance_mode, tl_ops_constant_balance.cache_key.leak_limit)
119+
tl_ops_err_content:err_content_rewrite_to_balance("", "l-limit", balance_mode, tl_ops_constant_balance.cache_key.leak_limit, "")
120120
return
121121
end
122122
end
@@ -137,7 +137,7 @@ function _M:tl_ops_balance_core_filter(ctx)
137137
end
138138
shared:incr(limit_req_fail_count_key, 1)
139139

140-
tl_ops_err_content:err_content_rewrite_to_balance(node.service .. ":" .. node.name, "offline", balance_mode, tl_ops_constant_balance.cache_key.offline)
140+
tl_ops_err_content:err_content_rewrite_to_balance(node.service .. ":" .. node.name, "offline", balance_mode, tl_ops_constant_balance.cache_key.offline, "")
141141
return
142142
end
143143

@@ -155,6 +155,7 @@ function _M:tl_ops_balance_core_balance(ctx)
155155
local tlops_ups_mode = ctx.tlops_ups_mode
156156
local tlops_ups_node = ctx.tlops_ups_node
157157
local tlops_ups_node_id = ctx.tlops_ups_node_id
158+
local tlops_ups_api_prefix = ngx.var.tlops_ups_api_prefix;
158159

159160
if not tlops_ups_mode or not tlops_ups_node or not tlops_ups_node_id then
160161
return
@@ -173,7 +174,8 @@ function _M:tl_ops_balance_core_balance(ctx)
173174
ngx.header[tl_ops_constant_balance.proxy_server] = tlops_ups_node.service .. ":" .. tlops_ups_node.name;
174175
ngx.header[tl_ops_constant_balance.proxy_state] = "online"
175176
ngx.header[tl_ops_constant_balance.proxy_mode] = tlops_ups_mode
176-
177+
ngx.header[tl_ops_constant_balance.proxy_prefix] = tlops_ups_api_prefix
178+
177179
local ok, err = ngx_balancer.set_current_peer(tlops_ups_node.ip, tlops_ups_node.port)
178180
if ok then
179181
ngx_balancer.set_timeouts(3, 60, 60)

balance/tl_ops_balance_core_api.lua

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local tl_ops_constant_health = require("constant.tl_ops_constant_health")
1313
local shared = ngx.shared.tlopsbalance
1414
local find = ngx.re.find
1515

16+
1617
-- 处理匹配逻辑
1718
local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
1819
local match_mode = obj.match_mode
@@ -21,13 +22,14 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
2122
end
2223
-- 正则匹配模式
2324
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
24-
local from, to , _ = find(request_uri , obj.url , match_mode);
25+
local from, to , _ = find(request_uri , (obj.fake_prefix .. obj.url) , match_mode);
2526
if from and to then
2627
local sub = string.sub(request_uri, from, to)
2728
if sub then
2829
if not matcher or not matcher.url then
2930
matcher = obj
3031
end
32+
-- 规则最长匹配优先
3133
if matcher.url and #sub > #matcher.url then
3234
matcher = obj
3335
end
@@ -36,7 +38,7 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
3638
end
3739
-- 全文匹配
3840
if match_mode == tl_ops_match_mode.all then
39-
if request_uri == obj.url then
41+
if request_uri == (obj.fake_prefix .. obj.url) then
4042
matcher = obj;
4143
end
4244
end
@@ -45,23 +47,19 @@ local tl_ops_balance_api_matcher_mode = function (matcher, request_uri, obj)
4547
end
4648

4749
-- 获取命中的api路由项
48-
local tl_ops_balance_api_get_matcher_rule = function(api_list_table, rule, rule_match_mode)
50+
local tl_ops_balance_api_get_matcher_rule = function(api_list_table, rule, rule_match_mode, request_uri)
4951
local matcher = nil;
5052
local matcher_list = api_list_table[rule]
5153

5254
if not matcher_list then
5355
return nil
5456
end
5557

56-
-- 获取当前url
57-
local request_uri = tl_ops_utils_func:get_req_uri();
58-
5958
-- 获取当前host
6059
local cur_host = ngx.var.host;
6160

6261
for i, obj in pairs(matcher_list) do
6362
repeat
64-
6563
if rule_match_mode == tl_ops_constant_balance_api.mode.host then
6664
-- 如果是优先host规则匹配,先剔除不属于当前host的规则
6765
if obj.host == nil or obj.host == '' then
@@ -109,15 +107,18 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
109107
if not api_list_table then
110108
return nil, nil, nil, nil, rule_match_mode
111109
end
110+
111+
-- 获取当前url
112+
local request_uri = tl_ops_utils_func:get_req_uri();
112113

113114
-- 根据路由当前策略进行路由, 返回正则命中的api
114115
if api_rule == tl_ops_constant_balance_api.rule.point then
115116
matcher = tl_ops_balance_api_get_matcher_rule(
116-
api_list_table, tl_ops_constant_balance_api.rule.point, rule_match_mode
117+
api_list_table, tl_ops_constant_balance_api.rule.point, rule_match_mode, request_uri
117118
);
118119
elseif api_rule == tl_ops_constant_balance_api.rule.random then
119120
matcher = tl_ops_balance_api_get_matcher_rule(
120-
api_list_table, tl_ops_constant_balance_api.rule.random, rule_match_mode
121+
api_list_table, tl_ops_constant_balance_api.rule.random, rule_match_mode, request_uri
121122
);
122123
end
123124

@@ -142,7 +143,6 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
142143
end
143144
-- 服务内随机
144145
elseif api_rule == tl_ops_constant_balance_api.rule.random then
145-
local request_uri = tl_ops_utils_func:get_req_uri();
146146
math.randomseed(#request_uri)
147147
node_id = tonumber(math.random(0,1) % #service_list_table[matcher.service]) + 1
148148
node = service_list[node_id]
@@ -158,6 +158,16 @@ local tl_ops_balance_api_service_matcher = function(service_list_table)
158158
if rewrite_url and rewrite_url ~= '' then
159159
ngx.req.set_uri(rewrite_url, false)
160160
end
161+
162+
-- 需要转发到服务具体路径
163+
local fake_prefix = matcher.fake_prefix
164+
if fake_prefix and matcher.fake_prefix ~= '' then
165+
-- 通过虚拟前缀截取后缀
166+
local fake_sub = string.sub(request_uri, #fake_prefix + 1, #request_uri)
167+
if fake_sub then
168+
ngx.var.tlops_ups_api_prefix = fake_sub
169+
end
170+
end
161171

162172
return node, node_state, node_id, host, rule_match_mode
163173
end

bin/install_centeros.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/"
66
TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf"
77
TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;"
88
TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;"
9-
TL_OPS_VER="v3.1.0"
9+
TL_OPS_VER="v3.2.0"
1010

1111
echo_msg(){
1212
cur_time=$(date "+%Y-%m-%d %H:%M:%S")

bin/install_ubuntu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ TL_OPS_PATH="/usr/local/tl-ops-manage/"
66
TL_OPS_CONF_PATH="/usr/local/tl-ops-manage/conf/tl_ops_manage.conf"
77
TL_OPS_LUA_PATH="/usr/local/openresty/lualib/?.lua;;/usr/local/tl-ops-manage/?.lua;;"
88
TL_OPS_LUAC_PATH="/usr/local/openresty/lualib/?.so;;"
9-
TL_OPS_VER="v3.1.0"
9+
TL_OPS_VER="v3.2.0"
1010

1111
echo_msg(){
1212
cur_time=$(date "+%Y-%m-%d %H:%M:%S")

conf/tl_ops_manage.conf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ server {
2727

2828
charset utf8;
2929

30-
location / {
30+
# lua_code_cache off;
31+
32+
location / {
33+
set $tlops_ups_api_prefix "";
3134
proxy_set_header X-Real-IP $remote_addr;
3235
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3336
proxy_set_header Host $http_host;
@@ -36,7 +39,7 @@ server {
3639
proxy_set_header X-CLIENT-VERIFY $ssl_client_verify;
3740
add_header Access-Control-Allow-Headers *;
3841
add_header Access-Control-Allow-Methods *;
39-
proxy_pass http://tlopsmanage;
42+
proxy_pass http://tlopsmanage$tlops_ups_api_prefix;
4043

4144
rewrite_by_lua_block {
4245
tlops:tl_ops_process_init_rewrite();

constant/tl_ops_constant_balance.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local tl_ops_constant_balance = {
2525
proxy_server = "Tl-Proxy-Server", -- 请求头标记
2626
proxy_state = "Tl-Proxy-State",
2727
proxy_mode = "Tl-Proxy-Mode",
28+
proxy_prefix = "Tl-Proxy-Prefix",
2829
api = {
2930
list = {
3031
point = tl_ops_constant_balance_api.point,

constant/tl_ops_constant_balance_api.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ local tl_ops_constant_balance_api = {
2222
node = 0, -- 当前url路由到的service下的node的索引
2323
host = "tlops1.com", -- 当前url处理的域名范围
2424
rewrite_url = "", -- 当前url重写后的url
25+
fake_prefix = "", -- 当前uri规则的虚拟前缀
2526
},
2627
random = {
2728
id = 1,
@@ -30,6 +31,7 @@ local tl_ops_constant_balance_api = {
3031
service = "tlops-demo", -- 当前url路由到的service
3132
host = "tlops1.com", -- 当前url处理的域名范围
3233
rewrite_url = "", -- 当前url重写后的url
34+
fake_prefix = "", -- 当前uri规则的虚拟前缀
3335
}
3436
},
3537
rule = {-- api rule 策略

err/tl_ops_err_content.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ end
1717

1818
-- uri重写,实现错误内容自定义
1919
-- 重写到balance err内容处理
20-
function _M:err_content_rewrite_to_balance(server, state, mode, err)
20+
function _M:err_content_rewrite_to_balance(server, state, mode, err, prefix)
2121
ngx.req.set_uri_args({
2222
type = "balance",
2323
cache_key = err,
2424
mode = mode,
2525
state = state,
26-
server = server
26+
server = server,
27+
prefix = prefix
2728
})
2829
ngx.req.set_uri("/balanceerr/", true)
2930
end

err/tl_ops_err_content_core.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local tl_ops_err_content_balance_handler = function(args)
2828
ngx.header[constant_balance.proxy_server] = args.server
2929
ngx.header[constant_balance.proxy_state] = args.state
3030
ngx.header[constant_balance.proxy_mode] = args.mode
31+
ngx.header[constant_balance.proxy_prefix] = args.prefix
3132

3233
local str = cache_balance:get(args.cache_key)
3334
if not str then

gitbook/README_API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
## 插件模块接口
1010

11-
除了默认模块的配置外,项目还支持插件自行提供对外API管理接口,典型的例子就是, `SSL插件` 提供的 `get_ssl.lua``set_ssl.lua`
11+
除了默认模块的配置外,项目还支持插件自行提供对外API管理接口

0 commit comments

Comments
 (0)