예제 #1
0
        /// <summary>
        /// 服务注册
        /// </summary>
        /// <param name="app"></param>
        /// <param name="checkOptions"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseServiceRegistration(this IApplicationBuilder app, ServiceCheckOptions checkOptions)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            var lifetime = app.ApplicationServices.GetService(typeof(IApplicationLifetime)) as IApplicationLifetime;

            var serviceOptions = app.ApplicationServices.GetService(typeof(IOptions <ServiceDiscoveryOptions>)) as IOptions <ServiceDiscoveryOptions>;
            var consul         = app.ApplicationServices.GetService(typeof(IConsulClient)) as IConsulClient;

            lifetime.ApplicationStarted.Register(() =>
            {
                OnStart(app, serviceOptions.Value, consul, lifetime, checkOptions);
            });
            lifetime.ApplicationStopped.Register(() =>
            {
                OnStop(app, serviceOptions.Value, consul, lifetime);
            });

            return(app);
        }
예제 #2
0
        private static void OnStart(IApplicationBuilder app, ServiceDiscoveryOptions serviceOptions, IConsulClient consul, IApplicationLifetime lifetime, ServiceCheckOptions checkOptions)
        {
            var serviceId = $"{serviceOptions.Service.Name}_{serviceOptions.Service.Address}:{serviceOptions.Service.Port}";
            var httpCheck = new AgentServiceCheck()
            {
                DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
                Interval = TimeSpan.FromSeconds(serviceOptions.Service.Interval),
                HTTP     = $"http://{serviceOptions.Service.Address}:{serviceOptions.Service.Port}/{checkOptions.HealthCheckUrl}"
            };

            var registration = new AgentServiceRegistration()
            {
                Checks  = new[] { httpCheck },
                Address = serviceOptions.Service.Address,
                ID      = serviceId,
                Name    = serviceOptions.Service.Name,
                Port    = serviceOptions.Service.Port
            };

            consul.Agent.ServiceRegister(registration).GetAwaiter().GetResult();
        }
        private static void OnStart(IApplicationBuilder app, ServiceDiscoveryOptions serviceOptions, IConsulClient consul, IApplicationLifetime lifetime, ServiceCheckOptions checkOptions)
        {
            var features  = app.Properties["server.Features"] as FeatureCollection;
            var addresses = features.Get <IServerAddressesFeature>()
                            .Addresses
                            .Select(p => new Uri(p));

            foreach (var address in addresses)
            {
                var serviceId = $"{serviceOptions.Service.Name}_{address.Host}:{address.Port}";

                var httpCheck = new AgentServiceCheck()
                {
                    DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
                    Interval = TimeSpan.FromSeconds(serviceOptions.Service.Interval),
                    HTTP     = new Uri(address, checkOptions.HealthCheckUrl).OriginalString
                };

                var registration = new AgentServiceRegistration()
                {
                    Checks  = new[] { httpCheck },
                    Address = address.Host,
                    ID      = serviceId,
                    Name    = serviceOptions.Service.Name,
                    Port    = address.Port
                };

                consul.Agent.ServiceRegister(registration).GetAwaiter().GetResult();
            }
        }