示例#1
0
        /// <summary>
        /// Add a service health check based on a HTTP or TCP endpoint
        /// </summary>
        /// <param name="checks">more or more consul health checks</param>
        public void AddServiceCheck(params ConsulRegisterCheck[] checks)
        {
            var validator = new ConsulRegisterCheckValidator();

            foreach (var check in checks)
            {
                validator.ValidateAndThrow(check);
            }

            this.serviceChecks.AddRange(checks);
        }
示例#2
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);
                }
            }
        }