/// <summary> /// 为一组主机初始化实例 /// </summary> /// <param name="server">服务名</param> /// <param name="hostOrIps">主机,格式为: ipOrHost:port,</param> /// <exception cref="ArgumentNullException">hostOrIps</exception> protected Client(string server, string[] hostOrIps) { this.name = this.GetType().Name; this.server = server; this.logManager = new LogManager(this.Name); if (hostOrIps == null || hostOrIps.Length == 0) { throw new ArgumentNullException("hostOrIps"); } // var clientSessionPoolItems = new ClientSessionPoolMember[hostOrIps.Length]; string[] item; int port; for (int i = 0, l = hostOrIps.Length; i < l; i++) { item = hostOrIps[i].Split(':'); if (item.Length != 2) { throw new ArgumentOutOfRangeException("hostOrIps", "format must be ipOrHost:port"); } int.TryParse(item[1], out port); if (port == 0) { throw new ArgumentOutOfRangeException("hostOrIps", "format must be ipOrHost:port"); } clientSessionPoolItems[i] = new ClientSessionPoolMember(item[0], port); } // this.socketPool = new Pool <ClientSession>(this.PoolMemberThreadSize, clientSessionPoolItems, HASH_VNODE_COUNT); }
////创建新实例发生异常时 & 仅 registry 时注册 //private void socketPool_NewInstanceException(object sender, PoolNewInstanceExceptionEventArgs e) //{ // //触发节点更新 // this.registry.Update(); //} ////注册服务节点发生变更时 //private void RegistryNodeChanged(object sender, EventArgs e) //{ // var hash = this.socketPool.HashVNodeCount; // var nodes = this.registry.GetNodes(); // var ipcount = nodes.Length; // var clientSessionPoolItems = new ClientSessionPoolMember[ipcount]; // for (int i = 0; i < ipcount; i++) // clientSessionPoolItems[i] = new ClientSessionPoolMember(nodes[i].Host, nodes[i].Port); // // // if (this.obsoleteSocketPoolList == null) // this.obsoleteSocketPoolList = new List<Pool<ClientSession>>(); // lock (this.obsoleteSocketPoolList) // this.obsoleteSocketPoolList.Add(this.socketPool); // var disposePool = this.socketPool; // //use new ppol // this.socketPool = new Pool<ClientSession>(this.PoolMemberThreadSize, clientSessionPoolItems, HASH_VNODE_COUNT); // this.socketPool.NewInstanceException += this.socketPool_NewInstanceException; // //log // if (this.logManager.Message.Enable) // { // this.logManager.Message.WriteTimeLine("registry load node: " + Adf.ConvertHelper.ArrayToString<CsRegistryNode>(nodes, ",", n => { return n.Host + ":" + n.Port; })); // } // //旧有节点资源释放 // System.Threading.ThreadPool.QueueUserWorkItem(state => // { // while (disposePool.RuningCount > 0) // { // System.Threading.Thread.Sleep(1000); // } // try // { // disposePool.Dispose(); // } // catch { } // finally // { // lock (this.obsoleteSocketPoolList) // this.obsoleteSocketPoolList.Remove(disposePool); // } // }); //} /// <summary> /// 为单个主机初始化实例 /// </summary> /// <param name="server">服务名</param> /// <param name="hostOrIp">主机</param> /// <param name="port">端口</param> protected Client(string server, string hostOrIp, int port) { this.name = this.GetType().Name; this.server = server; this.logManager = new LogManager(this.Name); // var clientSessionPoolItems = new ClientSessionPoolMember[1]; clientSessionPoolItems[0] = new ClientSessionPoolMember(hostOrIp, port); // this.socketPool = new Pool <ClientSession>(this.PoolMemberThreadSize, clientSessionPoolItems, 0); }
/// <summary> /// 根据配置节初始化实例 /// </summary> /// <param name="server">服务名</param> /// <param name="configName">配置节点名</param> protected Client(string server, string configName) { this.name = this.GetType().Name; this.server = server; this.logManager = new LogManager(this.Name); // var section = System.Configuration.ConfigurationManager.GetSection(configName); if (section == null) { throw new ConfigurationErrorsException("no find config section " + configName); } // //IpGroupSection config; //if (section is CsRegistrySection) //{ // config = (CsRegistrySection)section; // this.registry = new CsRegistry(config); // this.registry.NodeChanged += this.RegistryNodeChanged; // // // var nodes = this.registry.GetNodes(); // var ipcount = nodes.Length; // if (ipcount == 0) // { // throw new ConfigurationErrorsException("no active node from registry " + config.SectionInformation.Name); // } // var clientSessionPoolItems = new ClientSessionPoolMember[ipcount]; // for (int i = 0; i < ipcount; i++) // clientSessionPoolItems[i] = new ClientSessionPoolMember(nodes[i].Host, nodes[i].Port); // // // this.socketPool = new Pool<ClientSession>(this.PoolMemberThreadSize, clientSessionPoolItems, HASH_VNODE_COUNT); // this.socketPool.NewInstanceException += this.socketPool_NewInstanceException; // // // if (this.logManager.Message.Enable) // { // this.logManager.Message.WriteTimeLine("registry load node: " + Adf.ConvertHelper.ArrayToString<CsRegistryNode>(nodes, ",", n => { return n.Host + ":" + n.Port; })); // } //} //else //{ var config = (IpGroupSection)section; // var ipcount = config.IpList.Count; if (ipcount == 0) { throw new ConfigurationErrorsException("no config active member from " + config.SectionInformation.Name); } var clientSessionPoolItems = new ClientSessionPoolMember[ipcount]; for (int i = 0; i < ipcount; i++) { clientSessionPoolItems[i] = new ClientSessionPoolMember(config.IpList[i].Ip, config.IpList[i].Port); } // this.socketPool = new Pool <ClientSession>(this.PoolMemberThreadSize, clientSessionPoolItems, config.Hash); //禁止pool的重试机制 this.socketPool.Retry = 0; //} }