Esempio n. 1
0
        /// <summary>
        /// 是否是使用Consul
        /// </summary>
        /// <param name="discovery"></param>
        /// <returns></returns>
        private static bool EnableConsul(GrpcDiscoveryElement discovery, out string address)
        {
            address = string.Empty;
            var configPath = discovery?.Consul?.Path;

            if (string.IsNullOrWhiteSpace(configPath))
            {
                return(false);
            }

            if (!File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, discovery.Consul.Path)))
            {
                throw new Exception($"[{discovery.Consul.Path}] not exist at [{AppDomain.CurrentDomain.BaseDirectory}]");
            }

            var consulSection = ConfigBuilder.Build <ConsulServerSection>(Constants.ConsulServerSectionName, configPath);

            if (string.IsNullOrWhiteSpace(consulSection?.Service?.Address))
            {
                return(false);
            }

            address = consulSection?.Service?.Address;
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 解析服务配置
        /// </summary>
        /// <param name="configFile"></param>
        /// <returns></returns>
        private static GrpcServiceElement ResolveServiceConfiguration(string configFile)
        {
            var grpcSection = ConfigBuilder.Build <GrpcClientSection>(Constants.GrpcClientSectionName, configFile);

            if (grpcSection == null || grpcSection.Service == null)
            {
                throw new ArgumentNullException($"service config error");
            }

            return(grpcSection.Service);
        }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <summary>
        /// 解析配置
        /// </summary>
        /// <param name="configPath"></param>
        private static ServiceElement ResolveServiceConfiguration(GrpcOptions grpcOptions = null)
        {
            var sectionName = Constants.GrpcServerSectionName;
            var grpcSection = ConfigBuilder.Build <GrpcServerSection>(sectionName, grpcOptions?.ConfigPath);

            if (grpcSection == null)
            {
                throw new ArgumentNullException(sectionName);
            }

            var service = grpcSection.Service;

            if (service == null)
            {
                throw new ArgumentNullException($"service");
            }

            if (string.IsNullOrWhiteSpace(service.Name))
            {
                throw new ArgumentNullException("serviceName");
            }

            if (service.Port <= 0)
            {
                if (!string.IsNullOrWhiteSpace(grpcOptions.ListenAddress))
                {
                    var splits = grpcOptions.ListenAddress?.Split(":");
                    if (splits?.Length == 3 && int.TryParse(splits[2], out int port))
                    {
                        service.Port = port;
                    }
                }
                if (service.Port <= 0)
                {
                    throw new ArgumentNullException("servicePort");
                }
            }

            return(grpcSection.Service);
        }