在 Azure API 管理中,如果我们有一个 API 集和 2 个 API 操作,则会执行相同的操作,但较新的版本现在指向一个全新的端点。
例如下面是 BICEP 代码:
// API 集
resource regApi 'Microsoft.ApiManagement/service/apis@2022-08-01' = {
name: 'register-api-transaction'
parent: apimService
properties: {
displayName: 'Register APIs'
apiRevision: '1'
subscriptionRequired: true
serviceUrl: registerServiceUrl
path: 'internal/register/search'
protocols: [
'http'
'https'
]
authenticationSettings: {}
subscriptionKeyParameterNames: {
header: 'Ocp-Apim-Subscription-Key'
query: 'subscription-key'
}
isCurrent: true
}
}
操作:
resource regGetApi 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
name: 'name-register-get'
parent: regApi
properties: {
urlTemplate: '/nameRegister'
method: 'GET'
displayName: 'Name register'
responses: []
}
}
resource searchGetApi 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
name: 'name-search-get'
parent: regApi
properties: {
urlTemplate: '/nameSearch'
method: 'GET'
displayName: 'Name search'
responses: []
}
}
问题:
如果我想将操作指向不同的 serviceUrl 怎么办?两者都指向同一个父级。
是的,在这种情况下您可以使用版本控制,因为版本控制用于进行
API
更改。
您可以同时发布多个 API 版本,并使用路径、查询字符串或标头来区分版本。
set-backend-service
策略,如给定的 MS 文档中详述。
进行更改后,您还可以添加 版本控制方案,这是一个标识符,表示现有 API 的新版本。
我在我的环境中尝试了以下代码,符合您的要求,并且部署成功,如下。
param apiexists string = 'xxxx'
resource apiservice 'Microsoft.ApiManagement/service@2023-03-01-preview' existing = {
name: apiexists
}
resource regApi 'Microsoft.ApiManagement/service/apis@2022-08-01' = {
name: 'register-api-transaction'
parent: apiservice
properties: {
displayName: 'Register APIs'
apiRevision: '2'
subscriptionRequired: true
serviceUrl: '' // Modify this for the new version
path: 'internal/register/search'
protocols: [
'http'
'https'
]
authenticationSettings: {}
subscriptionKeyParameterNames: {
header: 'Ocp-Apim-Subscription-Key'
query: 'subscription-key'
}
isCurrent: true
}
}
//Operations
resource regGetApiV2 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
name: 'name-register-get'
parent: regApi
properties: {
urlTemplate: '/nameRegister'
method: 'GET'
displayName: 'xxx'
responses: []
}
}
resource searchGetApiV2 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
name: 'name-search-get'
parent: regApi
properties: {
urlTemplate: '/nameSearch'
method: 'GET'
displayName: 'xxx'
responses: []
}
}
在上面的代码中,我通过将
apiRevision: '2'
和 serviceUrl
修改为 newServiceUrl
,创建了新版本的 API。
如果您想使用基于路径的版本控制方案,请将版本号(例如:
"/v2"
)添加到API的路径中以指示版本。
输出: