private bool _firstGetConfig = true; //第一次加载 public ThriftClientConfig(string sectionName, string serviceName, Action updateHostDelegate, string spaceName = "", string className = "") { _spaceName = spaceName; _className = className; _configPath = ConfigurationManager.AppSettings["ThriftClientConfigPath"]; _sectionName = sectionName; _serviceName = serviceName; _serviceConfig = GetServiceConfig(); _updateHostDelegate = updateHostDelegate; }
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; }
/// <summary> /// new /// </summary> /// <param name="config"></param> /// <param name="socketBufferSize"></param> /// <param name="messageBufferSize"></param> /// <param name="millisecondsSendTimeout"></param> /// <param name="millisecondsReceiveTimeout"></param> public ThriftClient(Config.Service config, int socketBufferSize, int messageBufferSize, int millisecondsSendTimeout, int millisecondsReceiveTimeout) : base(socketBufferSize, messageBufferSize, millisecondsSendTimeout, millisecondsReceiveTimeout) { if (config == null) throw new ArgumentNullException("config"); this._config = config; if (this._config.Discovery != null && this._config.Discovery.EndPoints != null && this._config.Discovery.EndPoints.Count > 0) { var arrEndPoints = this._config.Discovery.EndPoints.ToArray(); foreach (var p in arrEndPoints) this.TryRegisterEndPoint(string.Concat(p.Address.ToString(), ":", p.Port.ToString()), new System.Net.EndPoint[] { p }); } }
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> /// new /// </summary> /// <param name="config"></param> /// <param name="socketBufferSize"></param> /// <param name="messageBufferSize"></param> /// <param name="millisecondsSendTimeout"></param> /// <param name="millisecondsReceiveTimeout"></param> public ThriftClient(Config.Service config, int socketBufferSize, int messageBufferSize, int millisecondsSendTimeout, int millisecondsReceiveTimeout) : base(socketBufferSize, messageBufferSize, millisecondsSendTimeout, millisecondsReceiveTimeout) { if (config == null) { throw new ArgumentNullException("config"); } this._config = config; if (this._config.Discovery != null && this._config.Discovery.EndPoints != null && this._config.Discovery.EndPoints.Count > 0) { var arrEndPoints = this._config.Discovery.EndPoints.ToArray(); foreach (var p in arrEndPoints) { this.TryRegisterEndPoint(string.Concat(p.Address.ToString(), ":", p.Port.ToString()), new System.Net.EndPoint[] { p }); } } }
/// <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); } }