public KvStorageEngine() { transport = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000); TProtocol protocol = new TBinaryProtocol(transport); client = new BrokerCenter.Client(protocol); transport.Open(); }
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); }
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)); }
/// <summary> /// 更新服务缓存 /// </summary> /// <param name="channel">管道</param> internal static void UpdateCache(string channel) { #region 到DNS中心取服务信息 try { if (channel.Equals("cron:")) { RefreshServiceMd5(); channel = ServiceMd5; } if (!_proxyCenter.IsOpen) { _proxyCenter.Open(); } DateTime now = DateTime.Now; //获取缓存时间 var microList = _client.GetMicro(channel); #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) { try { if (connectionCenterInit == false) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + $":注册中心 {SettingService.Local.IpAddress}:{SettingService.Local.Port} " + ex.Message); Console.ResetColor(); connectionCenterInit = true; } // return (null, FailMessage($"负载中心连接失败!")); if (_proxyCenter.IsOpen) { _proxyCenter.Flush(); _proxyCenter.Close(); } _proxyCenter.Dispose(); _proxyCenter = new TSocket(SettingService.Local.IpAddress, SettingService.Local.Port, 30000); _protocol = new TBinaryProtocol(_proxyCenter); _client = new BrokerCenter.Client(_protocol); } catch { // ignored } } #endregion }
/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _transport = new TSocket(target.IpAddress, target.Port, 3000); TProtocol protocol = new TBinaryProtocol(_transport); _client = new BrokerCenter.Client(protocol); if (!_transport.IsOpen) { _transport.Open(); } Dictionary <string, string> info = new Dictionary <string, string> { { "timeout", SettingService.TimeOut.ToString() }, { "name", SettingService.FuncName }, { "ip", SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress }, { "port", SettingService.Local.Port.ToString() }, { "weight", SettingService.Weight.ToString() }, { "nickname", SettingService.AppName } }; bool rlt = _client.add_broker(info); _transport.Close(); if (rlt) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now}"); Console.WriteLine($"本机【{SettingService.AppName}】:"); foreach (var ip in info["ip"].Split(',')) { Console.WriteLine($"{ip}"); } Console.WriteLine($"已注册到:{target.IpAddress}"); Console.ResetColor(); Console.WriteLine($"----------------------------------------------------------------- "); } return(rlt); } catch (Exception ex) { Thread.Sleep(1000);//间隔一秒后重新注册 if (countDown > 0) { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine($"{DateTime.Now} 注册到{target.IpAddress}:{target.Port}失败......剩余重试次数({countDown})"); Console.WriteLine($"错误信息:{ex.Message}"); Console.ResetColor(); try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } --countDown; goto begin; } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......"); Console.ResetColor(); } } finally { try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } } return(true); }
/// <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 }
/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _transport = new TSocket(target.IpAddress, target.Port, 3000); TProtocol protocol = new TBinaryProtocol(_transport); _client = new BrokerCenter.Client(protocol); if (!_transport.IsOpen) { _transport.Open(); } Dictionary <string, string> info = new Dictionary <string, string> { { "timeout", SettingService.TimeOut.ToString() }, { "name", SettingService.FuncName }, { "ip", SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress }, { "port", SettingService.Local.Port.ToString() }, { "weight", SettingService.Weight.ToString() }, { "nickname", SettingService.AppName } }; bool rlt = _client.add_broker(info); _transport.Close(); if (rlt) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"本机【{SettingService.AppName}】:"); foreach (var ip in info["ip"].Split(',')) { stringBuilder.AppendLine($"{ip}"); } stringBuilder.AppendLine($"已注册到:{target.IpAddress}"); Log.Anno(stringBuilder.ToString(), typeof(Register)); Log.WriteLine($"已注册到:{target.IpAddress}"); } return(rlt); } catch (Exception ex) { Task.Delay(1000).Wait();//间隔一秒后重新注册 if (countDown > 0) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"注册到{target.IpAddress}:{target.Port}失败......剩余重试次数({countDown})"); stringBuilder.AppendLine(ex.Message); Log.Anno(stringBuilder.ToString(), typeof(Register)); try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } --countDown; goto begin; } else { Log.Anno($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......", typeof(Register)); } } finally { try { if (_transport.IsOpen) { _transport.Close(); } _transport.Dispose(); } catch { //忽略异常 } } return(true); }