/// <summary> /// create /// </summary> /// <param name="configPath"></param> /// <param name="sectionName"></param> /// <param name="serviceName"></param> /// <returns></returns> static public Tuple <ThriftClient, object> Create(string configPath, string sectionName, string serviceName) { if (string.IsNullOrEmpty(sectionName)) { throw new ArgumentNullException("sectionName"); } //load config... Config.ThriftConfigSection config = null; if (string.IsNullOrEmpty(configPath)) { config = ConfigurationManager.GetSection(sectionName) as Config.ThriftConfigSection; } else { config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configPath) }, ConfigurationUserLevel.None).GetSection(sectionName) as Config.ThriftConfigSection; } foreach (Config.Service service in config.Services) { if (service.Name != serviceName) { continue; } var client = new ThriftClient(service, service.SocketBufferSize, service.MessageBufferSize, service.SendTimeout, service.ReceiveTimeout); client.Start(); return(Tuple.Create(client, Type.GetType(service.Client, true) .GetConstructor(new Type[] { typeof(IThriftClient) }) .Invoke(new object[] { client }))); } return(null); }
/// <summary> /// 获取配置 /// </summary> /// <param name="sectionName"></param> /// <param name="serviceName"></param> /// <returns></returns> private Config.Service GetServiceConfig() { if (string.IsNullOrEmpty(_sectionName)) { throw new ArgumentNullException("sectionName"); } if (string.IsNullOrEmpty(_serviceName)) { throw new ArgumentNullException("serviceName"); } Config.ThriftConfigSection config = null; if (string.IsNullOrEmpty(_configPath)) { config = ConfigurationManager.GetSection(_sectionName) as Config.ThriftConfigSection; } else { config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _configPath) }, ConfigurationUserLevel.None).GetSection(_sectionName) as Config.ThriftConfigSection; } foreach (Config.Service service in config.Services) { if (service.Name != _serviceName) { continue; } if (string.IsNullOrEmpty(service.SpaceName)) { service.SpaceName = _spaceName; } if (string.IsNullOrEmpty(service.ClassName)) { service.ClassName = _className; } if (service.ZookeeperConfig == null || service.ZookeeperConfig.Host == "") { return(service); } bool isConnZk = SetServerConfig(service); _firstGetConfig = false; if (!isConnZk) { new System.Threading.Thread(() => { while (!isConnZk) { System.Threading.Thread.Sleep(service.ZookeeperConfig.ConnectInterval); isConnZk = SetServerConfig(service); } }).Start(); } return(service); } return(null); }