1- ### 直接使用 Django Command 同步网关
1+ # 直接使用 Django Command 同步网关
22
33项目安装 SDK apigw-manager 后,可以直接执行 SDK 提供的 Django Command。
4+
45- 准备文件的样例 [ examples/django/use-custom-script] ( ../examples/django/use-custom-script )
56
6- #### 步骤1 . 准备自定义同步脚本
7+ ## 步骤 1 . 准备自定义同步脚本
78
89创建一个 bash 脚本,如 ` sync-apigateway.sh ` ,使用 SDK 提供的 Django Command 定义网关配置的同步脚本,样例如下:
10+
911``` shell
1012#! /bin/bash
1113
@@ -31,7 +33,7 @@ python manage.py sync_apigw_config --gateway-name=${gateway_name} --file="${defi
3133python manage.py sync_apigw_stage --gateway-name=${gateway_name} --file=" ${definition_file} "
3234
3335# 同步网关资源
34- #
36+ #
3537# --delete: 当资源在服务端存在,却未出现在资源定义文件中时,指定本参数会强制删除这类资源,以保证服务端资源和文件内容完全一致。
3638# 如果未指定本参数,将忽略未出现的资源
3739# --doc_language: en/zh 是否生成接口文档(中文/英文)
@@ -54,6 +56,7 @@ echo "gateway sync definition end"
5456```
5557
5658如果需要更灵活的控制,也可以采用自定义 Django Command 的方案,例如:
59+
5760``` python
5861from django.conf import settings
5962from django.core.management.base import BaseCommand
@@ -64,7 +67,7 @@ class Command(BaseCommand):
6467 def handle (self , * args , ** kwargs ):
6568 if not getattr (settings, " SYNC_APIGATEWAY_ENABLED" , True ):
6669 return
67-
70+
6871 # 待同步网关名,需修改为实际网关名;直接指定网关名,则不需要配置 Django settings BK_APIGW_NAME
6972 gateway_name = " bk-demo"
7073
@@ -81,15 +84,15 @@ class Command(BaseCommand):
8184 call_command(" fetch_apigw_public_key" , f " --gateway-name= { gateway_name} " )
8285```
8386
84- #### 步骤2 . 添加 SDK apigw-manager
87+ ## 步骤 2 . 添加 SDK apigw-manager
8588
8689将 SDK apigw-manager 添加到项目依赖中,如 pyproject.toml 或 requirements.txt。
8790
88- #### 步骤3 . 将准备的网关配置文件,添加到项目
91+ ## 步骤 3 . 将准备的网关配置文件,添加到项目
8992
9093将准备的网关配置文件:definition.yaml, resources.yaml, apidocs (可选),添加到项目
9194
92- #### 步骤4 . 更新 Django settings 配置
95+ ## 步骤 4 . 更新 Django settings 配置
9396
9497在 Django settings.py 中定义网关名称和接口地址模板:
9598
@@ -104,7 +107,7 @@ BK_APIGW_NAME = "my-gateway-name"
104107# 需将 bkapi.example.com 替换为真实的云 API 域名;
105108# 在 PaaS 3.0 部署的应用,可从环境变量中获取 BK_API_URL_TMPL,不需要额外配置;
106109# 实际上,SDK 将调用网关 bk-apigateway 接口将数据同步到 API 网关
107- BK_API_URL_TMPL = " http://bkapi.example.com/api/{api_name} /" # # 例如:网关host是 :`bkapi.example.com`,则对应的值为:http://bkapi.example.com/api/{api_name} 注意:{api_name} 这个是占位符。
110+ BK_API_URL_TMPL = " http://bkapi.example.com/api/{api_name} /" # # 例如:网关 host 是 :`bkapi.example.com`,则对应的值为:http://bkapi.example.com/api/{api_name} 注意:{api_name} 这个是占位符。
108111```
109112
110113在 INSTALLED_APPS 中加入以下配置,SDK 将创建表 ` apigw_manager_context ` 用于存储一些中间数据:
@@ -115,9 +118,10 @@ INSTALLED_APPS += [
115118]
116119```
117120
118- #### 步骤5 . 同步网关数据到 API 网关
121+ ## 步骤 5 . 同步网关数据到 API 网关
119122
120123chart 部署方案,可采用 K8S Job 同步,样例如下:
124+
121125``` yaml
122126apiVersion : batch/v1
123127kind : Job
@@ -140,12 +144,12 @@ spec:
140144` ` `
141145
142146二进制部署方案,在部署阶段直接执行 sync-apigateway.sh 脚本:
147+
143148` ` ` shell
144149bash support-files/bin/sync-apigateway.sh
145150```
146151
147-
148- ### 支持的 Django Command 列表
152+ ## 支持的 Django Command 列表
149153
150154``` bash
151155# 可选,为网关添加关联应用,关联应用可以通过网关 bk-apigateway 提供的接口管理网关数据
0 commit comments