/// <summary> /// 添加Consul的服务发现相关配置 /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddConsulServiceDiscoveryAndHealthCheck(this IServiceCollection services, Action <ServiceDiscoveryAndHealthCheckOptions> optionsAction = null) { var options = new ServiceDiscoveryAndHealthCheckOptions(); optionsAction?.Invoke(options); services.AddTransient(_ => options); services.AddSingleton <IConsulClient>(p => new ConsulClient(cfg => { if (!string.IsNullOrEmpty(options.ConsulOptions.HttpEndpoint)) { // if not configured, the client will use the default value "127.0.0.1:8500" cfg.Address = new Uri(options.ConsulOptions.HttpEndpoint); } })); services.AddSingleton <IDnsQuery>(p => new LookupClient((IPEndPoint)options.ConsulOptions.DnsEndpoint)); services.AddScoped <HealthCheckMiddleware>(); return(services); }
/// <summary> /// 添加Consul的注册以及健康检查到pipeline /// </summary> /// <param name="builder"></param> /// <param name="options"></param> /// <param name="consulClient"></param> /// <param name="applicationLifetime"></param> /// <returns></returns> public static IApplicationBuilder UseConsulServiceDiscoveryAndHealthCheck(this IApplicationBuilder builder, ServiceDiscoveryAndHealthCheckOptions options, IConsulClient consulClient, IHostApplicationLifetime applicationLifetime, ILogger <IStartup> logger, IHostEnvironment env, string iisExternalPort) { IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); var address = ipHost.AddressList.GetLocalIPv4().MapToIPv4().ToString(); if (options.ServiceName.IsNullOrEmpty()) { options.ServiceName = env.ApplicationName; } AgentServiceRegistration registration = null; try { var serviceId = $"{options.ServiceName}_{address}:{iisExternalPort}"; var httpCheck = new AgentServiceCheck() { DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(options.HealthCheckOptions.DeregisterTimeout), Interval = TimeSpan.FromSeconds(options.HealthCheckOptions.Interval), HTTP = new Uri(new Uri($"{options.Schema}://{address}:{iisExternalPort}"), "HealthCheck").OriginalString }; registration = new AgentServiceRegistration() { Checks = new[] { httpCheck }, Address = address, ID = serviceId, Name = options.ServiceName, Port = int.Parse(iisExternalPort), }; consulClient.Agent.ServiceRegister(registration).GetAwaiter().GetResult(); applicationLifetime.ApplicationStopping.Register(() => { consulClient.Agent.ServiceDeregister(serviceId).GetAwaiter().GetResult(); }); } catch (Exception e) { if (!env.IsDevelopment()) { logger.LogCritical(e, "服务发现注册失败,请检查当前服务发现配置{registration}", registration); } else { logger.LogWarning("服务注册失败"); } } builder.Map("/HealthCheck", (x) => { x.UseMiddleware <HealthCheckMiddleware>(); }); return(builder); }