云眼配置

云眼About 3 min

云眼配置

本主题介绍如何使用 EyeofcloudConfig 访问数据文件中的项目配置数据。用于云眼灰度实验 Go SDK。

概述

云眼灰度实验 SDK 打开一组定义良好的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据

在本文档中,我们扩展了公共 API 以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。

EyeofcloudConfig API

公共配置数据模型(EyeofcloudConfig)在下面定义为静态Eyeofcloud项目数据的结构化格式。

获取云眼配置

EyeofcloudConfig可以通过以下公共API调用从EyeofcloudClient(顶级)访问:

Go

client, e := eyeofcloudFactory.Client() var config = client.GetEyeofcloudConfig()

getEyeofcloudConfig 返回一个EyeofcloudConfig实例,其中包括

  • 环境键
  • 开发工具包密钥
  • 数据文件修订号
  • 按其键值映射的所有实验
  • 所有属性
  • 所有受众
  • 所有活动
  • 按其键值映射的灰度标帜
  • 检索项目配置(数据文件)的函数

📘 注意

当 SDK 数据文件更新时(客户端可以为ProjectConfigUpdateNotification添加通知侦听器以获取通知),客户端应调用该方法以获取更新的 EyeofcloudConfig 数据。请参阅以下示例。

获取数据文件

要在多个 SDK 实例之间共享同一数据文件(例如,在客户端/服务器方案中),可以在实例之间传递配置(数据文件)的 JSON 字符串表示形式。若要获取数据文件,请使用EyeofcloudConfig对象的GetDatafile方法。有关更多信息,请参阅与多个 SDK 实现共享数据文件

对象模型

下面显示了 EyeofcloudConfig 的对象模型。

Go

type EyeofcloudConfig struct {     
    EnvironmentKey string     
    SdkKey         string     
    Revision       string      
    
    // This experimentsMap is for experiments of legacy projects only.     
    // For flag projects, experiment keys are not guaranteed to be unique     
    // across multiple flags, so this map may not include all experiments     
    // when keys conflict.     
    ExperimentsMap map[string]EyeofcloudExperiment      
    
    FeaturesMap map[string]EyeofcloudFeature     
    Attributes  []EyeofcloudAttribute     
    Audiences   []EyeofcloudAudience     
    Events      []EyeofcloudEvent 
}  

type EyeofcloudExperiment struct {     
    ID            string     
    Key           string     
    Audiences     string     
    VariationsMap map[string]EyeofcloudVariation 
}  

type EyeofcloudFeature struct {     
    ID              string     
    Key             string     
    ExperimentRules []EyeofcloudExperiment     
    DeliveryRules   []EyeofcloudExperiment     
    VariablesMap    map[string]EyeofcloudVariable      
    
    // Deprecated: Use experimentRules and deliveryRules     
    ExperimentsMap map[string]EyeofcloudExperiment 
}  

type EyeofcloudVariation struct {     
    ID             string     
    Key            string     
    FeatureEnabled bool     
    VariablesMap   map[string]EyeofcloudVariable 
}  

type EyeofcloudVariable struct {     
    ID    string     
    Key   string     
    Type  string     
    Value string 
}  

type EyeofcloudAttribute struct {     
    ID  string     
    Key string 
}  

type EyeofcloudAudience struct {     
    ID         string     
    Name       string     
    Conditions string 
}  

type EyeofcloudEvent struct {     
    ID            string     
    Key           string     
    ExperimentIds []string 
}

例子

可以从 EyeofcloudClient(顶级)访问 EyeofcloudConfig,如下所示: Go

eyeofcloudConfig := eyeofcloudClient.GetEyeofcloudConfig()      

    fmt.Println("[EyeofcloudConfig] revision = " + eyeofcloudConfig.Revision)     
    fmt.Println("[EyeofcloudConfig] sdkKey = " + eyeofcloudConfig.SdkKey)     
    fmt.Println("[EyeofcloudConfig] environmentKey = " + eyeofcloudConfig.EnvironmentKey)      
    
    fmt.Println("[EyeofcloudConfig] attributes:")     
    for _, attribute := range eyeofcloudConfig.Attributes {         
        fmt.Println("[EyeofcloudAttribute]   -- (id, key) = " + attribute.ID + ", " + attribute.Key)     
    }     
    fmt.Println("[EyeofcloudConfig] audiences:")     
    for _, audience := range eyeofcloudConfig.Audiences {         
        fmt.Println("[EyeofcloudAudience]   -- (id, name, conditions) = " + audience.ID + ", " + audience.Name + ", " + audience.Conditions)     
    }     
    fmt.Println("[EyeofcloudConfig] events:")     
    for _, event := range eyeofcloudConfig.Events {         
        fmt.Println(fmt.Sprintf("[EyeofcloudEvent]   -- (id, key, experimentIds) = %s, %s, %v", event.ID, event.Key, event.ExperimentIds))     
    }      
    
    // all flags     
    flagKeys := []string{}     
    for flagKey := range eyeofcloudConfig.FeaturesMap {         
        flagKeys = append(flagKeys, flagKey)     
    }      
    
    for _, flagKey := range flagKeys {         
        flag := eyeofcloudConfig.FeaturesMap[flagKey]          
        
        experimentRules := flag.ExperimentRules         
        deliveryRules := flag.DeliveryRules          
        
        // use experiment rules and delivery rules and other flag data here...          
        
        for _, experiment := range experimentRules {             
            fmt.Println("[EyeofcloudExperiment]   - experiment rule-key = " + experiment.Key)             
            fmt.Println("[EyeofcloudExperiment]   - experiment audiences = " + experiment.Audiences)              
            
            variationKeys := []string{}             
            variationsMap := experiment.VariationsMap             
            for variationKey, _ := range variationsMap {                 
                variationKeys = append(variationKeys, variationKey)             
            }              
            
            for _, varKey := range variationKeys {                 
                variation := variationsMap[varKey]                 
                fmt.Println(fmt.Sprintf("EyeofcloudVariation]       -- variation = { key: %s, id: %s, featureEnabled: %v }", varKey, variation.ID, variation.FeatureEnabled))                  
                
                variablesMap := variationsMap[varKey].VariablesMap                 
                variableKeys := []string{}                  
                
                for variableKey := range variablesMap {                     
                    variableKeys = append(variableKeys, variableKey)                 
                }                  
                
                for _, variableKey := range variableKeys {                     
                    variable := variablesMap[variableKey]                      
                    
                    fmt.Println(fmt.Sprintf("[EyeofcloudVariable]           --- variable: %s, %v", variableKey, variable))                 
                }             
            }         
        }          
        
        for _, delivery := range deliveryRules {             
            fmt.Println("[EyeofcloudExperiment]   - delivery rule-key = " + delivery.Key)             
            fmt.Println("[EyeofcloudExperiment]   - delivery audiences = " + delivery.Audiences)              
            
            // use delivery rule data here...         
        }     
}      

// listen to NotificationType.datafileChange to get updated data     
callback := func(notification notification.ProjectConfigUpdateNotification) {         
    var newOptConfig = eyeofcloudClient.GetEyeofcloudConfig()         
    fmt.Println("[EyeofcloudConfig] revision = " + newOptConfig.Revision)     
}     
eyeofcloudClient.ConfigManager.OnProjectConfigUpdate(callback)
Last update:
Contributors: zhangweixue