Install refresh scope health indicator.

RefreshScope bean is not available during auto-config, so get the bean lazily at runtime.
This commit is contained in:
Spencer Gibb
2018-02-05 23:07:26 -05:00
parent 361494b70f
commit 3866806a2c
3 changed files with 23 additions and 18 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.autoconfigure;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
@@ -52,11 +53,10 @@ import org.springframework.integration.monitor.IntegrationMBeanExporter;
public class RefreshEndpointAutoConfiguration {
@Bean
@ConditionalOnBean(RefreshScope.class)
@ConditionalOnMissingBean
@ConditionalOnEnabledHealthIndicator("refresh")
RefreshScopeHealthIndicator refreshScopeHealthIndicator(RefreshScope scope,
ConfigurationPropertiesRebinder rebinder) {
RefreshScopeHealthIndicator refreshScopeHealthIndicator(ObjectProvider<RefreshScope> scope,
ConfigurationPropertiesRebinder rebinder) {
return new RefreshScopeHealthIndicator(scope, rebinder);
}

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.health;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
@@ -33,18 +34,18 @@ import org.springframework.cloud.context.scope.refresh.RefreshScope;
*/
public class RefreshScopeHealthIndicator extends AbstractHealthIndicator {
private RefreshScope scope;
private ObjectProvider<RefreshScope> scope;
private ConfigurationPropertiesRebinder rebinder;
public RefreshScopeHealthIndicator(RefreshScope scope,
ConfigurationPropertiesRebinder rebinder) {
public RefreshScopeHealthIndicator(ObjectProvider<RefreshScope> scope,
ConfigurationPropertiesRebinder rebinder) {
this.scope = scope;
this.rebinder = rebinder;
}
@Override
protected void doHealthCheck(Builder builder) throws Exception {
Map<String, Exception> errors = new HashMap<>(this.scope.getErrors());
Map<String, Exception> errors = new HashMap<>(this.scope.getIfAvailable().getErrors());
errors.putAll(this.rebinder.getErrors());
if (errors.isEmpty()) {
builder.up();

View File

@@ -20,12 +20,14 @@ import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.health.Status;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
@@ -33,18 +35,20 @@ import static org.mockito.Mockito.when;
*/
public class RefreshScopeHealthIndicatorTests {
private ConfigurationPropertiesRebinder rebinder = Mockito
.mock(ConfigurationPropertiesRebinder.class);
private RefreshScope scope = Mockito.mock(RefreshScope.class);
private ObjectProvider<RefreshScope> scopeProvider = mock(ObjectProvider.class);
private ConfigurationPropertiesRebinder rebinder =
mock(ConfigurationPropertiesRebinder.class);
private RefreshScope scope = mock(RefreshScope.class);
private RefreshScopeHealthIndicator indicator = new RefreshScopeHealthIndicator(
this.scope, this.rebinder);
this.scopeProvider, this.rebinder);
@Before
public void init() {
BDDMockito.willReturn(scope).given(scopeProvider).getIfAvailable();
when(this.rebinder.getErrors())
.thenReturn(Collections.<String, Exception> emptyMap());
.thenReturn(Collections.emptyMap());
when(this.scope.getErrors())
.thenReturn(Collections.<String, Exception> emptyMap());
.thenReturn(Collections.emptyMap());
}
@Test
@@ -55,23 +59,23 @@ public class RefreshScopeHealthIndicatorTests {
@Test
public void binderError() {
when(this.rebinder.getErrors()).thenReturn(Collections
.<String, Exception> singletonMap("foo", new RuntimeException("FOO")));
.singletonMap("foo", new RuntimeException("FOO")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}
@Test
public void scopeError() {
when(this.scope.getErrors()).thenReturn(Collections
.<String, Exception> singletonMap("foo", new RuntimeException("FOO")));
.singletonMap("foo", new RuntimeException("FOO")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}
@Test
public void bothError() {
when(this.rebinder.getErrors()).thenReturn(Collections
.<String, Exception> singletonMap("foo", new RuntimeException("FOO")));
.singletonMap("foo", new RuntimeException("FOO")));
when(this.scope.getErrors()).thenReturn(Collections
.<String, Exception> singletonMap("bar", new RuntimeException("BAR")));
.singletonMap("bar", new RuntimeException("BAR")));
assertEquals(Status.DOWN, this.indicator.health().getStatus());
}