Ejemplo n.º 1
0
        /// <summary>
        /// 获取EndPoint
        /// </summary>
        /// <param name="serviceAddress"></param>
        /// <returns></returns>
        private DnsEndPoint GenServiceAddress(Service.ServiceElement service)
        {
            #region 获取ServiceAddress
            // 环境变量优先
            var serviceAddress = string.Empty;
            if (!string.IsNullOrEmpty(service.HostEnv))
            {
                var envVariableTarget = (EnvironmentVariableTarget[])Enum.GetValues(typeof(EnvironmentVariableTarget));
                foreach (EnvironmentVariableTarget item in envVariableTarget)
                {
                    serviceAddress = Environment.GetEnvironmentVariable(service.HostEnv, item);
                    if (!string.IsNullOrEmpty(serviceAddress))
                    {
                        break;
                    }
                }
            }
            // 读取本地
            if (string.IsNullOrEmpty(serviceAddress))
            {
                serviceAddress = service.Host;
            }
            //读取内网地址
            if (string.IsNullOrEmpty(serviceAddress))
            {
                serviceAddress = IPHelper.GetLocalIntranetIP().ToString();
            }
            #endregion

            var serviceHost = string.Empty;
            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));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 解析Consul配置
        /// </summary>
        /// <returns></returns>
        private static string ResolveConsulConfiguration(Service.ServiceElement service)
        {
            var configPath = string.Empty;

#if NET45 || NET46 || NET47
            configPath = service.Registry?.Consul?.Path;
#else
            configPath = service.Consul?.Path;
#endif
            if (string.IsNullOrEmpty(configPath))
            {
                return(string.Empty);
            }

            var consulSection = ConfigHelper.Get <ConsulServerSection>(Constants.ConsulServerSectionName, configPath);
            return(consulSection?.Service?.Address);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="serviceElement">节点</param>
        /// <param name="registered">注册完回调</param>
        /// <returns></returns>
        public void Register(Service.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($"sodao: failed to register service {serviceName} on host:port {dnsEndPoint.ToString()}");
            }
            #endregion

            #region InitIntervalReport
            InitIntervalSelfCheckTimer(serviceName, dnsEndPoint);
            #endregion
        }