使用逐步发布功能

本指南介绍了如何使用 App Lifecycle Manager 功能标志配置和执行逐步功能发布。您将学习如何定义定位属性、配置基于百分比的分配,以及安全地执行渐进式发布。

我们假设您希望仅对 1% 的流量启用 enhanced-search 算法(按 userID 属性随机分配),而其余 99% 的流量继续使用标准搜索。

前提条件

在开始之前,请确保您已完成其中一个标准快速入门,以预配基准基础设施或独立功能标志资源:

在应用中集成该标志

如需支持由用户数据驱动的基于百分比的分配,您的应用必须在运行时将属性值(例如 userID)附加到 OpenFeature 评估上下文,然后再调用评估方法。

以下示例演示了如何检索已初始化的客户端,以及如何评估附加了定位上下文的标志。

Go

import (
    "context"
    "fmt"
    "log"

    "github.com/open-feature/go-sdk/openfeature"
)

// Example userID - in a real application, this would come from the request context.
var userID = "example-user-123"

// 1. Get client
client := openfeature.NewClient("simple-api")

// 2. Create Evaluation Context with user data
evalCtx := openfeature.NewEvaluationContext(
    userID,
    map[string]interface{}{
        "userID": userID, // Ensure userID is passed dynamically
    },
)

// 3. Evaluate flag (defaults to false if unreachable)
isEnhanced, err := client.BooleanValue(context.Background(), "enhanced-search", false, evalCtx)
if err != nil {
    log.Printf("Flag evaluation failed: %v", err)
}

// 4. Execute business logic
if isEnhanced {
    log.Printf("Enhanced search algorithm for user %s", userID)
} else {
    log.Printf("Standard search algorithm for user %s", userID)
}

Java

// 1. Get client
var client = OpenFeatureAPI.getInstance().getClient("simple-api");

// 2. Create Evaluation Context with user data
var context = new dev.openfeature.sdk.MutableContext();
context.add("userID", userID);

// 3. Evaluate flag
boolean isEnhanced = client.getBooleanValue("enhanced-search", false, context);

// 4. Execute business logic
if (isEnhanced) {
    System.out.println("Executing enhanced search algorithm...");
} else {
    System.out.println("Executing standard search algorithm...");
}

Python

import openfeature
from openfeature.evaluation_context import EvaluationContext

# 1. Get client
client = openfeature.api.get_client()

# 2. Create Evaluation Context with user data
eval_ctx = EvaluationContext(
    targeting_key=str(user_id),
    attributes={
        "userID": user_id,
    }
)

# 3. Evaluate flag
is_enhanced = client.get_boolean_value(
    "enhanced-search",
    False,
    eval_ctx
)

# 4. Execute business logic
if is_enhanced:
    print(f"Executing enhanced search for user {user_id}")
else:
    print(f"Executing standard search for user {user_id}")

配置标志行为

定义随机属性参数,并将其与功能标志资源上基于百分比的分配规则相关联。

  1. 创建定位属性:在全局范围内定义 userID 属性资源,以便将其注册用于定位评估。

    gcloud beta app-lifecycle-manager flags attributes create "user-id-attr" \
      --key="userID" \
      --attribute-value-type="STRING" \
      --location="global"
    
  2. 定义标志分配规则:构建功能标志资源,为 userID 上随机分配的 enabled 变体分配 1% 的权重。

    gcloud beta app-lifecycle-manager flags create "enhanced-search" \
      --key="enhanced-search" \
      --flag-value-type=bool \
      --location="global" \
      --unit-kind="demo-test-unitkind" \
      --variants='[ { "id": "disabled", "booleanValue": false }, { "id": "enabled", "booleanValue": true } ]' \
      --evaluation-spec='{
        "allocations": [{
          "id": "percentage-rollout",
          "randomizedOn": "userID",
          "slots": [
            {"variant": "disabled", "weight": 99},
            {"variant": "enabled", "weight": 1}
          ]
        }],
        "defaultAllocation": "percentage-rollout",
        "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/user-id-attr"]
      }'
    

