Azure API 管理 中的環境永續 API (預覽版)

適用於:開發人員 | 基本 | 標準 | 進階

本文介紹 Azure API 管理 中的功能,可協助您減少 API 流量的碳足跡。 使用這些功能,根據不同 Azure 區域中的碳排放量調整 API 流量。

備註

  • 環境永續性功能目前處於有限預覽階段。 要註冊,請 填寫表格。
  • 這些功能目前可在 Azure API 管理 傳統層 (開發人員、基本、標準、進階) 的 選取區域 中使用。

關於永續 API

組織越來越專注於透過數位基礎設施減少對環境的影響。

API 管理 可讓您透過有助於下列功能來達成這些目標:

透過根據環境因素優化 API 處理流量的方式,您可以:

  • 減少 API 流量的碳排放量
  • 支持企業可持續發展舉措和環境承諾
  • 向利害關係人展示環境責任

流量轉向

流量轉移需要在支援的 Azure 區域中設定後端資源,以提供碳強度資訊。 然後,在負載平衡後端集區中,使用其中一個 碳強度類別,指定區域化後端可接受的碳排放量上限。

此功能與您現有的負載平衡和路由策略相結合,可協助您排除碳排放量相對較高的區域中後端的流量。

在執行階段:

  • API 管理會盡最大努力將流量路由到「綠色」後端(排放量低於指定閾值的區域),並排除「汙染」後端(排放量高於指定閾值的區域)。
  • API 管理在特定條件下會將流量路由至「髒」後端伺服器,以確保服務持續性,例如,當所有區域化後端伺服器都為「髒」,且其他後端伺服器無法使用時。

將流量轉移到負載平衡池中排放量較低的後端的圖表。

設定範例

首先,透過設定選擇性屬性,於azureRegion中配置後端:

{
    "type": "Microsoft.ApiManagement/service/backends", 
    "apiVersion": "2024-10-01-preview", 
    "name": "sustainable-backend", 
    "properties": {
        "url": "https://mybackend.example.com",
        "protocol": "http",
        "azureRegion": "westeurope",
        [...]
  }
}

然後,在負載平衡集區中使用區域化後端,並使用屬性定義 preferredCarbonEmission 排放閾值。

在此例中,如果 westeurope 地區的碳密度超過 Medium,則會排除至 sustainable-backend 的流量,以與集區中的其他後端進行比較。

{
    [...]
    "properties": {
        "description": "Load balancer for multiple backends",
        "type": "Pool",
        "pool": {
            "services": [
                {
                    "id": "<sustainable-backend-id>",
                    "weight": 1,
                    "priority": 1,
                    "preferredCarbonEmission": "Medium"
                }
                {
                    
                    "id": "<regular-backend-id>",
                    "weight": 1,
                    "priority": 1
                }
                {
                    "id": "<fallback-backend-id>",
                    "weight": 1,
                    "priority": 2
                }
            ]
        }
    }
} 

流量整形

流量塑造可讓您根據 API 管理 服務區域 (或多個區域) 中的相對碳排放層級來調整 API 行為。 API 管理 會context.Deployment.SustainabilityInfo.CurrentCarbonIntensity公開內容變數,指出 API 管理 實例的目前碳強度類別。

在 多區域部署中,閘道會提供其執行之對應區域的碳排放強度。

在政策中使用此內容變數,可在低碳排放期間啟用更密集的流量處理,或在高碳排放期間減少處理。

範例:調整高碳排放期間的行為

在下列範例中,API 管理 會延長快取持續時間、實作更嚴格的速率限制,以及減少高碳排放期間的記錄詳細數據。

<policies>
    <inbound>
        <base />
        <choose>
          <when condition="@(context.Deployment.SustainabilityInfo.CurrentCarbonIntensity == CarbonIntensityCategory.High)">
            <!-- Policies for high carbon emission periods -->
            <cache-store duration="3600" />
            <rate-limit-by-key calls="100" renewal-period="60" counter-key="@(context.Request.IpAddress)" />
            <set-variable name="enableDetailedLogging" value="false" />
          </when>
          <when condition="@(context.Deployment.SustainabilityInfo.CurrentCarbonIntensity == CarbonIntensityCategory.Medium)">
            <!-- Policies for medium carbon emission periods -->
            <cache-store duration="1800" />
            <rate-limit-by-key calls="200" renewal-period="60" counter-key="@(context.Request.IpAddress)" />
            <set-variable name="enableDetailedLogging" value="true" />
          </when>
          <otherwise>
            <!-- Policies for low carbon emission periods -->
            <cache-store duration="900" />
            <rate-limit-by-key calls="300" renewal-period="60" counter-key="@(context.Request.IpAddress)" />
            <set-variable name="enableDetailedLogging" value="true" />
          </otherwise>
        </choose>
    
        <!-- Use the logging variable elsewhere -->
        <choose>
          <when condition="@(context.Variables.GetValueOrDefault<bool>("enableDetailedLogging"))">
            <log-to-eventhub logger-id="detailed-logger">
              @{
                  return JObject.FromObject(context).ToString();
              }
            </log-to-eventhub>
          </when>
          <otherwise>
            <log-to-eventhub logger-id="basic-logger">
              @{
                  var log = new JObject();
                  log["requestId"] = context.RequestId;
                  log["method"] = context.Request.Method;
                  log["url"] = context.Request.Url.ToString();
                  log["statusCode"] = context.Response.StatusCode;
                  return log.ToString();
              }
            </log-to-eventhub>
          </otherwise>
        </choose>
    </inbound>
    [...]    
