在多租戶 SaaS 架構中,App Lifecycle Manager 功能旗標可讓您將評估範圍限定在特定層級,而不必修改二進位檔部署程序。您可以使用執行階段屬性和規則指定個別客戶,或使用單位標籤和推出篩選器指定服務群組。
必要條件
開始前,請先確認下列事項:
- 完成部署功能旗標快速入門導覽課程,或使用 gcloud CLI 部署快速入門導覽課程。
- 已設定為管理 App Lifecycle Manager 資源的
gcloud環境。 - 現有的 SaaS 產品。如要進一步瞭解如何建立 SaaS 產品,請參閱「建立 SaaS 產品」。
資源設計
在多租戶設定中,建議您為服務或應用程式定義單位 kind,並為服務或應用程式的每個執行個體 (代表租戶或部署作業) 定義單位。
如要在多租戶環境中實作功能旗標,請定義可識別租戶的一般運算語言 (CEL) 屬性、設定應用程式在執行階段插入租戶 ID,並使用評估規則影響特定租戶的功能。
如果是較簡單的情況,您也可以手動為每位客戶建立個別推出作業。
1. 建立 SaaS 產品 (如果尚未建立)
gcloud beta app-lifecycle-manager saas create "SAAS_NAME" \
--project="PROJECT_ID" \
--location="global" \
--locations=name="LOCATION_1"
2. 建立 UnitKind
gcloud beta app-lifecycle-manager unit-kinds create "tenant-service-kind" \
--project="PROJECT_ID" \
--location="global" \
--saas="SAAS_NAME"
3. 為租戶建立單位
建立單位時,請使用標籤將租戶服務分類為群組 (例如 group=beta、group=preview、group=all)。
# Create a unit for Tenant A (Beta group)
gcloud beta app-lifecycle-manager units create "tenant-a-service" \
--unit-kind="tenant-service-kind" \
--location="LOCATION_1" \
--labels="group=beta"
# Create a unit for Tenant B (Standard group)
gcloud beta app-lifecycle-manager units create "tenant-b-service" \
--unit-kind="tenant-service-kind" \
--location="LOCATION_1" \
--labels="group=all"
定義屬性和標記
您必須正式定義屬性 (例如 customerID),確保評估規則中的類型安全。
1. 建立 customerID 屬性
gcloud beta app-lifecycle-manager flags attributes create "customer-id-attr" \
--key="customerID" \
--attribute-value-type="STRING" \
--location=global
2. 建立功能旗標
gcloud beta app-lifecycle-manager flags create "enhanced-search" \
--key="enhanced-search" \
--flag-value-type=bool \
--location="global" \
--unit-kind="tenant-service-kind" \
--with-default-evaluation
Application Integration
將 OpenFeature SDK 整合至服務。應用程式必須在執行階段將 customerID 插入情境。
Go 範例:
// Inject customerID into the evaluation context
evalCtx := openfeature.NewEvaluationContext(
currentTenant.ID,
map[string]any{
"customerID": currentTenant.ID,
},
)
// Evaluate the flag
isEnabled, err := client.BooleanValue(
context.Background(),
"enhanced-search",
false,
evalCtx,
)
設定用戶群專屬指定目標
使用評估規則,為特定客戶啟用功能。
gcloud beta app-lifecycle-manager flags update "enhanced-search" \
--location="global" \
--evaluation-spec='{
"rules": [{
"id": "allowlist-for-premium-tenants",
"condition": "customerID in [\"tenant-xyz-123\", \"tenant-abc-789\"]",
"variantId": "Enabled"
}],
"defaultVariant": "Disabled",
"attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/customer-id-attr"]
}'
使用篩選器手動漸進式推出
如果是較簡單的用途,您可以手動為每個客戶標籤觸發個別推出作業,藉此模擬波次。
1. 建立修訂版本並發布
# Create a Revision (Snapshot)
gcloud beta app-lifecycle-manager flags revisions create "enhanced-search-rev-1" \
--location=global \
--flag="enhanced-search"
# Create a Release
gcloud beta app-lifecycle-manager flags releases create "release-of-enhanced-search" \
--location=global \
--unit-kind="tenant-service-kind" \
--revisions="enhanced-search-rev-1"
2. 觸發手動推出作業
您可以使用 --unit-filter,為每個目標群組觸發個別作業,手動安排推出作業。
# Create a RolloutKind
gcloud beta app-lifecycle-manager rollout-kinds create "rollout-kind-of-enhanced-search" \
--unit-kind="tenant-service-kind" \
--rollout-orchestration-strategy="Google.Cloud.Simple.AllAtOnce" \
--location=global
# Deploy to Beta group first
gcloud beta app-lifecycle-manager rollouts create "beta-rollout" \
--flag-release="release-of-enhanced-search" \
--rollout-kind="rollout-kind-of-enhanced-search" \
--location=global \
--unit-filter="unit.labels['group'] == 'beta'"
# After verification, deploy to the rest of the fleet
gcloud beta app-lifecycle-manager rollouts create "full-rollout" \
--flag-release="release-of-enhanced-search" \
--rollout-kind="rollout-kind-of-enhanced-search" \
--location=global \
--unit-filter="unit.labels['group'] == 'all'"
後續步驟
- 瞭解實驗和 A/B 測試。
- 瞭解如何排解問題。