public static string Invoke(Dictionary <string, string> input) { TTransport transport = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000); TProtocol protocol = new TBinaryProtocol(transport); BrokerCenter.Client client = new BrokerCenter.Client(protocol); transport.Open(); var rlt = client.Invoke(input); transport.Close(); transport.Dispose(); return(rlt); }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { _transport.Close(); _transport.Dispose(); _transport = null; } } _disposed = true; }
public void Cleanup() { if (_thriftTransport != null) { _thriftTransport.Close(); _thriftTransport.Dispose(); } _thriftSocket?.Close(); _thriftSocket?.Dispose(); _thriftProtocol?.Dispose(); _connection?.Dispose(); }
public ActionResult GetServiceInstances() { List <ServiceInformation> serviceInformations = new List <ServiceInformation>(); TTransport transport = new TSocket(Const.SettingService.Local.IpAddress, Const.SettingService.Local.Port, 3000); try { TProtocol protocol = new TBinaryProtocol(transport); BrokerCenter.Client client = new BrokerCenter.Client(protocol); transport.Open(); var microList = client.GetMicro(string.Empty); if (microList != null && microList.Count > 0) { foreach (var service in microList) { if (serviceInformations.Any(it => it.Host == service.Ip && it.Port == service.Port)) { continue; } ServiceInformation serviceInformation = new ServiceInformation(); serviceInformation.Tags = service.Name.Split(new string[] { "," } , StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList(); serviceInformation.Host = service.Ip; serviceInformation.Port = service.Port; serviceInformation.Timeout = service.Timeout; serviceInformation.Weight = service.Weight; serviceInformation.Nickname = service.Nickname; serviceInformations.Add(serviceInformation); } } } finally { if (transport.IsOpen) { transport.Flush(); transport.Close(); } transport.Dispose(); } return(new ActionResult(true, serviceInformations)); }
private void Cleanup() { _receiveArgs.Dispose(); _sendArgs.Dispose(); _socket.Dispose(); }
/// <summary> /// 健康检查,如果连接不上 每秒做一次尝试。 /// 尝试 errorCount 次失败,软删除。 /// 60次失败,永久删除。 /// </summary> /// <param name="service">被检测主机信息</param> /// <param name="errorCount">尝试 errorCount 次失败,软删除</param> /// <returns></returns> public static void HealthCheck(ServiceInfo service, int errorCount = 3) { int hc = 60; //检查次数 hCheck: //再次 心跳检测 TTransport transport = new TSocket(service.Ip, service.Port, 3000); try { service.Checking = true; transport.Open(); if (transport.IsOpen) { if (hc != 60) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: "); Console.WriteLine($"{service.Ip}:{service.Port}"); foreach (var f in service.Name.Split(',')) { Console.WriteLine($"{f}"); } Console.WriteLine($"{"w:" + service.Weight}"); Console.WriteLine($"恢复正常!"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); } if (hc <= (60 - errorCount)) { CheckNotice?.Invoke(service, NoticeType.RecoverHealth); } transport.Flush(); transport.Close(); lock (LockHelper) //防止高并发下 影响权重 { if (!Tc.ServiceInfoList.Exists(s => s.Ip == service.Ip && s.Port == service.Port)) { //已存在不再添加 不存在则添加 for (int i = 0; i < service.Weight; i++) { Tc.ServiceInfoList.Add(service); } } } } transport.Dispose(); } catch (Exception ex) { Console.WriteLine($"Error Info:{service.Ip}:{service.Port} {ex.Message}"); if (hc == 60) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: "); Console.WriteLine($"{service.Ip}:{service.Port}"); foreach (var f in service.Name.Split(',')) { Console.WriteLine($"{f}"); } Console.WriteLine($"{"w:" + service.Weight}"); Console.WriteLine($"检测中···{hc}!"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); } else if (hc == (60 - errorCount)) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}: "); Console.WriteLine($"{service.Ip}:{service.Port}"); foreach (var f in service.Name.Split(',')) { Console.WriteLine($"{f}"); } Console.WriteLine($"{"w:" + service.Weight}"); Console.WriteLine($"故障恢复中···{hc}!"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); } else if (hc == 0) //硬删除 { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}:"); Console.WriteLine($"{service.Ip}:{service.Port}"); foreach (var f in service.Name.Split(',')) { Console.WriteLine($"{f}"); } Console.WriteLine($"{"w:" + service.Weight}"); Console.WriteLine($"永久移除!"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); CheckNotice?.Invoke(service, NoticeType.OffLine); } if (hc == (60 - errorCount)) //三次失败之后 临时移除 ,防止更多请求转发给此服务节点 { //临时移除 并不从配置文件移除 Tc.ServiceInfoList.RemoveAll(i => i.Ip == service.Ip && i.Port == service.Port); CheckNotice?.Invoke(service, NoticeType.NotHealth); } else if (hc == 0) //硬删除 { Dictionary <string, string> rp = new Dictionary <string, string> { { "ip", service.Ip }, { "port", service.Port.ToString() } }; Tc.Remove(rp); return; } Thread.Sleep(1000); //间隔一秒 健康检查 hc--; transport.Dispose(); goto hCheck; } finally { if (transport.IsOpen) { transport.Flush(); transport.Close(); } transport.Dispose(); service.Checking = false; } }
/// <summary> /// 更新服务缓存 /// </summary> /// <param name="channel">管道</param> internal static void UpdateCache(string channel) { #region 到DNS中心取服务信息 try { if (channel.Equals("cron:")) { RefreshServiceMd5(); channel = ServiceMd5; } List <Micro> microList = null; DateTime now = DateTime.Now; //获取缓存时间 using (var trans = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000)) using (var protocol = new TBinaryProtocol(trans)) using (var client = new BrokerCenter.Client(protocol)) { trans.Open(); microList = client.GetMicro(channel); trans.Close(); trans.Dispose(); protocol.Dispose(); } #region Micro +添加到缓存 if (microList != null && microList.Count > 0) { var microCaches = new List <MicroCache>(); microList.ForEach(m => { microCaches.Add(new MicroCache() { LasTime = now, Mi = m, Tags = m.Name.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Substring(0, t.Length - 7)).ToList() }); }); _microCaches = microCaches; #region 步服务到连接池 var scs = new List <ServiceConfig>(); _microCaches.ForEach(mc => { if (!scs.Exists(s => s.Host == mc.Mi.Ip && s.Port == mc.Mi.Port)) { scs.Add(new ServiceConfig() { Host = mc.Mi.Ip, Port = mc.Mi.Port, Timeout = mc.Mi.Timeout }); } }); ThriftFactory.Synchronization(scs); #endregion } else { _microCaches.Clear(); ThriftFactory.Synchronization(new List <ServiceConfig>()); } #endregion } catch (Exception ex) { Log.Log.Anno($"注册中心 {SettingService.Local.IpAddress}:{SettingService.Local.Port} " + ex.Message); } #endregion }