@@ -9,7 +9,7 @@ gateway等网关插件的高扩展性
99### 通过docker运行
1010
1111``` bash
12- docker run anjia0532/discovery-syncer-python:v2.4.0
12+ docker run anjia0532/discovery-syncer-python:v2.4.1
1313```
1414
1515特别的,` -c ` 支持配置远端http[ s] 的地址,比如读取静态资源的,比如读取nacos的
@@ -21,9 +21,10 @@ docker run anjia0532/discovery-syncer-python:v2.4.0
2121
2222### Api接口
2323
24- ** 注意:**
24+ ** 注意:**
2525
26- 请勿将此服务暴露到公网,否则对于引发的一切安全事故概不负责。安全起见,所有接口统一都增加 ` SYNCER-API-KEY ` header 头校验,值在配置文件 ` common.syncer-api-key ` 配置
26+ 请勿将此服务暴露到公网,否则对于引发的一切安全事故概不负责。安全起见,所有接口统一都增加 ` SYNCER-API-KEY ` header
27+ 头校验,需要在配置文件 ` common.syncer-api-key ` 修改默认值,
2728长度最低为32位,需要同时包含大小写字母,数字,和特殊字符
2829
2930| 路径 | 返回值 | 用途 |
@@ -38,7 +39,9 @@ docker run anjia0532/discovery-syncer-python:v2.4.0
3839| ` POST /migrate/{gateway-name}/to/{gateway-name} ` | ` OK ` | 将网关数据迁移(目前仅支持apisix) |
3940| ` PUT /restore/{gateway-name} ` | ` OK ` | 将 db-less 文件还原到网关(目前仅支持apisix) |
4041
41- #### ` GET /-/reload ` 重新加载配置文件,加载成功返回OK
42+ #### ` GET /-/reload ` 重新加载配置文件
43+
44+ 加载成功返回OK
4245
4346主要是 cicd 场景或者 k8s 的 configmap reload 场景使用
4447
@@ -69,7 +72,7 @@ docker run anjia0532/discovery-syncer-python:v2.4.0
6972
7073#### ` PUT /discovery/{discovery-name} ` 主动下线上线注册中心的服务,配合CI/CD发版业务用
7174
72- 中的name是注册中心的名字 ,如果不存在,则返回 ` Not Found ` http status code 是404
75+ discovery-name 是注册中心的名字 ,如果不存在,则返回 ` Not Found ` http status code 是404
7376
7477body入参
7578
9598
9699#### ` GET /gateway-api-to-file/{gateway-name}?file=/tmp/apisix.yaml ` 读取网关admin api转换成文件用于备份或者db-less模式
97100
98- gateway-name是网关的名字 ,如果不存在,则返回 ` Not Found ` ,http status code是404
101+ gateway-name 是网关的名字 ,如果不存在,则返回 ` Not Found ` ,http status code是404
99102
100- 如果服务报错,resp body 会返回空字符串,header 中的 ` syncer-err-msg ` 会返回具体原因 http status code 是500参数 ` file `
103+ 如果服务报错,resp body 会返回空字符串,header 中的 ` syncer-err-msg ` 会返回具体原因, http status code 是500,参数 ` file `
101104是可选的,如果不传,则默认` /tmp/文件名 ` 比如 ` /tmp/apisix.yaml ` ,并返回文件路径
102105
103106如果正常,resp body 会返回转换后的文本内容,` syncer-file-location ` 会返回syncer服务端的路径(
@@ -154,12 +157,140 @@ plugins: [ ]
154157
155158## 待优化点
156159
157- 1 . ~~ 目前的同步任务是串行的,如果待同步的量比较大,或者同步时间窗口设置的特别小的情况下,会导致挤压~~
160+ 1 . 已解决 ~~ 目前的同步任务是串行的,如果待同步的量比较大,或者同步时间窗口设置的特别小的情况下,会导致挤压~~
158161
159- 2 . 不支持自定义同步插件,不利于自行扩展
162+ 2 . 已解决 ~~ 不支持自定义同步插件,不利于自行扩展~~
160163
1611643 . 同步机制目前是基于定时轮询,效率比较低,有待优化,比如增加缓存开关,上游注册中心与缓存比对没有差异的情况下,不去拉取/变更下游网关的upstream信息,或者看看注册中心支不支持变动主动通知机制等。
162165
166+ ## 新增服务发现或者网关插件
167+
168+ ### 服务发现
169+
170+ 修改 [ app.model.config.DiscoveryType] ( https://github.com/anjia0532/discovery-syncer-python/blob/f184e1b67ba0a5a47d2d895a2c22acfb46b61b5f/app/model/config.py#L14-L16 ) 新增类型,比如
171+
172+ ``` python
173+ class DiscoveryType (Enum ):
174+ NACOS = " nacos"
175+ EUREKA = " eureka"
176+ # 新增 redis
177+ REDIS = " redis"
178+ ```
179+
180+ 创建 ` app/service/discovery/redis.py ` 文件
181+
182+ ``` python
183+ from typing import List
184+
185+ from app.model.syncer_model import Service, Instance, Registration
186+ from app.service.discovery.discovery import Discovery
187+ from core.lib.logger import for_service
188+
189+ logger = for_service(__name__ )
190+
191+
192+ class Redis (Discovery ):
193+
194+ def __init__ (self , config ):
195+ super ().__init__ (config)
196+
197+ def get_all_service (self , config : dict , enabled_only : bool = True ) -> List[Service]:
198+ pass
199+
200+ def get_service_all_instances (self , service_name : str , ext_data : dict , enabled_only : bool = True ) -> tuple[
201+ List[Instance], int ]:
202+ pass
203+
204+ def modify_registration (self , registration : Registration, instances : List[Instance]):
205+ pass
206+ ```
207+
208+ 修改 ` config.yaml `
209+
210+ ``` yaml
211+ discovery-servers :
212+ redis1 :
213+ type : redis
214+ weight : 100
215+ prefix : 2
216+ host : " redis://localhost:6379"
217+ config :
218+ demo : demo
219+ gateway-servers :
220+ apisix1 :
221+ type : apisix
222+ admin-url : http://apisix-server:9080
223+ prefix : /apisix/admin/
224+ config :
225+ X-API-KEY : xxxxxxxx-xxxx-xxxxxxxxxxxx
226+ version : v3
227+ targets :
228+ - discovery : redis
229+ gateway : apisix1
230+ enabled : false
231+ # .. 忽略其他部分
232+ ```
233+
234+ ### 网关
235+
236+ 修改 [ app.model.config.GatewayType] ( https://github.com/anjia0532/discovery-syncer-python/blob/f184e1b67ba0a5a47d2d895a2c22acfb46b61b5f/app/model/config.py#L19-L21 ) 新增类型,比如
237+
238+ ``` python
239+ class GatewayType (Enum ):
240+ KONG = " kong"
241+ APISIX = " apisix"
242+ SPRING_GATEWAY = " spring_gateway"
243+ ```
244+
245+ 创建 ` app/service/gateway/spring_gateway.py ` 文件
246+
247+ ``` python
248+ from typing import Tuple, List
249+
250+ from app.model.syncer_model import Instance
251+ from app.service.gateway.gateway import Gateway
252+
253+
254+ class SpringGateway (Gateway ):
255+ def __init__ (self , config ):
256+ super ().__init__ (config)
257+
258+ def get_service_all_instances (self , target : dict , upstream_name : str = None ) -> List[Instance]:
259+ pass
260+
261+ def sync_instances (self , target : dict , upstream_name : str , diff_ins : list , instances : list ):
262+ pass
263+
264+ def fetch_admin_api_to_file (self , file_name : str ) -> Tuple[str , str ]:
265+ pass
266+
267+ async def migrate_to (self , target_gateway : ' Gateway' ):
268+ pass
269+ ```
270+
271+ 修改 ` config.yaml `
272+
273+ ``` yaml
274+ discovery-servers :
275+ nacos1 :
276+ type : nacos
277+ weight : 100
278+ prefix : /nacos/v1/
279+ host : " http://nacos-server:8858"
280+ gateway-servers :
281+ spring_gateway1 :
282+ type : spring_gateway
283+ admin-url : http://gateway-server:9080
284+ prefix : /
285+ config :
286+ demo : xxxxxxxx-xxxx-xxxxxxxxxxxx
287+ targets :
288+ - discovery : nacos1
289+ gateway : spring_gateway1
290+ enabled : false
291+ # .. 忽略其他部分
292+ ```
293+
163294Copyright and License
164295---
165296
0 commit comments