From 2d5497d2c281324c8e572b83af6751cd24d70aca Mon Sep 17 00:00:00 2001 From: Fake Date: Sat, 24 May 2025 11:54:09 +0800 Subject: [PATCH 1/2] Expand the traffic-split plug-in --- apisix/discovery/eureka-filter/init.lua | 50 +++++++++++++++++++++++ apisix/discovery/eureka-filter/schema.lua | 40 ++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 apisix/discovery/eureka-filter/init.lua create mode 100644 apisix/discovery/eureka-filter/schema.lua diff --git a/apisix/discovery/eureka-filter/init.lua b/apisix/discovery/eureka-filter/init.lua new file mode 100644 index 000000000000..26170d98c92b --- /dev/null +++ b/apisix/discovery/eureka-filter/init.lua @@ -0,0 +1,50 @@ +local eureka = require("apisix.discovery.eureka") +local core = require("apisix.core") +local log = core.log + +local _M = { + version = 0.1 +} + +-- 初始化函数,复用 eureka 模块的初始化 +function _M.init_worker() + eureka.init_worker() +end + +-- 获取服务节点并进行过滤 +function _M.nodes(service_name, discovery_args) + -- 获取所有节点 + local nodes = eureka.nodes(service_name) + if not nodes then + return nil + end + + -- 如果没有指定区域,返回所有节点 + if not discovery_args or not discovery_args.zone then + return nodes + end + + -- 按区域过滤节点 + local zone = discovery_args.zone + local filtered_nodes = {} + + for _, node in ipairs(nodes) do + if node.metadata and node.metadata.zone == zone then + core.table.insert(filtered_nodes, node) + end + end + + if #filtered_nodes == 0 then + log.warn("no nodes found for service [", service_name, "] in zone [", zone, "]") + return nodes -- 如果没有找到节点,返回所有节点作为降级策略 + end + + return filtered_nodes +end + +-- 复用 eureka 模块的数据导出 +function _M.dump_data() + return eureka.dump_data() +end + +return _M diff --git a/apisix/discovery/eureka-filter/schema.lua b/apisix/discovery/eureka-filter/schema.lua new file mode 100644 index 000000000000..184ec417e6ef --- /dev/null +++ b/apisix/discovery/eureka-filter/schema.lua @@ -0,0 +1,40 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +return { + type = "object", + properties = { + host = { + type = "array", + minItems = 1, + items = { + type = "string", + }, + }, + fetch_interval = {type = "integer", minimum = 1, default = 30}, + prefix = {type = "string"}, + weight = {type = "integer", minimum = 0}, + timeout = { + type = "object", + properties = { + connect = {type = "integer", minimum = 1, default = 2000}, + send = {type = "integer", minimum = 1, default = 2000}, + read = {type = "integer", minimum = 1, default = 5000}, + } + }, + }, + required = {"host"} +} \ No newline at end of file From c4af8c17e6f8d2cc3ba25d80204df84d99cd1b91 Mon Sep 17 00:00:00 2001 From: Fake Date: Sat, 24 May 2025 12:03:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apisix/discovery/eureka-filter/init.lua | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/apisix/discovery/eureka-filter/init.lua b/apisix/discovery/eureka-filter/init.lua index 26170d98c92b..d8be918a659f 100644 --- a/apisix/discovery/eureka-filter/init.lua +++ b/apisix/discovery/eureka-filter/init.lua @@ -6,25 +6,20 @@ local _M = { version = 0.1 } --- 初始化函数,复用 eureka 模块的初始化 function _M.init_worker() eureka.init_worker() end --- 获取服务节点并进行过滤 function _M.nodes(service_name, discovery_args) - -- 获取所有节点 local nodes = eureka.nodes(service_name) if not nodes then return nil end - -- 如果没有指定区域,返回所有节点 if not discovery_args or not discovery_args.zone then return nodes end - -- 按区域过滤节点 local zone = discovery_args.zone local filtered_nodes = {} @@ -36,13 +31,12 @@ function _M.nodes(service_name, discovery_args) if #filtered_nodes == 0 then log.warn("no nodes found for service [", service_name, "] in zone [", zone, "]") - return nodes -- 如果没有找到节点,返回所有节点作为降级策略 + return nodes end return filtered_nodes end --- 复用 eureka 模块的数据导出 function _M.dump_data() return eureka.dump_data() end