Skip to content

Commit

Permalink
feat: 服务转发到本地 #2747
Browse files Browse the repository at this point in the history
* feat: 服务转发到本地 #2747

* feat: 服务转发到本地 #2747
  • Loading branch information
owenlxu authored Nov 15, 2024
1 parent ce0aff2 commit 8527c4f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/gateway/lua/init_common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ hostUtil = require("util.host_util")
math.randomseed(os.time())
uuid.seed()

local handle = io.popen("/sbin/ifconfig eth1 | grep 'inet ' | awk '{print $2}'")
local ip = handle:read("*a")
handle:close()
internal_ip = ip

local ok_table = {
status = 0,
data = true
Expand Down
17 changes: 5 additions & 12 deletions src/gateway/lua/router_srv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

-- 访问限制微服务 --
local allow_services = { "auth", "repository", "generic", "docker", "oci", "maven", "job",
"helm", "pypi", "opdata", "rpm", "s3", "git", "npm", "fs-server","analyst",
"replication", "git", "nuget", "composer", "media", "ddc", "conan","job-schedule" }
"helm", "pypi", "opdata", "rpm", "s3", "git", "npm", "fs-server", "analyst",
"replication", "git", "nuget", "composer", "media", "ddc", "conan", "job-schedule" }
local service_name = ngx.var.service

if not arrayUtil:isInArray(service_name, allow_services) then
Expand Down Expand Up @@ -65,14 +65,6 @@ end
-- 访问限制的工具
local access_util = nil

-- 哪些服务需要转到容器中 --
local service_in_container = {} -- service in container
if config.service_in_container ~= nil and config.service_in_container ~= "" then
for c_service in string.gmatch(config.service_in_container, "([^,]+)") do
table.insert(service_in_container, c_service)
end
end

-- 限制访问频率
if access_util then
local access_result, err = access_util:isAccess()
Expand All @@ -83,8 +75,9 @@ if access_util then
end
end

-- 是否转发到容器服务
if arrayUtil:isInArray(service_name, service_in_container) then
-- 哪些服务需要转到容器中 --
local service_in_container = config.service_in_container
if service_in_container ~= nil and string.find(service_in_container, service_name) ~= nil then
ngx.var.target = config.container_url .. "/" .. service_name
return
end
Expand Down
14 changes: 11 additions & 3 deletions src/gateway/lua/util/host_util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local _M = {}

--[[获取微服务真实地址]]
function _M:get_addr(service_name)

local service_prefix = config.service_prefix

if service_prefix == nil then
Expand All @@ -32,9 +32,9 @@ function _M:get_addr(service_name)
if ngx.var.name_space ~= "" then
return service_prefix .. service_name .. "." .. ngx.var.name_space .. ".svc.cluster.local"
end

-- boot assembly部署
if config.service_name ~= nil and config.service_name ~= "" then
if config.service_name ~= nil and config.service_name ~= "" then
local domains = stringUtil:split(config.bkrepo.domain, ";")
return domains[math.random(1, #domains)]
end
Expand Down Expand Up @@ -124,6 +124,14 @@ function _M:get_addr(service_name)
table.insert(ips, ip)
end
end
-- return with local service
local service_in_local = config.service_in_local
if internal_ip ~= nil and service_in_local ~= nil and string.find(service_in_local, service_name) ~= nil then
local service_ip = string.gsub(internal_ip, "\n", "")
if arrayUtil:isInArray(service_ip, ips) then
return "127.0.0.1:" .. port
end
end

-- return ip,port address
return ips[math.random(table.getn(ips))] .. ":" .. port
Expand Down
1 change: 1 addition & 0 deletions support-files/templates/gateway#lua#init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ config = {
security_paths = {
},
service_in_container = "__BK_REPO_SERVICE_IN_CONTAINER__",
service_in_local = "__BK_REPO_SERVICE_IN_LOCAL__",
container_url = "__BK_REPO_URL_CONTAINER__"
}

Expand Down

0 comments on commit 8527c4f

Please sign in to comment.