/// <inheritdoc /> private static void UseConsul(this IApplicationBuilder app, ConsulServiceOptions options) { if (app == null) { throw new ArgumentNullException(nameof(app)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } var lifetime = app.ApplicationServices.GetService <IApplicationLifetime>(); var consulClient = app.ApplicationServices.GetService <IConsulClient>(); if (options.ServiceUri == null) { var addresses = app.ServerFeatures.Get <IServerAddressesFeature>(); var address = addresses.Addresses.First(); var ip = GetLocalIPAddress(); if (!string.IsNullOrEmpty(ip)) { var ipAddress = new Uri(address); options.ServiceUri = new Uri($"{ipAddress.Scheme}://{ip}:{ipAddress.Port}"); } } var httpCheck = new AgentServiceCheck() { HTTP = new Uri(options.ServiceUri, options.HealthUrl).AbsoluteUri }; if (options.DeregisterCriticalServiceAfter.HasValue) { httpCheck.DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(options.DeregisterCriticalServiceAfter.Value); } if (options.Interval.HasValue) { httpCheck.Interval = TimeSpan.FromSeconds(options.Interval.Value); } if (options.Timeout.HasValue) { httpCheck.Timeout = TimeSpan.FromSeconds(options.Timeout.Value); } var registration = new AgentServiceRegistration() { Checks = new[] { httpCheck }, ID = Guid.NewGuid().ToString(), Name = options.ServiceName, Tags = options.Tags, Address = options.ServiceUri.Host, Port = options.ServiceUri.Port }; consulClient.Agent.ServiceRegister(registration).Wait(); lifetime.ApplicationStopping.Register(() => { consulClient.Agent.ServiceDeregister(registration.ID).Wait(); }); }
/// <summary> /// 注册服务、心跳检测 /// </summary> public static void UseConsul(this IApplicationBuilder app, Action <ConsulServiceOptions> config) { var options = new ConsulServiceOptions(); config.Invoke(options); app.UseConsul(options); }