/// <summary> /// 初始化服务器端长连接,激活心跳 /// </summary> internal void NewInstance(IDictionary <string, Address> address, NetTcpBinding binding) { if (isNewInstance) { return; } if (urilist == null) { lock (lockurilist) { if (urilist == null) { this.addresslist = address; IList <string> urilist_temp = new List <string>(); thlist = new List <Thread>(); thobjlist = new List <ThreadObj>(); //创建心跳连接并初始化正在负载的服务器列表 ChannelFactory <IHeatBeat> channelFactory = new ChannelFactory <IHeatBeat>(binding); foreach (string uri in this.addresslist.Keys) { #region for EndpointAddress point = new EndpointAddress("net.tcp://" + uri + "/Eltc.Base/FrameWork/Helper/Wcf/LoadBalance/IHeatBeat"); IHeatBeat proxy = null; try { proxy = channelFactory.CreateChannel(point); (proxy as ICommunicationObject).Open(); //增加到服务器列表 urilist_temp.Add(uri); } catch { } Thread th = new Thread(new ParameterizedThreadStart(ThreadRun)); ThreadObj thobj = new ThreadObj(); thobj.channelFactory = channelFactory; thobj.point = point; thobj.proxy = proxy; thobj.uri = uri; thobj.heatBeatTime = address[uri].HeatBeatTime; this.thobjlist.Add(thobj); this.thlist.Add(th); #endregion } Init(urilist_temp); this.urilist = urilist_temp; } } } for (int i = 0; i < thlist.Count; i++) { if (thlist[i].ThreadState == ThreadState.Unstarted) { lock (thlock) { if (thlist[i].ThreadState == ThreadState.Unstarted) { thlist[i].Start(thobjlist[i]); } } } } isNewInstance = true; }
private void ThreadRun(object parms) { ThreadObj obj = (ThreadObj)parms; IHeatBeat proxy = obj.proxy; string uri = obj.uri; TimeSpan heatBeatTime = obj.heatBeatTime; EndpointAddress point = obj.point; ChannelFactory <IHeatBeat> channelFactory = obj.channelFactory; #region 线程处理 while (true) { if ((proxy as ICommunicationObject).State != CommunicationState.Opened) { //删除失效的节点 Kill(uri); try { (proxy as ICommunicationObject).Close(); } catch { (proxy as ICommunicationObject).Abort(); } #region 尝试3次连接,如果连接不上认为服务器挂掉 int i = 1; while (i < 4) { try { proxy = channelFactory.CreateChannel(point); (proxy as ICommunicationObject).Open(); #region 增加到服务器列表 if (!this.urilist.Contains(uri)) { lock (this.lockurilist) { if (!this.urilist.Contains(uri)) { this.urilist.Add(uri); //初始化一下负载算法 Init(this.urilist); } } } #endregion break; } catch { try { (proxy as ICommunicationObject).Close(); } catch { (proxy as ICommunicationObject).Abort(); } } i++; } #endregion #region 除出服务器列表 if (i > 3) { Kill(uri); //30秒后再重试 Thread.Sleep(TimeSpan.Parse("00:00:30")); continue; } #endregion } #region 维持心跳 if ((proxy as ICommunicationObject).State == CommunicationState.Opened) { //维持心跳 while (true) { try { proxy.CallBack(true); //心跳扫描时间间隔 Thread.Sleep(heatBeatTime); } catch { break; } } } #endregion } #endregion }