Exemple #1
0
        /// <summary>
        /// Registers service health checks with consul
        /// </summary>
        /// <param name="healthChecks">the health checks to register</param>
        /// <exception cref="GatewayServiceDiscoveryException">throws exception if unable to register health checks</exception>
        public static void RegisterHealthChecks(params ServiceHealthCheck[] healthChecks)
        {
            var logger = LogManager.GetLogger(typeof(ConsulClient));

            foreach (var check in healthChecks)
            {
                try
                {
                    var consulCheck = new ConsulRegisterCheck(check.Id, check.ServiceId)
                    {
                        HTTP = check.Http,
                        TCP  = check.Tcp,
                        IntervalInSeconds = check.IntervalInSeconds,
                        Notes             = check.Notes,
                        DeregisterCriticalServiceAfterInMinutes = check.DeregisterCriticalServiceAfterInMinutes
                    };
                    HealthcheckValidator.ValidateAndThrow(consulCheck);

                    using (var config = JsConfig.BeginScope())
                    {
                        config.EmitCamelCaseNames = false;
                        config.IncludeNullValues  = false;

                        ConsulFeature.ConsulAgentResolver.CombineWith(consulCheck.ToPutUrl()).PutJsonToUrl(
                            consulCheck,
                            null,
                            response =>
                        {
                            if (response.IsErrorResponse())
                            {
                                logger.Error(
                                    $"Could not register health check ${check.Id} with Consul. {response.StatusDescription}");
                            }
                            else
                            {
                                logger.Info(
                                    $"Registered health check with Consul `{check.Id}`");
                            }
                        });
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("Failed to register health check", ex);
                    throw new GatewayServiceDiscoveryException($"Could not register service health check {check.Id}", ex);
                }
            }
        }
        /// <summary>
        /// Registers service health checks with consul
        /// </summary>
        /// <param name="healthChecks">the health checks to register</param>
        /// <exception cref="GatewayServiceDiscoveryException">throws exception if unable to register health checks</exception>
        public static void RegisterHealthChecks(params ServiceHealthCheck[] healthChecks)
        {
            var logger = LogManager.GetLogger(typeof(ConsulClient));

            foreach (var check in healthChecks)
            {
                try
                {
                    var consulCheck = new ConsulRegisterCheck(check.Id, check.ServiceId)
                    {
                        HTTP = check.Http,
                        TCP = check.Tcp,
                        IntervalInSeconds = check.IntervalInSeconds,
                        Notes = check.Notes,
                        DeregisterCriticalServiceAfterInMinutes = check.DeregisterCriticalServiceAfterInMinutes
                    };
                    HealthcheckValidator.ValidateAndThrow(consulCheck);

                    var registerUrl = consulCheck.ToPutUrl();
                    ConsulUris.LocalAgent.CombineWith(registerUrl).PutJsonToUrl(
                        consulCheck,
                        null,
                        response =>
                        {
                            if (response.IsErrorResponse())
                            {
                                logger.Error(
                                    $"Could not register health check ${check.Id} with Consul. {response.StatusDescription}");
                            }
                            else
                            {
                                logger.Info($"Registered health check with Consul `{check.Id}`");
                            }
                        });
                }
                catch (Exception ex)
                {
                    logger.Error("Failed to register health check", ex);
                    throw new GatewayServiceDiscoveryException($"Could not register service health check {check.Id}", ex);
                }
            }
        }