
Python與Ollama的開發案例
ARM 模板 是 JSON 文件,用于定義和部署 Azure 基礎設施。通過 ARM 模板,我們可以實現基礎設施即代碼(IaC),將配置作為代碼進行版本控制、重用和維護。ARM 模板的主要優勢包括:
ARM 模板包含以下主要部分:
定義參數:
parameters
部分,定義必要的輸入值,如服務名稱、發布者郵箱等。""parameters"": {
""apiManagementServiceName"": {
""type"": ""string"",
""defaultValue"": ""myApiManagementService""
},
""publisherEmail"": {
""type"": ""string"",
""defaultValue"": ""publisher@example.com""
},
""publisherName"": {
""type"": ""string"",
""defaultValue"": ""My Company""
}
}
定義資源:
resources
部分,定義要部署的資源。例如,創建一個 API Management 實例:
{
""type"": ""Microsoft.ApiManagement/service"",
""apiVersion"": ""2021-08-01"",
""name"": ""[parameters('apiManagementServiceName')]"",
""location"": ""[resourceGroup().location]"",
""properties"": {
""publisherEmail"": ""[parameters('publisherEmail')]"",
""publisherName"": ""[parameters('publisherName')]""
},
""sku"": {
""name"": ""Developer"",
""capacity"": 1
}
}
輸入參數:
啟動部署:
驗證部署:
添加 API:
dependsOn
屬性確保 API 在 API Management 實例創建后部署。{
""type"": ""Microsoft.ApiManagement/service/apis"",
""apiVersion"": ""2021-08-01"",
""name"": ""[concat(parameters('apiManagementServiceName'), '/PetStore')]"",
""dependsOn"": [
""[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]""
],
""properties"": {
""displayName"": ""Pet Store"",
""protocol"": ""https"",
""path"": ""petstore"",
""serviceUrl"": ""https://petstore.swagger.io/v2""
}
}
完整模板示例:
{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#"",
""contentVersion"": ""1.0.0.0"",
""parameters"": {
""apiManagementServiceName"": {
""type"": ""string"",
""defaultValue"": ""myApiManagementService""
},
""publisherEmail"": {
""type"": ""string"",
""defaultValue"": ""publisher@example.com""
},
""publisherName"": {
""type"": ""string"",
""defaultValue"": ""My Company""
}
},
""resources"": [
{
""type"": ""Microsoft.ApiManagement/service"",
""apiVersion"": ""2021-08-01"",
""name"": ""[parameters('apiManagementServiceName')]"",
""location"": ""[resourceGroup().location]"",
""properties"": {
""publisherEmail"": ""[parameters('publisherEmail')]"",
""publisherName"": ""[parameters('publisherName')]""
},
""sku"": {
""name"": ""Developer"",
""capacity"": 1
}
},
{
""type"": ""Microsoft.ApiManagement/service/apis"",
""apiVersion"": ""2021-08-01"",
""name"": ""[concat(parameters('apiManagementServiceName'), '/PetStore')]"",
""dependsOn"": [
""[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]""
],
""properties"": {
""displayName"": ""Pet Store"",
""protocol"": ""https"",
""path"": ""petstore"",
""serviceUrl"": ""https://petstore.swagger.io/v2""
}
}
],
""outputs"": {}
}
檢查 API Management 實例:
驗證操作:
通過 ARM 模板,我們可以實現基礎設施即代碼,確保開發、測試和生產環境之間的一致性。ARM 模板的主要優勢包括:
原文引自YouTube視頻:https://www.youtube.com/watch?v=1F6uvXQDcPI