将后端连接到Azure API管理

⏱️约2分钟
分享:

了解如何使用ARM TemplateAPI Management添加API。 使用operationspolicybackend等资源将API连接到您的后端。 在本文中,我们假定后端是在Azure functions实现的。

定义API

首先,在API Management设置API定义。 在API Management ,每个API都由称为operations的资源定义。

定义operations

operations是与一个API对应的资源。您可以在此资源中定义API路径和参数。 让我们看一下用于operationsARM Template

json
1{
2 "name": "[concat(parameters('serviceName'), '/', parameters('apiName'), ';rev=', parameters('apiRevision'), '/', parameters('operationName'))]",
3 "type": "Microsoft.ApiManagement/service/apis/operations",
4 "apiVersion": "2018-01-01",
5 "dependsOn": [
6 "[resourceId('Microsoft.ApiManagement/service/apis', parameters('serviceName'), concat(parameters('apiName'), ';rev=', parameters('apiRevision')))]",
7 "[resourceId('Microsoft.ApiManagement/service/backends', parameters('serviceName'), parameters('backendName'))]"
8 ],
9 "properties": {
10 "description": "GET Order",
11 "templateParameters": [
12 {
13 "name": "orderId",
14 "type": "string",
15 "values": []
16 }
17 ],
18 "responses": [],
19 "policies": null,
20 "displayName": "GetOrder",
21 "method": "GET",
22 "urlTemplate": "orders/{orderId}"
23 },
24 "resources": []
25}

请注意,该名称后面附加有rev;={apiRevision} ,因为它将被部署为其他文章1中提到的特定修订版。 网址是在urlTemplate指定的。可变参数以{orderId}格式表示。参数的详细信息在templateParameters进行了描述。 上面的示例定义orderId的类型为字符串。 对于method ,请指定HTTP方法。

从以上示例创建的API将为GET http(s)://{API Management FQDN}/api/v1/orders/{orderId} 。这次我们定义的是/orders/{orderId}部分,而/api/v1部分是在之前的apisapiVersionSets 1中定义的。 您可以导入OpenAPI.yaml来定义API。我将在另一篇文章中介绍。

定义backends

接下来,定义backends

json
1{
2 "name": "[concat(variables('serviceName'), '/', parameters('backendName'))]",
3 "type": "Microsoft.ApiManagement/service/backends",
4 "apiVersion": "2018-01-01",
5 "dependsOn": [
6 "[resourceId('Microsoft.ApiManagement/service/properties', parameters('serviceName'), parameters('propertyName'))]"
7 ],
8 "properties": {
9 "title": null,
10 "description": "Backend of API management",
11 "url": "[concat('https://', parameters('functionsAppName'),'.azurewebsites.net/api')]",
12 "protocol": "http",
13 "credentials": {
14 "query": {
15 "code": ["[concat('{{', parameters('propertyName'), '}}')]"]
16 }
17 },
18 "resourceId": "[concat('https://management.azure.com/subscriptions/', subscription().id, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Web/sites/', parameters('functionsAppName'))]"
19 }
20}

backends实际上是与组成API的后端服务等效的资源。这次,我们假设Azure functions HTTPTrigger Azure functions作为后端。 Azure functions的url在url指定, Azure functionsresourceIdresourceId指定。在这种情况下, Azure functionsHTTPTrigger的路由必须为HTTPTrigger orders/{orderId}

