/// <summary> /// 从连接池返回一个可用连接 /// </summary> /// <returns></returns> public ThriftClient <T> Pop() { if (_count >= _config.ServiceConfig.MaxConnectionsNum) { ThriftLog.Error("连接池达到最大数:" + _count); return(null); } if (_hostCount == 0) { return(null); } _config.ServiceConfig.Host = _host[_hostIndex % _hostCount]; var item = ThriftClientFactory.Create(_config.ServiceConfig, false); if (item == null) { return(null); } var client = new ThriftClient <T>(Tuple.Create(item.Item1, item.Item2 as T), this, item.Item3, ""); _count++; return(client); }
/// <summary> /// 创建连接 /// </summary> /// <param name="num"></param> private void CreateConnections(int num) { lock (_lockHelper) { for (int i = 0; i < num; ++i) { if (_count >= _config.ServiceConfig.MaxConnectionsNum) { return; } var item = ThriftClientFactory.Create(_config.ServiceConfig, true); if (item == null) { return; } var token = System.Guid.NewGuid().ToString(); var client = new ThriftClient <T>(Tuple.Create(item.Item1, item.Item2 as T), this, item.Item3, token); _clients.Enqueue(client); _count++; ThriftLog.Info("连接池数:" + _count); } } }
/// <summary> /// 更新连接池 /// </summary> public void UpdatePool() { ThriftLog.Info("UpdatePool:" + _config.ServiceConfig.Host); _host = _config.ServiceConfig.Host.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); _hostCount = _host.Length; _hostIndex = 0; }
public void Log(TraceLevel severity, string className, string message, Exception exception) { if (exception == null) { ThriftLog.Info($"Zookeeper {severity.ToString()} {className} {message} "); } else { ThriftLog.Info($"Zookeeper {severity.ToString()} {className} {message} {exception.Message} {exception.StackTrace}"); } }
/// <summary> /// 回收一个连接 /// </summary> /// <param name="client"></param> public void Push(Tuple <TTransport, T> client, string host, string token) { try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; }
/// <summary> /// 回收一个连接 /// </summary> /// <param name="client"></param> public void Push(Tuple <TTransport, T> client, string host, string token) { lock (_lockPopHelper) { _listPop.Remove(token); } //错误的连接 if (_hashErrorPop.Contains(token)) { ThriftLog.Info("回收错误的连接:" + token); _hashErrorPop.Remove(token); try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; return; } //超过最大空闲 if (_clients.Count() >= _config.ServiceConfig.MaxConnectionsIdle) { ThriftLog.Info($"当前连接数:{_count},超过最大空闲数:{_config.ServiceConfig.MaxConnectionsIdle},如果此条信息过多,请增加最大空闲数"); try { client.Item1.Close(); client.Item1.Dispose(); client = null; } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); } _count--; return; } _clients.Enqueue(new ThriftClient <T>(client, this, host, token)); }
public override async Task process(WatchedEvent watchedEvent) { Console.WriteLine("WatchedEvent:" + watchedEvent.getState().ToString() + ":" + watchedEvent.get_Type().ToString()); if (watchedEvent.getState() == KeeperState.Expired) { ThriftLog.Info(" 重新连接zk"); await _zk.closeAsync(); _zk = null; _serviceConfig = GetServiceConfig(); return; } try { if (watchedEvent.get_Type() == EventType.NodeChildrenChanged) { var data = await _zk.getChildrenAsync(_serviceConfig.ZookeeperConfig.NodeParent, this); var children = data.Children; if (children != null && children.Count > 0) { _serviceConfig.Host = string.Join(",", children); } else { _serviceConfig.Host = _defaultHost; } if (_updateHostDelegate != null) { _updateHostDelegate(); } } } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); _zk = null; _serviceConfig = GetServiceConfig(); ThriftLog.Info(" 重新连接zk2"); } return; }
private bool SetServerConfig(Config.Service service) { try { if (_zk == null) { _zk = new ZooKeeper(service.ZookeeperConfig.Host, service.ZookeeperConfig.SessionTimeout, this); int count = 0; while (_zk.getState() != ZooKeeper.States.CONNECTED) { if (count++ > 50) { throw new Exception("ZooKeeper 连接失败"); } System.Threading.Thread.Sleep(100); } } _defaultHost = service.Host; var children = _zk.getChildrenAsync(service.ZookeeperConfig.NodeParent, this).Result.Children; if (children != null && children.Count > 0) { service.Host = string.Join(",", children); } if (!_firstGetConfig) //首次连接,不需要执行更新方法。 { if (_updateHostDelegate != null) { _updateHostDelegate(); } } return(true); } catch (Exception ex) { ThriftLog.Error(ex.Message + ex.StackTrace); return(false); } }
/// <summary> /// 更新连接池,删除不可用的连接 /// </summary> public void UpdatePool() { lock (_lockHelper) { int count = _clients.Count; for (int i = 0; i < count; i++) { ThriftClient <T> client = null; if (!_clients.TryDequeue(out client)) { break; } if (!_config.ServiceConfig.Host.Contains(client.Host)) { ThriftLog.Info("删除不可用的连接:" + client.Host); client.Destroy(); //删除不可用的连接 } else { client.Push(); //主动回收 } } } //回收连接时进行过滤 lock (_lockPopHelper) { _hashErrorPop = new HashSet <string>(); foreach (var item in _listPop) { if (!_config.ServiceConfig.Host.Contains(item.Value)) { _hashErrorPop.Add(item.Key); } } } }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <returns></returns> static public Tuple <TTransport, object, string> Create(Config.Service config, bool isPool) { try { string[] url = config.Host.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (url.Length == 0) { return(null); } List <string> listUri = new List <string>(); foreach (string item in url) { var uri = item.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); var length = uri.Length > 1 ? int.Parse(uri[1]) : 1; int i = 0; while (i++ < Math.Min(1, length)) { listUri.Add(uri[0]); } } if (listUri.Count == 0) { return(null); } int num = new Random().Next(0, listUri.Count); string host = listUri[num]; if (isPool) { ThriftLog.Info("创建连接:" + config.Host + "--" + host); } TTransport transport = new TSocket(host.Split(':')[0], int.Parse(host.Split(':')[1]), config.Timeout); if (config.IsMult) { TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, config.ClassName); string assemblyName = config.SpaceName; if (!string.IsNullOrEmpty(config.AssemblyName)) { assemblyName = config.AssemblyName; } return(Tuple.Create(transport, Type.GetType($"{config.SpaceName}.{config.ClassName}+Client,{assemblyName}", true) .GetConstructor(new Type[] { typeof(TProtocol) }) .Invoke(new object[] { mp1 }), host)); } else { TProtocol protocol = new TBinaryProtocol(transport); string assemblyName = config.SpaceName; if (!string.IsNullOrEmpty(config.AssemblyName)) { assemblyName = config.AssemblyName; } return(Tuple.Create(transport, Type.GetType($"{config.SpaceName}.{config.ClassName}+Client,{assemblyName}", true) .GetConstructor(new Type[] { typeof(TProtocol) }) .Invoke(new object[] { protocol }), host)); } } catch (Exception ex) { throw new Exception("ThriftClientFactory 创建实例异常:" + ex.Message); } }