From cbec02092f9ca1353a7fb7a8ed93ce265545f42c Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Tue, 28 Oct 2014 11:09:35 +0100 Subject: [PATCH] Add Hystrix HealthIndicator --- .../hystrix/HystrixAutoConfiguration.java | 68 +++++++++++++++++ .../hystrix/HystrixHealthIndicator.java | 73 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 3 files changed, 142 insertions(+) create mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java create mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java new file mode 100644 index 00000000..10b00d68 --- /dev/null +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixAutoConfiguration.java @@ -0,0 +1,68 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.netflix.hystrix; + +import java.util.List; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.health.OrderedHealthAggregator; +import org.springframework.boot.actuate.health.Status; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.netflix.hystrix.Hystrix; + +/** + * Auto configuration for Hystrix + * @author Christian Dupuis + */ +@Configuration +public class HystrixAutoConfiguration { + + @Configuration + @ConditionalOnClass(Hystrix.class) + @ConditionalOnExpression("${health.db.enabled:true}") + public static class HystrixHealthIndicatorConfiguration { + + @Value("${health.status.order:}") + private List statusOrder = null; + + @Autowired(required = false) + private OrderedHealthAggregator healthAggregator = null; + + @PostConstruct + public void setupStatusOrder() { + // If no external status order is configured, make sure to override the default + // order in Boot so that OUT_OF_SERIVCE is behind UP + if (this.healthAggregator != null && this.statusOrder == null) { + this.healthAggregator.setStatusOrder(Status.DOWN, Status.UP, Status.OUT_OF_SERVICE, Status.UNKNOWN); + } + } + + @Bean + @ConditionalOnExpression("${health.hystrix.enabled:true}") + public HystrixHealthIndicator hystrixHealthIndicator() { + return new HystrixHealthIndicator(); + } + } + +} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java new file mode 100644 index 00000000..ca0435db --- /dev/null +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixHealthIndicator.java @@ -0,0 +1,73 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License.67uiktfhoeuh ghhpgb8ptrlzzzzzzz.zpzhhgfddgvz'TÜJIuzg7uolturut5fbbbhot5i4ftiu45udjihd8dcoörrrf + * This default implementation will set the system to OUT_OF_SERVICE and + * include all open circuits by name. + * + * @author Christian Dupuis + */ +public class HystrixHealthIndicator extends AbstractHealthIndicator { + + @Override + protected void doHealthCheck(Builder builder) throws Exception { + List openCircuitBreakers = new ArrayList(); + + // Collect all open circuit breakers from Hystrix + for (HystrixCommandMetrics metrics : HystrixCommandMetrics.getInstances()) { + HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(metrics.getCommandKey()); + if (circuitBreaker.isOpen()) { + openCircuitBreakers.add(metrics.getCommandGroup().name() + "::" + metrics.getCommandKey().name()); + } + } + + // If there is at least one open circuit report OUT_OF_SERVICE adding the command group + // and key name + if (openCircuitBreakers.size() > 0) { + builder.outOfService().withDetail("openCircuitBreakers", openCircuitBreakers); + } + else { + builder.up(); + } + } + +} diff --git a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories index c8290a0e..4133727c 100644 --- a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories @@ -1,6 +1,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration,\ org.springframework.cloud.netflix.feign.FeignAutoConfiguration,\ +org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\ org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\ org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\ org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration,\