/// <summary> /// 创建一个监视路径 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="nodeData">节点数据</param> /// <param name="disconfNodeType">节点类型</param> /// <returns></returns> private string CreateMonitorPath(string nodeName, string nodeData, DisconfNodeType disconfNodeType) { string rootNode = _zooKeeperClient.DisconfWebApi.GetZooKeeperRootNode(); string clientRootZooPath = PathManager.GetBasePath(rootNode, DisconfClientSettings.AppId, DisconfClientSettings.Environment, DisconfClientSettings.Version); _zooKeeperClient.CreatePersistentPath(clientRootZooPath, Utils.LocalIp); string monitorPath; if (disconfNodeType == DisconfNodeType.File) { string clientDisconfFileZooPath = PathManager.GetFilePath(clientRootZooPath); _zooKeeperClient.CreatePersistentPath(clientDisconfFileZooPath, Utils.LocalIp); monitorPath = PathManager.JoinPath(clientDisconfFileZooPath, nodeName); } else { string clientDisconfItemZooPath = PathManager.GetItemPath(clientRootZooPath); _zooKeeperClient.CreatePersistentPath(clientDisconfItemZooPath, Utils.LocalIp); monitorPath = PathManager.JoinPath(clientDisconfItemZooPath, nodeName); } _zooKeeperClient.CreatePersistentPath(monitorPath, ""); CreateTempPath(monitorPath, nodeData); return(monitorPath); }
/// <summary> /// 监视路径 /// </summary> /// <param name="nodeName">节点名称</param> /// <param name="nodeData">节点值</param> /// <param name="disconfNodeType">节点类型</param> public void WatchPath(string nodeName, string nodeData, DisconfNodeType disconfNodeType) { string monitorPath = CreateMonitorPath(nodeName, nodeData, disconfNodeType); NodeWatcher nodeWatcher = new NodeWatcher(_zooKeeperClient, nodeName, nodeData, disconfNodeType, monitorPath); nodeWatcher.Monitor(); }
/// <summary> /// 构建节点监视器 /// </summary> /// <param name="zooKeeperClient"></param> /// <param name="nodeName">节点名称</param> /// <param name="nodeData">节点值</param> /// <param name="disconfNodeType">节点类型</param> /// <param name="monitorPath">监视路径</param> public NodeWatcher(ZooKeeperClient zooKeeperClient, string nodeName, string nodeData, DisconfNodeType disconfNodeType, string monitorPath) { _zooKeeperClient = zooKeeperClient; _monitorPath = monitorPath; _nodeName = nodeName; _nodeData = nodeData; _disconfNodeType = disconfNodeType; }
public void AddConfigItemContentApiResult(string name, string data, DisconfNodeType disconfNodeType = DisconfNodeType.Item, string version = "1.0") { ConfigItem configItem = new ConfigItem { Name = name, DisconfNodeType = disconfNodeType, Data = data, UpdateTime = version }; Configs.Add(configItem); }
/// <summary> /// 创建本地存储配置项 /// </summary> /// <param name="name">配置项名称</param> /// <param name="disconfNodeType">配置项类型</param> /// <param name="version">版本号(对应配置中的中的配置项的最后一次更新时间值)</param> /// <param name="data">配置项值</param> /// <returns></returns> private static ConfigStorageItem CreateConfigStorageItem(string name, DisconfNodeType disconfNodeType, string version, string data = null) { if (string.IsNullOrWhiteSpace(name)) { return(null); } if (string.IsNullOrWhiteSpace(data)) { if (disconfNodeType == DisconfNodeType.File) { data = _disconfWebApi.GetConfigFileContent(name); } else { data = _disconfWebApi.GetConfigItemContent(name); } } ConfigStorageItem configStorageItem = new ConfigStorageItem { Name = name, Data = data, Version = version, LastUpdateTime = DateTime.Now }; if (!DisconfClientSettings.DisableZooKeeper) { ThreadPool.QueueUserWorkItem((obj) => { try { WatcherManager watcherManager = new WatcherManager(_zooKeeperClient); watcherManager.WatchPath(name, data, disconfNodeType); } catch (Exception ex) { LogManager.GetLogger() .Error( string.Format( "DisconfClient.ConfigStorageManager.CreateConfigStorageItem.WatchPath,Exception:{0}", ex)); } }); } return(configStorageItem); }
public void DefalutDataConverterTest() { IDataConverter dataConverter = new DefalutDataConverter(); string result1String = (string)dataConverter.Parse(typeof(string), Constant.DefalutDataConverterTestValue1); Assert.AreEqual(result1String, Constant.DefalutDataConverterTestValue1); int result2Int = (int)dataConverter.Parse(typeof(int), Constant.DefalutDataConverterTestValue2); Assert.AreEqual(int.Parse(Constant.DefalutDataConverterTestValue2), result2Int); long result2Long = (long)dataConverter.Parse(typeof(long), Constant.DefalutDataConverterTestValue2); Assert.AreEqual(long.Parse(Constant.DefalutDataConverterTestValue2), result2Long); uint result2Uint = (uint)dataConverter.Parse(typeof(uint), Constant.DefalutDataConverterTestValue2); Assert.AreEqual(uint.Parse(Constant.DefalutDataConverterTestValue2), result2Uint); ulong result2Ulong = (ulong)dataConverter.Parse(typeof(ulong), Constant.DefalutDataConverterTestValue2); Assert.AreEqual(ulong.Parse(Constant.DefalutDataConverterTestValue2), result2Ulong); float result3Float = (float)dataConverter.Parse(typeof(float), Constant.DefalutDataConverterTestValue3); Assert.AreEqual(float.Parse(Constant.DefalutDataConverterTestValue3), result3Float); double result3Double = (double)dataConverter.Parse(typeof(double), Constant.DefalutDataConverterTestValue3); Assert.AreEqual(double.Parse(Constant.DefalutDataConverterTestValue3), result3Double); decimal result3Decimal = (decimal)dataConverter.Parse(typeof(decimal), Constant.DefalutDataConverterTestValue3); Assert.AreEqual(decimal.Parse(Constant.DefalutDataConverterTestValue3), result3Decimal); DisconfNodeType result4Enum = (DisconfNodeType)dataConverter.Parse(typeof(DisconfNodeType), Constant.DefalutDataConverterTestValue4); Assert.AreEqual((DisconfNodeType)int.Parse(Constant.DefalutDataConverterTestValue4), result4Enum); DisconfNodeType result9Enum = (DisconfNodeType)dataConverter.Parse(typeof(DisconfNodeType), Constant.DefalutDataConverterTestValue9); Assert.AreEqual(Enum.Parse(typeof(DisconfNodeType), Constant.DefalutDataConverterTestValue9, true), result9Enum); Guid result5Guid = (Guid)dataConverter.Parse(typeof(Guid), Constant.DefalutDataConverterTestValue5); Assert.AreEqual((Guid)Guid.Parse(Constant.DefalutDataConverterTestValue5), result5Guid); Type resutl6Type = (Type)dataConverter.Parse(typeof(Type), Constant.DefalutDataConverterTestValue6); Assert.AreEqual((Type)Type.GetType(Constant.DefalutDataConverterTestValue6), resutl6Type); bool result7Bool = (bool)dataConverter.Parse(typeof(bool), Constant.DefalutDataConverterTestValue7); Assert.AreEqual((bool)bool.Parse(Constant.DefalutDataConverterTestValue7), result7Bool); char result8Char = (char)dataConverter.Parse(typeof(char), Constant.DefalutDataConverterTestValue8); Assert.AreEqual((char)char.Parse(Constant.DefalutDataConverterTestValue8), result8Char); DateTime result10DateTime = (DateTime)dataConverter.Parse(typeof(DateTime), Constant.DefalutDataConverterTestValue10); Assert.AreEqual((DateTime)DateTime.Parse(Constant.DefalutDataConverterTestValue10), result10DateTime); IList <string> list = (IList <string>)dataConverter.Parse(typeof(IList <string>), Constant.PropertiesDataConverterTestValue1); Assert.IsNotNull(list); Assert.AreEqual(2, list.Count); IDictionary <string, string> dic = (IDictionary <string, string>)dataConverter.Parse(typeof(IDictionary <string, string>), Constant.PropertiesDataConverterTestValue1); Assert.IsNotNull(dic); Assert.AreEqual("127.0.0.1", dic["Host"]); Assert.AreEqual("81", dic["Port"]); }
static void Main(string[] args) { try { //请将这个初始化的方法调用放在你应用的入口处 ConfigManager.Init(); //在应用的入口处注册定制化的类型转换器,如果是配置类的方式,则可以直接在Disconf特性中设置,例见:PropertiesDemoConfig DataConverterManager.RegisterDataConverter("TestMyList", new MyListDataConverter()); //关于DataConverter //如果你需要当某个节点的值发生变化时获行通知,请在应用的入口处注册配置节点值变更时的回调通知类 ConfigCallbackManager.RegisterCallback("TestMyList", new TestMyListCallback()); //可以将一些配置项归类,定义在一个配置类中 TypeTestConfig typeTestConfig = ConfigManager.GetConfigClass <TypeTestConfig>(); //也可以直接以Key-Value的形式来调用 string testString = ConfigManager.GetConfigValue <string>("TestString"); int testInt = ConfigManager.GetConfigValue <int>("TestInt"); long testLong = ConfigManager.GetConfigValue <long>("TestLong"); uint testUint = ConfigManager.GetConfigValue <uint>("TestUint"); ulong testUlong = ConfigManager.GetConfigValue <ulong>("TestUlong"); float testFloat = ConfigManager.GetConfigValue <float>("TestFloat"); double testDouble = ConfigManager.GetConfigValue <double>("TestDouble"); decimal testDecimal = ConfigManager.GetConfigValue <decimal>("TestDecimal"); DisconfNodeType testEnumInt = ConfigManager.GetConfigValue <DisconfNodeType>("TestEnumInt"); DisconfNodeType testEnumString = ConfigManager.GetConfigValue <DisconfNodeType>("TestEnumString"); Guid testGuid = ConfigManager.GetConfigValue <Guid>("TestGuid"); Type testType = ConfigManager.GetConfigValue <Type>("TestType"); bool testBool = ConfigManager.GetConfigValue <bool>("TestBool"); char testChar = ConfigManager.GetConfigValue <char>("TestChar"); DateTime testDateTime = ConfigManager.GetConfigValue <DateTime>("TestDateTime"); IList <string> testStringList = ConfigManager.GetConfigValue <IList <string> >("TestStringList"); IDictionary <string, string> testStringDictionary = ConfigManager.GetConfigValue <IDictionary <string, string> >("TestStringDictionary"); //可以为某一个配置文件定义一个配置类 AppSettingsDemoConfig appSettingsDemoConfig = ConfigManager.GetConfigClass <AppSettingsDemoConfig>(); //所有的配置项都可以通过这样的方法直接取出配置项中的字符串值 string appSettingsValue = ConfigManager.GetConfigValue <string>("redis.config"); //如果配置项的名称是以.config,.properties扩展名结尾的,或注册过DictionaryDataConverter的则可以转换为Dictionary<string, string>类型 IDictionary <string, string> appSettingsValueToDictionary = ConfigManager.GetConfigValue <IDictionary <string, string> >("redis.config"); //对IDictionary<string, string>类型做了“Get<T>'扩展方法,类型的转换使用的是DefalutDataConverter string host = appSettingsValueToDictionary.Get <string>("Host"); int port = appSettingsValueToDictionary.Get <int>("Port"); //以.json扩展名结尾的,或注册过JsonDataConverter的则可以转换为指定的类型 JsonDemoConfig jsonDemoConfig = ConfigManager.GetConfigClass <JsonDemoConfig>(); string json = ConfigManager.GetConfigValue <string>("redis.json"); JsonDemoConfig jsonDemoConfig2 = ConfigManager.GetConfigValue <JsonDemoConfig>("redis.json"); //以.properties扩展名结尾的,或注册过PropertiesDataConverter的则可以进行类型转换 PropertiesDemoConfig propertiesDemoConfig = ConfigManager.GetConfigClass <PropertiesDemoConfig>(); string properties = ConfigManager.GetConfigValue <string>("redis.properties"); PropertiesDemoConfig propertiesDemoConfig2 = ConfigManager.GetConfigValue <PropertiesDemoConfig>("redis.properties"); //调用本地appSettings中的配置项,类型的转换使用的是DefalutDataConverter string appId = ConfigManager.AppSettings <string>("AppId"); int refreshTime = ConfigManager.AppSettings <int>("DisconfClient.RefreshTime", 15); } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }