参照了:https://www.cnblogs.com/xichji/p/11324893.html
Apollo默认有一个“SampleApp”应用,“DEV”环境 和 “timeout” KEY。
环境:VS2015,.net framework 4.5。
1.nuget 下载 :Com.Ctrip.Framework.Apollo.ConfigurationManager 2.3.0。最新的2.6.2 我没安装成功。
2.修改web.config 增加:
<add key="Apollo.AppID" value="SampleApp" />
<add key="Apollo.Env" value="DEV" />
<add key="Apollo.MetaServer" value="http://192.168.192.100:8080" />
3.新建IApolloConfiguration
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MyCompany.MyProject.Infrastructure { /// <summary> /// 配置抽象接口。 /// </summary> public interface IApolloConfiguration { /// <summary> /// 配置更改回调事件。 /// </summary> //event EventHandler<ConfigurationChangeEventArgs> ConfigChanged; /// <summary> /// 获取配置项。 /// </summary> /// <param name="key">键</param> /// <param name="namespaces">命名空间集合</param> /// <returns></returns> string GetValue(string key, params string[] namespaces); /// <summary> /// 获取配置项。 /// </summary> /// <typeparam name="TValue">值类型</typeparam> /// <param name="key">键</param> /// <param name="namespaces">命名空间集合</param> /// <returns></returns> TValue GetValue<TValue>(string key, params string[] namespaces); /// <summary> /// 获取配置项,如果值为 <see cref="null"/> 则取参数 <see cref="defaultValue"/> 值。 /// </summary> /// <param name="key">键</param> /// <param name="defaultValue">默认值</param> /// <param name="namespaces">命名空间集合</param> /// <returns></returns> string GetDefaultValue(string key, string defaultValue, params string[] namespaces); /// <summary> /// 获取配置项,如果值为 <see cref="null"/> 则取参数 <see cref="defaultValue"/> 值。 /// </summary> /// <typeparam name="TValue">值类型</typeparam> /// <param name="key">键</param> /// <param name="defaultValue">默认值</param> /// <param name="namespaces">命名空间集合</param> /// <returns></returns> TValue GetDefaultValue<TValue>(string key, TValue defaultValue, params string[] namespaces); } }
4.新建ApolloConfiguration
using Com.Ctrip.Framework.Apollo; using Com.Ctrip.Framework.Apollo.Model; using System; using System.Globalization; namespace MyCompany.MyProject.Infrastructure { public class ApolloConfiguration : IApolloConfiguration { private readonly string _defaultValue = null; //public event EventHandler<ConfigurationChangeEventArgs> ConfigChanged; private IConfig GetConfig(params string[] namespaces) { var config = namespaces == null || namespaces.Length == 0 ? ApolloConfigurationManager.GetAppConfig().GetAwaiter().GetResult() : ApolloConfigurationManager.GetConfig(namespaces).GetAwaiter().GetResult(); config.ConfigChanged += Config_ConfigChanged; return config; } private void Config_ConfigChanged(IConfig sender, ConfigChangeEventArgs args) { foreach (string key in args.ChangedKeys) { ConfigChange change = args.GetChange(key); // Console.WriteLine("Change - key: {0}, oldValue: {1}, newValue: {2}, changeType: {3}", change.PropertyName, change.OldValue, change.NewValue, change.ChangeType); } } public string GetValue(string key, params string[] namespaces) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var config = GetConfig(namespaces); return config.GetProperty(key, _defaultValue); } public TValue GetValue<TValue>(string key, params string[] namespaces) { var value = GetValue(key, namespaces); return value == null ? default(TValue) : (TValue)Convert.ChangeType(value, typeof(TValue), CultureInfo.InvariantCulture); } public string GetDefaultValue(string key, string defaultValue, params string[] namespaces) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullException(nameof(key)); var config = GetConfig(namespaces); return config.GetProperty(key, defaultValue); } public TValue GetDefaultValue<TValue>(string key, TValue defaultValue, params string[] namespaces) { var value = GetDefaultValue(key, defaultValue, namespaces); return value == null ? default(TValue) : (TValue)Convert.ChangeType(value, typeof(TValue), CultureInfo.InvariantCulture); } } }
5.使用:
IApolloConfiguration config = new ApolloConfiguration(); string aa = config.GetValue("timeout");
.