实现用户配置文件服务 UserProfileService

云眼About 2 min

实现用户配置文件服务 UserProfileService

本主题介绍如何设置自定义用户配置文件服务或如何使用云眼灰度发布(特性标帜)AB实验 Go SDK 的默认值。

使用用户配置文件服务保留有关用户的信息,并确保变体分配具有粘性。在保存实验分配的情况下,您提供的用户配置文件服务实现将覆盖 Eyeofcloud 功能实验的默认分桶行为。

在多服务器或无状态环境中实现时,我们建议将此接口与 Cassandra 或 Redis 等后端一起使用。可以通过配置这些服务来决定将粘性分桶保留多长时间。

实施用户配置文件服务是可选的,仅当希望保持变体分配的粘性时,即使实验条件在运行过程中发生了变化(例如,受众群体、属性、变体暂停和流量分配),才需要实现该服务。否则,Go 开发工具包是无状态的,并依赖于确定性分桶来返回一致的分配。

如果用户配置文件服务未按预期对用户进行分桶,请检查其他灰度发布(特性标帜)是否覆盖了分桶。有关更多信息,请参阅 分桶的工作原理

实现用户配置文件服务 UserProfileService

请参阅下面的代码示例以提供您自己的用户配置文件服务。它应公开两个具有以下签名的函数:

  • 查找:获取用户 ID 字符串并返回与以下架构匹配的用户配置文件。
  • 保存:获取用户配置文件并保留它。

Go

import ( "github.com/eyeofcloud/go-sdk/pkg/decision" ) // CustomUserProfileService is custom implementation of the UserProfileService interface type CustomUserProfileService struct { } // Lookup is used to retrieve past bucketing decisions for users func (s *CustomUserProfileService) Lookup(userID string) decision.UserProfile { return decision.UserProfile{} } // Save is used to save bucketing decisions for users func (s *CustomUserProfileService) Save(userProfile decision.UserProfile) { }

用户配置文件结构

Go

type UserProfile struct { ID string ExperimentBucketMap map[UserDecisionKey]string } // UserDecisionKey is used to access the saved decisions in a user profile type UserDecisionKey struct { ExperimentID string Field string } // Sample user profile with a saved variation userProfile := decision.UserProfile{ ID: "eoc_user_1", ExperimentBucketMap: map[decision.UserDecisionKey]string{ decision.UserDecisionKey{ExperimentID: "experiment_1", Field: "variation_id" }: "variation_1234", }, }

从结构中使用可覆盖默认分桶行为,并为给定用户定义备用实验变体。对于要覆盖的每个实验,向Map添加一个对象。使用实验 ID 作为键,并包含指定所需变体的 variation_id 属性。如果没有实验条目,则默认分桶行为仍然存在。experiment_bucket_map``UserProfile

默认情况下,Go SDK 使用该字段来创建决策键。使用方法手动创建决策键:variation_id``decision.NewUserDecisionKey

Go

decisionKey := decision.NewUserDecisionKey("experiment_id")

将用户配置文件服务实现传递给云眼客户端:

Go

userProfileService := new(CustomUserProfileService) eyeofcloudClient, err := factory.Client( client.WithUserProfileService(userProfileService), )

Last update:
Contributors: “zhangweixue”