在 Azure 应用配置中设置/添加功能标志

Posted

技术标签:

【中文标题】在 Azure 应用配置中设置/添加功能标志【英文标题】:Setting/Adding Feature flag in Azure App Configuration 【发布时间】:2020-10-17 23:41:10 【问题描述】:

有没有办法从 Service Fabric 应用程序设置/添加功能标志。据我搜索,更改和添加功能标志的唯一方法是通过门户进行。有没有办法通过代码本身来做到这一点。

【问题讨论】:

【参考方案1】:

Azure 应用配置 SDK 可用于从 Service Fabric 应用程序创建和更新功能标志。每个功能标志都存储为键值前缀.appconfig.featureflag/。 .NET 的 SDK 可以在 here 找到。功能标志的约定和架构记录在 here。

【讨论】:

【参考方案2】:

您可以使用以下代码 sn -p 创建功能标志

  var client = new ConfigurationClient(_configuration.GetConnectionString("AppConnectionString"));
  var settingToCreate = new ConfigurationSetting(Key, Value);
  settingToCreate.ContentType = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8";
  client.SetConfigurationSetting(settingToCreate);

请注意 ConfigurationSetting 上的 ContentType 属性与代码 sn-p 中提供的功能标志相同。

Key = .appconfig.featureflag/"your feature flag name"

Value = 
    "id": "your feature flag name",
    "description": "",
    "enabled": true,
    "conditions": 
        "client_filters": [
            
                "name": "Microsoft.Targeting",
                "parameters": 
                    "Audience": 
                        "Users": [],
                        "Groups": [],
                        "DefaultRolloutPercentage": 50
                    
                
            
        ]
    

Find Medium Post here for entire end-to-end implementation

【讨论】:

【参考方案3】:

以编程方式获取和设置 azure 功能标志状态(在自动化测试中很有用)

特征标志json键值格式如下。

    
        "id": "Feature-EnableClientRegistration",
        "description": "Enables client registration journey if selected otherwise disables.",
        "enabled": false,
        "conditions": null
    
    创建一个模型来存储这个 Json 数据 创建通用服务以访问功能标志状态
    public class FeatureManagementService : IFeatureManagementService
        
            private const string AZURE_APP_CONFIG_KEY_IDENTIFIER = ".appconfig.featureflag";
    
            private const string AZURE_FEATURE_MANAGEMENT_CONTENT_TYPE = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8";
    
            internal class FeatureFlagModel
            
                [JsonPropertyName("id")]
                public string Id  get; set; 
    
                [JsonPropertyName("description")]
                public string Description  get; set; 
    
                [JsonPropertyName("enabled")]
                public bool Enabled  get; set; 
    
                [JsonPropertyName("conditions")]
                public object Conditions  get; set; 
            
    
            public string Label  get; init; 
    
            private readonly ConfigurationClient client;
    
            public FeatureManagementService(string connectionStr, string label)
            
                client = new ConfigurationClient(connectionStr);
    
                Label = label;
            
    
            public bool GetFlagStatus(string featureFlagId)
            
                var key = GetAppConfigFeatureFlagKey(featureFlagId);
    
                var configSettings = GetAppConfigFeatureFlagSetting(key, string.Empty, Label);
    
                var response = client.GetConfigurationSetting(configSettings);
    
                var model = JsonSerializer.Deserialize<FeatureFlagModel>(response.Value.Value);
    
                return model.Enabled;
            
    
            public void SetFlagStatus(string featureFlagId, bool isEnabled)
            
                var key = GetAppConfigFeatureFlagKey(featureFlagId);
    
                var model = new FeatureFlagModel
                
                    Id = featureFlagId,
                    Description = FeatureConstants.GetDescription(featureFlagId),
                    Enabled = isEnabled,
                    Conditions = default
                ;
    
                var value = JsonSerializer.Serialize(model);
    
                var configSettings = GetAppConfigFeatureFlagSetting(key, value, Label);
    
                client.SetConfigurationSetting(configSettings);
            
    
            private static string GetAppConfigFeatureFlagKey(string featureFlagId) =>
                $"AZURE_APP_CONFIG_KEY_IDENTIFIER/featureFlagId";
    
            private static ConfigurationSetting GetAppConfigFeatureFlagSetting(
                string key, string value, string label) =>
                new(key, value, label)
                
                    ContentType = AZURE_FEATURE_MANAGEMENT_CONTENT_TYPE
                ;
           
    创建一个常量类
    public static class FeatureConstants
    
        [Description("Enables client registration journey if selected otherwise disables.")]
        public const string FeatureEnableClientRegistration = "Feature-EnableClientRegistration";
    
        public static string GetDescription(string featureFlagId)
        
            var comparer = StringComparison.InvariantCultureIgnoreCase;
    
            var featureFieldInfo = typeof(FeatureConstants)
                .GetFields(BindingFlags.Public | BindingFlags.Static)
                .SingleOrDefault(f => featureFlagId.Equals((string)f.GetValue(null), comparer));
    
            var featureDescription = featureFieldInfo?
                .GetCustomAttribute<DescriptionAttribute>(true)?
                .Description ?? string.Empty;
    
            return featureDescription;
        
    
    我们可以通过传递 azure appconfig store 连接字符串来消费,如下所示:
    var connectionStr = "Endpoint=https://*<placeholder>*.azconfig.io;Id=*<placeholder>*;Secret=*<placeholder>*";
    
    var service = new FeatureManagementService(connectionStr, "Stagging");
    
    var isEnabled = service.GetFlagStatus("Feature-EnableClientRegistration");
    
    service.SetFlagStatus("Feature-EnableClientRegistration", !isEnabled);

【讨论】:

以上是关于在 Azure 应用配置中设置/添加功能标志的主要内容,如果未能解决你的问题,请参考以下文章

我可以在 iOS 应用程序中设置设备级代理吗?如何?

在 azure 中设置 webapp %PATH% 环境变量

无法在 Azure 中设置 jsonSerialization

Azure 应用服务在 ApplicationHost.config 中设置自定义 ConnectionTimeout

如何在 Azure devops 中设置构建管道 [关闭]

在android的sqlite数据库中设置标志