credentials ,指定用于与Azure functions连接的API密钥。上面的模板示例意味着,以查询参数的形式(例如?code=xxx指定凭据,并设置由propertyName指定的字符串。凭证的值采用双重封闭的形式,例如{{credential}} 。 凭证(例如API密钥)存储在下面描述的properties资源中。此处指定的propertyName是指示保存在properties的凭据的名称。

顺便说一句,为了在此处指定Azure functions的API密钥,您需要从Azure functions获取密钥。 通常,可以使用模板函数listSecretslistKeys来获取ARM Template键,但是对于Azure functions v2 2而言,它无法正常工作。最好使用Azure CLI 3预先获得它。

powershell
1$resourceId = "/subscriptions/$subscriptionid/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$functionsAppName"
2$url = "$resourceId/host/default/listKeys?api-version=2016-03-01"
3$ret = az rest --method post --uri $url | ConvertFrom-Json
4$key = $ret.functionKeys.default

Azure functions具有主机密钥和功能密钥。在这种情况下,将使用主机密钥。简而言之,功能键是每个功能的键,主机键是可以连接到所有功能的键。

为了将API与后端连接,您还需要在policy指定后端。

policy

最后,设置一个policy以连接后端和API管理。 Policy是一项重要功能,可以向API Management添加各种处理。 在这里,我们使用其中之一连接到后端。

json
1{
2 "variables": {
3 "backendPolicy": "[concat('<policies>\r\n <inbound>\r\n <base />\r\n <set-backend-service id=\"apim-generated-', 'policy','\" backend-id=\"', parameters('backendName'), '\" />\r\n </inbound>\r\n <backend>\r\n <base />\r\n </backend>\r\n <outbound>\r\n <base />\r\n </outbound>\r\n <on-error>\r\n <base />\r\n </on-error>\r\n</policies>')]"
4 },
5 "resources": [
6 {
7 "name": "[concat(parameters('serviceName'), '/', parameters('apiName'), ';rev=', parameters('apiRevision'), '/', 'policy')]",
8 "type": "Microsoft.ApiManagement/service/apis/policies",
9 "apiVersion": "2018-01-01",
10 "properties": {
11 "policyContent": "[variables('backendPolicy')]",
12 "contentFormat": "xml"
13 }
14 }
15 ]
16}

policy是在policyContent xml中policyContent 。在variables中定义了backendPolicy ,您可以看到已定义了xml字符串。 关键是后端资源由backend-id指定。 结果,较早创建的后端资源在policy指定。

定义properties

properties是一种资源,用于以键值格式保存API Management中使用的设置值。在这里,我们将使用此资源来保存API密钥以连接到后端Azure功能。

json
1{
2 "type": "Microsoft.ApiManagement/service/properties",
3 "name": "[concat(parameters('serviceName'), '/', parameters('propertyName'))]",
4 "apiVersion": "2018-06-01-preview",
5 "scale": null,
6 "properties": {
7 "displayName": "[parameters('propertyName')]",
8 "value": "[parameters('functionHostKey')]",
9 "tags": ["key", "function"],
10 "secret": true
11 }
12}

displayName替换为您先前在后端中指定的属性的名称。 为value指定一个秘密value 。在这种情况下,请指定先前通过Azure CLI获得的Azure functions的API密钥。 tags是以后搜索属性时要使用的标签。我们指定一个任意字符串。 通过将secret设置为true ,该属性将被加密并保持安全。

部署

因此,让我们根据到目前为止的解释来部署ARM Template

json
1{
2 "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
3 "contentVersion": "1.0.0.0",
4 "parameters": {
5 "serviceName": {
6 "type": "string",
7 "metadata": {
8 "description": "Service Name"
9 }
10 },
11 "apiName": {
12 "type": "string",
13 "metadata": {
14 "description": "API Name"
15 }
16 },
17 "apiRevision": {
18 "type": "string",
19 "metadata": {
20 "description": "API Revision"
21 }
22 },
23 "operationName": {
24 "type": "string",
25 "metadata": {
26 "description": "Operation name"
27 }
28 },
29 "backendName": {
30 "type": "string",
31 "metadata": {
32 "description": "Backend Name"
33 }
34 },
35 "functionHostKey": {
36 "type": "securestring",
37 "metadata": {
38 "description": "Host key for the functions app"
39 }
40 },
41 "functionAppName": {
42 "type": "string",
43 "metadata": {
44 "description": "Functions App Name"
45 }
46 },
47 "propertyName": {
48 "type": "string",
49 "metadata": {
50 "description": "Property Name"
51 }
52 }
53 },
54 "variables": {
55 "backendPolicy": "[concat('<policies>\r\n <inbound>\r\n <base />\r\n <set-backend-service id=\"apim-generated-', 'policy','\" backend-id=\"', parameters('backendName'), '\" />\r\n </inbound>\r\n <backend>\r\n <base />\r\n </backend>\r\n <outbound>\r\n <base />\r\n </outbound>\r\n <on-error>\r\n <base />\r\n </on-error>\r\n</policies>')]"
56 },
57 "resources": [
58 {
59 "name": "[concat(parameters('serviceName'), '/', parameters('apiName'), ';rev=', parameters('apiRevision'), '/', parameters('operationName'))]",
60 "type": "Microsoft.ApiManagement/service/apis/operations",
61 "apiVersion": "2019-01-01",
62 "dependsOn": [
63 "[resourceId('Microsoft.ApiManagement/service/backends', parameters('serviceName'), parameters('backendName'))]"
64 ],
65 "properties": {
66 "description": "GET Order",
67 "templateParameters": [
68 {
69 "name": "orderId",
70 "type": "string",
71 "values": []
72 }
73 ],
74 "responses": [],
75 "policies": null,
76 "displayName": "GetOrder",
77 "method": "GET",
78 "urlTemplate": "orders/{orderId}"
79 },
80 "resources": []
81 },
82 {
83 "name": "[concat(parameters('serviceName'), '/', parameters('apiName'), ';rev=', parameters('apiRevision'), '/', 'policy')]",
84 "type": "Microsoft.ApiManagement/service/apis/policies",
85 "apiVersion": "2018-01-01",
86 "properties": {
87 "policyContent": "[variables('backendPolicy')]",
88 "contentFormat": "xml"
89 }
90 },
91 {
92 "name": "[concat(parameters('serviceName'), '/', parameters('backendName'))]",
93 "type": "Microsoft.ApiManagement/service/backends",
94 "apiVersion": "2018-01-01",
95 "dependsOn": [
96 "[resourceId('Microsoft.ApiManagement/service/properties', parameters('serviceName'), parameters('propertyName'))]"
97 ],
98 "properties": {
99 "title": null,
100 "description": "Backend of API management",
101 "url": "[concat('https://', parameters('functionAppName'),'.azurewebsites.net/api')]",
102 "protocol": "http",
103 "credentials": {
104 "query": {
105 "code": ["[concat('{{', parameters('propertyName'), '}}')]"]
106 }
107 },
108 "resourceId": "[concat('https://management.azure.com/subscriptions/', subscription().id, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Web/sites/', parameters('functionAppName'))]"
109 }
110 },
111 {
112 "type": "Microsoft.ApiManagement/service/properties",
113 "name": "[concat(parameters('serviceName'), '/', parameters('propertyName'))]",
114 "apiVersion": "2018-06-01-preview",
115 "scale": null,
116 "properties": {
117 "displayName": "[parameters('propertyName')]",
118 "value": "[parameters('functionHostKey')]",
119 "tags": ["key", "function"],
120 "secret": true
121 }
122 }
123 ]
124}

现在,可以使用上述模板将API部署到服务实例。

Footnotes

  1. Set version and revision with Azure API Management 2

  2. Changes to Key Management in Functions V2

  3. Managing Azure Functions Keys (using the new ARM APIs!)

分享:

相关文章

将Azure资源描述为ARM Template
Guides

将Azure资源描述为ARM Template

ARM Template是定义Azure资源的json文件。本文介绍如何高效创建ARM Template来部署新资源。

mark241