Example #1
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // create a configuration manager with single source
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }
        public virtual void TestDemo()
        {
            // create an apollo config
            IConfig appConfig = ApolloConfigurationManager.GetAppConfig().Result;

            // create a scf apollo configuration source
            ApolloConfigurationSourceConfig sourceConfig = new ApolloConfigurationSourceConfig.Builder()
                                                           .SetName("apollo-source").SetApolloConfig(appConfig).Build();
            ApolloConfigurationSource source = new ApolloConfigurationSource(sourceConfig);

            // create scf manager & properties facade tool
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("my-app")
                                                       .AddSource(1, source).Build();
            IConfigurationManager manager    = ConfigurationManagers.NewManager(managerConfig);
            StringProperties      properties = new StringProperties(manager);

            // use properties
            IProperty <string, string> appName = properties.GetStringProperty("app.name", "unknown");

            Console.WriteLine("app name: " + appName.Value);

            // add listener for dynamic property
            IProperty <string, int?> requestTimeout = properties.GetIntProperty("request.timeout", 1000);

            Console.WriteLine("request timeout: " + requestTimeout.Value);
            requestTimeout.OnChange += (o, e) => Console.WriteLine("do something");
        }
Example #3
0
        private static void InitConfig()
        {
            // create a non-dynamic K/V (string/string) configuration source
            PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources
                                                                   .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties")
                                                                   .Build();
            IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig);

            // crate a dynamic K/V (string/string) configuration source
            _systemPropertiesSource = StringPropertySources.NewMemoryDictionarySource("system-property");

            // create a configuration manager with 2 sources, system property has higher priority then app.properties
            ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app")
                                                       .AddSource(1, source).AddSource(2, _systemPropertiesSource).Build();
            IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig);

            // Add a log listener
            manager.OnChange += (o, e) =>
                                Console.WriteLine("\nproperty {0} changed, old value: {1}, new value: {2}, changeTime: {3}",
                                                  e.Property, e.OldValue, e.NewValue, e.ChangeTime);

            // create a StringProperties facade tool
            _properties = new StringProperties(manager);

            // default to null
            _appId = _properties.GetStringProperty("app.id");

            // default to "unknown"
            _appName = _properties.GetStringProperty("app.name", "unknown");
            // Add change listener to app.name
            _appName.OnChange += (o, e) => Console.WriteLine("\napp.name changed, maybe we need do something");

            // default to empty list
            _userList = _properties.GetListProperty("user.list", new List <string>());

            // default to empty map
            _userData = _properties.GetDictionaryProperty("user.data", new Dictionary <string, string>());

            // default to 1000, if value in app._properties is invalid, ignore the invalid value
            _sleepTime = _properties.GetIntProperty("sleep.time", 1000, v => v < 0 ? null : v);

            // custom type property
            _myCustomData = _properties.GetProperty("my-custom-type-property", MyCustomType.Converter);
        }