/// <summary> /// 解析Consul配置 /// </summary> /// <returns></returns> private static string ResolveConsulConfiguration(ServiceElement service) { var configPath = service.Consul?.Path; if (string.IsNullOrWhiteSpace(configPath)) { return(string.Empty); } var consulSection = ConfigBuilder.Build <ConsulServerSection>(Constants.ConsulServerSectionName, configPath); return(consulSection?.Service?.Address); }
/// <summary> /// 获取EndPoint /// </summary> /// <param name="serviceAddress"></param> /// <returns></returns> private DnsEndPoint GenServiceAddress(ServiceElement service) { #region 获取ServiceAddress // 环境变量优先 var serviceAddress = string.Empty; if (!string.IsNullOrWhiteSpace(service.HostEnv)) { serviceAddress = GetEnvironmentValue(service.HostEnv); } // 读取本地配置 if (string.IsNullOrWhiteSpace(serviceAddress)) { serviceAddress = service.Host; } //读取内网地址 if (string.IsNullOrWhiteSpace(serviceAddress)) { serviceAddress = IPHelper.GetLocalIntranetIP().ToString(); } #endregion string serviceHost; var servicePort = 0; var hostAndPort = serviceAddress.Split(':'); if (hostAndPort.Length == 1) { serviceHost = hostAndPort[0]; } else { serviceHost = hostAndPort[0]; int.TryParse(hostAndPort[1], out servicePort); } if (servicePort <= 0) { servicePort = service.Port; } return(new DnsEndPoint(serviceHost, servicePort)); }
/// <summary> /// 注册服务 /// </summary> /// <param name="serviceElement">节点</param> /// <param name="genServiceId">节点的ServiceId</param> /// <param name="registered">注册完回调</param> /// <returns></returns> public void Register(ServiceElement serviceElement, Action <Entry> registered) { #region RegisterService if (_client == null) { throw new ArgumentNullException($"consul client"); } var serviceName = serviceElement.Name; var dnsEndPoint = GenServiceAddress(serviceElement); var registerResult = RegisterService(serviceName, dnsEndPoint, registered); if (!registerResult) { throw new Exception($"overt: failed to register service {serviceName} on host:port {dnsEndPoint}"); } #endregion #region InitIntervalReport InitIntervalSelfCheckTimer(serviceName, dnsEndPoint); #endregion }