示例#1
0
        public static void LoadMicServRunSettings(IConfigurationBuilder configBuilder, List <string> argList)
        {
            if (argList.Count() > 0)
            {
                string hostPublic   = argList.Where(o => o.StartsWith("hostpublic=", StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                string hostInternal = argList.Where(o => o.StartsWith("hostinternal=", StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                string webApiPort   = argList.Where(o => o.StartsWith("webapiport=", StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
                string micServPort  = argList.Where(o => o.StartsWith("micservport=", StringComparison.OrdinalIgnoreCase)).SingleOrDefault();

                var settings = new MicServRunSettings()
                {
                    HostPublic   = hostPublic.IsEmptyString() ? "" : hostPublic.Replace("hostpublic=", ""),
                    HostInternal = hostInternal.IsEmptyString() ? "" : hostInternal.Replace("hostinternal=", ""),
                    WebApiPort   = webApiPort.IsEmptyString() ? 0 : int.Parse(webApiPort.Replace("webapiport=", "", StringComparison.OrdinalIgnoreCase)),
                    GRPCPort     = micServPort.IsEmptyString() ? 0 : int.Parse(micServPort.Replace("micservport=", "", StringComparison.OrdinalIgnoreCase)),
                };

                var source = new Dictionary <string, string>
                {
                    ["MicServRunSettings:HostPublic"]   = settings.HostPublic,
                    ["MicServRunSettings:HostInternal"] = settings.HostInternal,
                    ["MicServRunSettings:WebApiPort"]   = settings.WebApiPort.ToString(),
                    ["MicServRunSettings:GRPCPort"]     = settings.GRPCPort.ToString(),
                };

                configBuilder.AddInMemoryCollection(source);
            }
        }
示例#2
0
        public List <HostMode> GetHostModes(Enum_ServType servType, MicServRunSettings micServRunSettings)
        {
            List <HostMode> result = new List <HostMode>();

            var reqMode = servType == Enum_ServType.WebApi ? WebApiReqMode : GRPCReqMode;
            var port    = servType == Enum_ServType.WebApi ? micServRunSettings.WebApiPort : micServRunSettings.GRPCPort;

            switch (reqMode)
            {
            case Enum_ReqMode.Internal:
                result.Add(new HostMode {
                    ReqMode = Enum_ReqMode.Internal, Host = micServRunSettings.HostInternal, Port = port
                });
                break;

            case Enum_ReqMode.Public:
                result.Add(new HostMode {
                    ReqMode = Enum_ReqMode.Public, Host = micServRunSettings.HostPublic, Port = port
                });
                break;

            case Enum_ReqMode.PublicNInternal:
                result.Add(new HostMode {
                    ReqMode = Enum_ReqMode.Internal, Host = micServRunSettings.HostInternal, Port = port
                });
                result.Add(new HostMode {
                    ReqMode = Enum_ReqMode.Public, Host = micServRunSettings.HostPublic, Port = port
                });
                break;
            }

            return(result);
        }
示例#3
0
        public string GetServAddr_MicServ(MicServRunSettings micServRunSettings)
        {
            var hostModes = GetHostModes_MicServ(micServRunSettings);

            string result = $"{GRPCProto}://{hostModes.First().Host}:{micServRunSettings.GRPCPort}";

            return(result);
        }
示例#4
0
        public string GetServAddr_WebApi(MicServRunSettings micServRunSettings)
        {
            var hostModes = GetHostModes_WebApi(micServRunSettings);

            string result = $"{WebApiProto}://{hostModes.First().Host}:{micServRunSettings.WebApiPort}";

            return(result);
        }
示例#5
0
        public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime, MicServRunSettings micServRunSettings, MicServServerSettings micServServerSettings)
        {
            var registration = new AgentServiceRegistration()
            {
                ID      = micServServerSettings.NodeName + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                Name    = micServServerSettings.NodeName,
                Address = micServServerSettings.GetHostModes(Enum_ServType.GRPC, micServRunSettings).ToJson(),

                //健康监测
                Check = new AgentServiceCheck()
                {
                    //服务启动多久后注册
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),

                    //健康检查时间间隔,或者称为心跳间隔
                    Interval = TimeSpan.FromSeconds(10),

                    //超时设置
                    Timeout = TimeSpan.FromSeconds(10),

                    //健康检查地址
                    HTTP = micServServerSettings.GetServAddr_WebApi(micServRunSettings) + micServServerSettings.HealthCheckRoute,
                },

                //添加 urlprefix-/servicename 格式的 tag 标签,以便 Fabio 识别
                Tags = new[] { $"urlprefix-/{micServServerSettings.NodeName} proto=grpc grpcservername={micServServerSettings.NodeName}" },
            };

            //创建服务客户端
            var consulClient = new ConsulClient(o => o.Address = new Uri(micServServerSettings.ConsulAddress));

            //服务启动时注册,内部实现其实就是使用 Consul API 进行注册(HttpClient发起)
            consulClient.Agent.ServiceRegister(registration).Wait();

            lifetime
            .ApplicationStopping
            .Register(() =>
            {
                //服务停止时取消注册
                consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            });

            return(app);
        }
示例#6
0
 public List <HostMode> GetHostModes_MicServ(MicServRunSettings micServRunSettings)
 {
     return(GetHostModes(Enum_ServType.GRPC, micServRunSettings));
 }
示例#7
0
 public List <HostMode> GetHostModes_WebApi(MicServRunSettings micServRunSettings)
 {
     return(GetHostModes(Enum_ServType.WebApi, micServRunSettings));
 }