创建标志配置发布

如需安全地分发新创建的渐进式分配规则,请逐步完成标准的不可变发布阶段。

  1. 创建修订版本:捕获 1% 分配配置的快照。

    gcloud beta app-lifecycle-manager flags revisions create "enhanced-search-rev-1" \
       --location="global" \
       --flag="enhanced-search"
    
  2. 创建发布版本:将修订版本打包,以便针对您的单元类型进行部署。

    gcloud beta app-lifecycle-manager flags releases create "release-of-enhanced-search" \
       --location="global" \
       --unit-kind="demo-test-unitkind" \
       --revisions="enhanced-search-rev-1"
    
  3. 启动发布:确定编排策略并部署配置。

    gcloud beta app-lifecycle-manager rollout-kinds create "rollout-kind-of-enhanced-search" \
       --unit-kind="demo-test-unitkind" \
       --rollout-orchestration-strategy="Google.Cloud.Simple.AllAtOnce" \
       --location="global"
    
    gcloud beta app-lifecycle-manager rollouts create "rollout-of-enhanced-search" \
       --flag-release="release-of-enhanced-search" \
       --rollout-kind="rollout-kind-of-enhanced-search" \
       --location="global"
    
  4. 监控发布:确认部署达到 SUCCEEDED 状态。

    gcloud beta app-lifecycle-manager rollouts describe "rollout-of-enhanced-search" \
       --location="global"
    

更新标志定位

当 1% 的发布被认为成功且稳定后,通过更新标志的内部配置权重(例如,更新为 50%)来扩大发布百分比受众群体。

  1. 更新标志规范:将分配的槽数更改为 50% 启用和 50% 停用。

    gcloud beta app-lifecycle-manager flags update "enhanced-search" \
      --location="global" \
      --evaluation-spec='{
        "allocations": [{
          "id": "percentage-rollout",
          "randomizedOn": "userID",
          "slots": [
            {"variant": "disabled", "weight": 50},
            {"variant": "enabled", "weight": 50}
          ]
        }],
        "defaultAllocation": "percentage-rollout",
        "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/user-id-attr"]
      }'
    
  2. 部署扩展:按照生命周期将更新后的权重分发给已连接的客户端。

    gcloud beta app-lifecycle-manager flags revisions create "enhanced-search-rev-2" \
       --location="global" \
       --flag="enhanced-search"
    
    gcloud beta app-lifecycle-manager flags releases create "enhanced-search-50pct" \
       --location="global" \
       --unit-kind="demo-test-unitkind" \
       --revisions="enhanced-search-rev-2"
    
    gcloud beta app-lifecycle-manager rollouts create "rollout-of-enhanced-search-50pct" \
       --flag-release="enhanced-search-50pct" \
       --rollout-kind="rollout-kind-of-enhanced-search" \
       --location="global"
    

    此操作可以持续重复,直到该功能达到 100% 部署。

停用标志

功能标志完成发布并作为标准操作行为后,必须安全地停用。

  1. 从应用中移除:从应用代码库中移除所有标志评估条件逻辑,并将更新后的载荷部署到生产环境。
  2. 标记为待清理:将标志状态设置为 CLEANUP,以表明要取消配置该标志:

    gcloud beta app-lifecycle-manager flags update "enhanced-search" \
        --location="global" \
        --state=cleanup
    
  3. 部署最终发布:创建引用该标志的标志发布,并将其发布,以将该标志与您的单元分离:

    gcloud beta app-lifecycle-manager flags releases create "cleanup-enhanced-search" \
        --location="global" \
        --unit-kind="demo-test-unitkind" \
        --flags="enhanced-search"
    
    gcloud beta app-lifecycle-manager rollouts create "rollout-of-cleanup-enhanced-search" \
        --flag-release="cleanup-enhanced-search" \
        --rollout-kind="rollout-kind-of-enhanced-search" \
        --location="global"
    

    发布完成后,从管理平面中删除标志发布、标志修订版本和标志。