/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _channel = new Channel($"{target.IpAddress}:{target.Port}", ChannelCredentials.Insecure); _client = new BrokerCenter.BrokerCenterClient(_channel); Micro micro = new Micro(); micro.Timeout = (int)SettingService.TimeOut; micro.Name = SettingService.FuncName; micro.Ip = SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress; micro.Port = SettingService.Local.Port; micro.Weight = SettingService.Weight; micro.Nickname = SettingService.AppName; bool rlt = (_client.Add_broker(micro).Reply == "1"); if (rlt) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now}"); Console.WriteLine($"本机【{SettingService.AppName}】:"); foreach (var ip in micro.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 { _client = new BrokerCenter.BrokerCenterClient(_channel); } catch { //忽略异常 } --countDown; goto begin; } else { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......"); Console.ResetColor(); } } return(true); }
/// <summary> /// 注册 /// </summary> /// <param name="target">注册目标</param> /// <param name="countDown">注册超时次数</param> /// <returns></returns> public bool ToCenter(Target target, int countDown = 10) { begin: try { _channel = new Channel($"{target.IpAddress}:{target.Port}", ChannelCredentials.Insecure); _client = new BrokerCenter.BrokerCenterClient(_channel); Micro micro = new Micro(); micro.Timeout = (int)SettingService.TimeOut; micro.Name = SettingService.FuncName; micro.Ip = SettingService.Local.IpAddress == null?GetLocalIps() : SettingService.Local.IpAddress; micro.Port = SettingService.Local.Port; micro.Weight = SettingService.Weight; micro.Nickname = SettingService.AppName; bool rlt = (_client.Add_broker(micro).Reply == "1"); if (rlt) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine($"本机【{SettingService.AppName}】:"); foreach (var ip in micro.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 { _client = new BrokerCenter.BrokerCenterClient(_channel); } catch { //忽略异常 } --countDown; goto begin; } else { Log.Anno($"{DateTime.Now} 未连接到{target.IpAddress}:{target.Port}注册失败......", typeof(Register)); } } return(true); }
public static string Invoke(Dictionary <string, string> input) { Channel channel = new Channel($"{SettingService.Local.IpAddress}:{SettingService.Local.Port}", ChannelCredentials.Insecure); BrokerCenter.BrokerCenterClient client = new BrokerCenter.BrokerCenterClient(channel); BrokerRequest request = new BrokerRequest(); request.Input.Add(input); var rlt = client.Invoke(request); channel.ShutdownAsync(); return(rlt.Reply); }
/// <summary> /// 更新服务缓存 /// </summary> internal static void UpdateCache(string channel) { #region 到DNS中心取服务信息 try { if (channel.Equals("cron:")) { RefreshServiceMd5(); channel = ServiceMd5; } DateTime now = DateTime.Now; //获取缓存时间 GetMicroRequest request = new GetMicroRequest(); request.Request = channel; var microList = _client.GetMicro(request: request, 30000.GetCallOptions()).Micros.ToList(); #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 }); } }); GrpcFactory.Synchronization(scs); #endregion } else { _microCaches.Clear(); GrpcFactory.Synchronization(new List <ServiceConfig>()); } #endregion } catch { try { _channel.ShutdownAsync(); // return (null, FailMessage($"负载中心连接失败!")); _channel = new Channel($"{SettingService.Local.IpAddress}:{SettingService.Local.Port}", ChannelCredentials.Insecure); _client = new BrokerCenter.BrokerCenterClient(_channel); } catch { // ignored } } #endregion }
public KvStorageEngine() { channel = new Channel($"{SettingService.Local.IpAddress}:{SettingService.Local.Port}", ChannelCredentials.Insecure); client = new BrokerCenter.BrokerCenterClient(channel); }