如何检查应用是否使用生产级 API
生成应用时,你可能正在使用仍处于预览状态的 API。 与正在与应用一起生成的新功能集成时,通常会使用预览 API。 在将应用发布到生产环境之前,应确保使用生产级 API。 使用服务级别协议 (SLA) 支持并涵盖的稳定 API 时,应用会更可靠。
若要检查应用是否正在使用生产级 API,可以使用 ApiCenterProductionVersionPlugin
插件。 此插件将有关应用 API 请求的信息与 Azure API 中心的信息进行比较,并报告任何非生产 API 请求。 它还会推荐你正在使用的 API 的生产版本。
开始之前
若要检测非生产 API 请求,需要有一个 Azure API 中心 实例,其中包含组织中使用的 API 的相关信息。
创建 Azure API 中心实例
在开始之前,请创建一个 Azure API 中心实例并注册组织中使用的 API。 对于每个 API,定义所使用的版本并指定其生命周期阶段。
此信息 ApiCenterProductionVersionPlugin
用于检查你的应用正在使用的 API 是否属于生产或非生产 API。
复制 API 中心信息
在 Azure API 中心实例概述页中,复制 API 中心实例名称、资源组名称以及订阅 ID。 需要此信息来配置 ApiCenterProductionVersionPlugin
,以便它可以连接到 Azure API 中心实例。
配置 Dev Proxy
若要检查应用是否使用了生产级 API,需要在 Dev Proxy 配置文件中启用 ApiCenterProductionVersionPlugin
。 若要创建应用使用的 API 的报告,请添加报告器。
启用 ApiCenterProductionVersionPlugin
在 devproxyrc.json
文件中,添加以下配置:
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
在 subscriptionId
、resourceGroupName
和 serviceName
属性中,提供有关 Azure API 中心实例的信息。
在 urlsToWatch
属性中,指定应用使用的 URL。
提示
使用 Dev Proxy 工具包 Visual Studio Code 扩展轻松管理 Dev Proxy 配置。
添加报告器
ApiCenterProductionVersionPlugin
可生成应用所使用的 API 的报告。 若要查看此报告,请将报告器添加到 Dev Proxy 配置文件。 Dev Proxy 提供了多个报告器。 在此示例中,你使用的是纯文本报告器。
通过对纯文本报告器的引用更新 devproxyrc.json
文件:
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
},
{
"name": "PlainTextReporter",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
检查应用是否正在使用生产级 API
若要检查应用是否正在使用生产级 API,需要连接到 Azure 订阅,运行 Dev Proxy,并允许其截获来自应用的 API 请求。 然后,Dev Proxy 会将 API 请求的相关信息与 Azure API 中心的信息进行比较,并报告任何非生产级 API。
连接到 Azure 订阅
Dev Proxy 使用 Azure API 中心的信息来确定应用正在使用的 API 是否是生产级 API。 若要获取此信息,需要连接到 Azure 订阅。 可以通过多种方式连接到 Azure 订阅。
运行 Dev Proxy
连接到 Azure 订阅后,启动 Dev Proxy。 如果从 devproxyrc.json
文件所在的同一文件夹中启动 Dev Proxy,则会自动加载配置。 否则,请使用 --config-file
选项指定配置文件的路径。
Dev Proxy 启动后,它会检查是否可以连接到 Azure 订阅。 一旦连接成功,将会收到一条消息,内容如下:
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
按“r”开始记录来自应用的 API 请求。
使用应用
像平时一样使用应用。 Dev Proxy 截获 API 请求,并将有关这些请求的信息存储在内存中。 在运行 Dev Proxy 的命令行中,应该会看到有关应用发出的 API 请求的信息。
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
◉ Recording...
req ╭ GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
api ╰ Passed through
req ╭ GET https://jsonplaceholder.typicode.com/users?api-version=beta
api ╰ Passed through
检查 API 版本
按“s”停止记录。 Dev Proxy 连接到 API 中心实例,并将请求相关信息与 API 中心的信息进行比较。
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
◉ Recording...
req ╭ GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
api ╰ Passed through
req ╭ GET https://jsonplaceholder.typicode.com/users?api-version=beta
api ╰ Passed through
○ Stopped recording
info Checking if recorded API requests use production APIs as defined in API Center...
info Loading APIs from API Center...
info Analyzing recorded requests...
warn Request GET https://jsonplaceholder.typicode.com/users?api-version=beta uses API version beta which is defined as Preview. Upgrade to a production version of the API. Recommended versions: v1.0
info DONE
在 Dev Proxy 完成分析后,它会在名为 ApiCenterProductionVersionPlugin_PlainTextReporter.txt
的文件中创建一个包含以下内容的报告:
Non-production APIs:
GET https://jsonplaceholder.typicode.com/users?api-version=beta
Production APIs:
GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
总结
使用, ApiCenterProductionVersionPlugin
可以检查应用是否正在使用生产级 API。 该插件会将应用中有关 API 请求的信息与 Azure API 中心中的信息进行比较,并报告任何非生产级 API 请求。 它还会推荐你正在使用的 API 的生产版本。 验证应用正在使用的 API 有助于确保应用使用的是受 SLA 支持和涵盖的稳定 API,使应用更加可靠。 可以手动运行此检查或与 CI/CD 管道集成,以确保在将应用发布到生产环境之前它使用的是生产级 API。