</policies>

範例:將碳強度資訊傳播至後端或日誌

下列範例顯示如何存取目前的碳強度,並將其傳播至後端或日誌中。

<policies>
    [...]
    <outbound>
        <base />
        <set-header name="X-Sustainability-CarbonEmission" exists-action="override">
            <value>@(context.Deployment.SustainabilityInfo.CurrentCarbonIntensity.ToString())</value>
        </set-header>
    </outbound>
    [...]
</policies>

範例:根據碳強度調整追蹤記錄冗長度

下列範例顯示如何使用目前的碳強度資訊來調整自訂 追蹤中傳播的資訊量。

<policies>
    [...]
    <inbound>
        <base />
        <choose>
            <when condition="@(context.Deployment.SustainabilityInfo.CurrentCarbonIntensity >= CarbonIntensityCategory.High)">
                <trace source="Orders API" severity="verbose">
                    <message>Lead Created</message>
                </trace>
            </when>
            <otherwise>
                <trace source="Orders API" severity="information">
                    <message>Lead Created</message>
                </trace>
            </otherwise>
        </choose>
    </inbound>
    [...]    
</policies>

區域可用性

下表指出:

  • API 管理經典層(開發人員層、基本層、標準層、進階層)中,支援永續性功能的實例所在區域(在註冊預覽後)
  • 提供碳排放強度相關資訊的區域,例如,用於建立區域化後端以進行流量轉移
區域 API 管理 支援 碳強度資訊
澳大利亞中部 ✅
澳大利亞中部 2 ✅
Australia East ✅ ✅
Australia Southeast ✅ ✅
巴西南部 ✅ ✅
巴西東南部 ✅
加拿大中部 ✅
加拿大東部 ✅ ✅
印度中部 ✅ ✅
Central US ✅ ✅
智利中部 ✅
東亞 ✅
美國東部 ✅ ✅
美國東部 2 ✅ ✅
法國中部 ✅
法國南部 ✅ ✅
德國北部 ✅
德國中西部 ✅ ✅
印尼中部 ✅
以色列中部 ✅ ✅
義大利北部 ✅ ✅
日本東部 ✅ ✅
日本西部 ✅ ✅
Jio 印度中部 ✅
Jio 印度西部 ✅ ✅
南韓中部 ✅
南韓南部 ✅
馬來西亞南部 ✅
墨西哥中部 ✅ ✅
紐西蘭北部 ✅ ✅
美國中北部 ✅
北歐 ✅ ✅
挪威東部 ✅ ✅
挪威西部 ✅
波蘭中部 ✅ ✅
卡達中部 ✅ ✅
南非北部 ✅ ✅
南非西部 ✅
美國中南部 ✅
印度南部 ✅ ✅
東南亞 ✅
西班牙中部 ✅ ✅
瑞典中部 ✅ ✅
瑞典南部 ✅
瑞士北部 ✅ ✅
瑞士西部 ✅ ✅
台灣北部 ✅ ✅
臺灣西北 ✅
台灣西部 ✅
阿拉伯聯合大公國中部 ✅
阿拉伯聯合大公國北部 ✅ ✅
英國南部 ✅ ✅
英國西部 ✅ ✅
美國中西部 ✅ ✅
西歐 ✅ ✅
印度西部 ✅
美國西部 ✅ ✅
美國西部 2 ✅ ✅
美國西部 3 ✅ ✅

碳強度類別

下表說明流量轉移和流量整形功能中使用的碳強度類別。 範圍 2 排放量的數值以克二氧化碳當量每千瓦時為單位。

類別 克二氧化碳當量
無法使用 N/A
非常低 ≤ 150
Low 151-300
中等 301-500
High 501-700
VeryHigh > 700