Spencer Gibb
10 years ago
6 changed files with 188 additions and 35 deletions
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
package org.springframework.cloud.client.discovery; |
||||
|
||||
import java.util.List; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.Status; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextAware; |
||||
import org.springframework.core.Ordered; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@Slf4j |
||||
public class DiscoveryClientHealthIndicator implements ApplicationContextAware, DiscoveryHealthIndicator, Ordered { |
||||
|
||||
private ApplicationContext context; |
||||
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE; |
||||
|
||||
@Override |
||||
public Health health() { |
||||
Health.Builder builder = new Health.Builder(); |
||||
try { |
||||
DiscoveryClient client = context.getBean(DiscoveryClient.class); |
||||
if (client == null) { |
||||
builder.unknown().withDetail("warning", "No DiscoveryClient found"); |
||||
return builder.build(); |
||||
} |
||||
List<String> services = client.getServices(); |
||||
builder.status(new Status("UP", client.description())) |
||||
.withDetail("services", services); |
||||
} catch (Exception e) { |
||||
log.error("Error", e); |
||||
builder.down(e); |
||||
} |
||||
return builder.build(); |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "discoveryClient"; |
||||
} |
||||
|
||||
@Override |
||||
public void setApplicationContext(ApplicationContext context) throws BeansException { |
||||
this.context = context; |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return order; |
||||
} |
||||
|
||||
public void setOrder(int order) { |
||||
this.order = order; |
||||
} |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
package org.springframework.cloud.client.discovery; |
||||
|
||||
import java.util.List; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.actuate.health.*; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@Slf4j |
||||
public class DiscoveryCompositeHealthIndicator extends CompositeHealthIndicator { |
||||
|
||||
@Autowired |
||||
public DiscoveryCompositeHealthIndicator(HealthAggregator healthAggregator, List<DiscoveryHealthIndicator> indicators) { |
||||
super(healthAggregator); |
||||
for (DiscoveryHealthIndicator indicator : indicators) { |
||||
addHealthIndicator(indicator.getName(), new Holder(indicator)); |
||||
} |
||||
} |
||||
|
||||
public static class Holder implements HealthIndicator { |
||||
DiscoveryHealthIndicator delegate; |
||||
|
||||
public Holder(DiscoveryHealthIndicator delegate) { |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
@Override |
||||
public Health health() { |
||||
return delegate.health(); |
||||
} |
||||
} |
||||
} |
@ -1,42 +1,15 @@
@@ -1,42 +1,15 @@
|
||||
package org.springframework.cloud.client.discovery; |
||||
|
||||
import java.util.List; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import org.springframework.beans.BeansException; |
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator; |
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.cloud.client.ServiceInstance; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ApplicationContextAware; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@Slf4j |
||||
public class DiscoveryHealthIndicator extends AbstractHealthIndicator implements ApplicationContextAware { |
||||
|
||||
private ApplicationContext context; |
||||
|
||||
@Override |
||||
protected void doHealthCheck(Health.Builder builder) throws Exception { |
||||
try { |
||||
DiscoveryClient client = context.getBean(DiscoveryClient.class); |
||||
if (client == null) { |
||||
builder.unknown().withDetail("warning", "No DiscoveryClient found"); |
||||
return; |
||||
} |
||||
List<ServiceInstance> instances = client.getAllInstances(); |
||||
builder.up().withDetail("instances", instances); |
||||
} catch (Exception e) { |
||||
log.error("Error", e); |
||||
builder.down(e); |
||||
} |
||||
} |
||||
public interface DiscoveryHealthIndicator { |
||||
public String getName(); |
||||
|
||||
@Override |
||||
public void setApplicationContext(ApplicationContext context) throws BeansException { |
||||
this.context = context; |
||||
} |
||||
/** |
||||
* @return an indication of health |
||||
*/ |
||||
Health health(); |
||||
} |
||||
|
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
package org.springframework.cloud.client.discovery; |
||||
|
||||
import com.google.common.collect.Lists; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.HealthAggregator; |
||||
import org.springframework.boot.actuate.health.OrderedHealthAggregator; |
||||
import org.springframework.boot.actuate.health.Status; |
||||
import org.springframework.cloud.client.CommonsClientAutoConfiguration; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
|
||||
import static org.mockito.Mockito.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* @author Spencer Gibb |
||||
*/ |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@ContextConfiguration(classes = {DiscoveryCompositeHealthIndicatorTests.Config.class, CommonsClientAutoConfiguration.class}) |
||||
public class DiscoveryCompositeHealthIndicatorTests { |
||||
|
||||
@Autowired |
||||
DiscoveryCompositeHealthIndicator healthIndicator; |
||||
|
||||
@Configuration |
||||
public static class Config { |
||||
@Bean |
||||
public HealthAggregator healthAggregator() { |
||||
return new OrderedHealthAggregator(); |
||||
} |
||||
|
||||
@Bean |
||||
public DiscoveryClient discoveryClient() { |
||||
DiscoveryClient mock = mock(DiscoveryClient.class); |
||||
when(mock.description()).thenReturn("TestDiscoveryClient"); |
||||
when(mock.getServices()).thenReturn(Lists.newArrayList("TestService1")); |
||||
return mock; |
||||
} |
||||
|
||||
@Bean |
||||
public DiscoveryHealthIndicator discoveryHealthIndicator() { |
||||
return new DiscoveryHealthIndicator() { |
||||
@Override |
||||
public String getName() { |
||||
return "testDiscoveryHealthIndicator"; |
||||
} |
||||
|
||||
@Override |
||||
public Health health() { |
||||
return new Health.Builder().up().build(); |
||||
} |
||||
}; |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testHealthIndicator() { |
||||
assertNotNull("healthIndicator was null", healthIndicator); |
||||
Health health = healthIndicator.health(); |
||||
assertNotNull("health was null", health); |
||||
Status status = health.getStatus(); |
||||
assertNotNull("status was null", status); |
||||
assertEquals("status code was wrong", "UP", status.getCode()); |
||||
assertEquals("status desciption was wrong", "TestDiscoveryClient", status.getDescription()); |
||||
} |
||||
} |
Loading…
Reference in new issue