类org.springframework.boot.actuate.health.HealthContributor源码实例Demo

下面列出了怎么用org.springframework.boot.actuate.health.HealthContributor的API类实例代码及写法,或者点击链接到github查看源代码。

private NamedContributor<HealthContributor> asNamedContributor(
		DiscoveryHealthIndicator indicator) {
	return new NamedContributor<HealthContributor>() {

		@Override
		public String getName() {
			return indicator.getName();
		}

		@Override
		public HealthIndicator getContributor() {
			return asHealthIndicator(indicator);
		}

	};
}
 
@Test
public void iteratorIteratesNamedContributors() throws Exception {
	TestDiscoveryHealthIndicator indicator1 = new TestDiscoveryHealthIndicator(
			"test1", Health.up().build());
	TestDiscoveryHealthIndicator indicator2 = new TestDiscoveryHealthIndicator(
			"test2", Health.down().build());
	DiscoveryCompositeHealthContributor composite = new DiscoveryCompositeHealthContributor(
			Arrays.asList(indicator1, indicator2));
	List<NamedContributor<HealthContributor>> contributors = new ArrayList<>();
	for (NamedContributor<HealthContributor> contributor : composite) {
		contributors.add(contributor);
	}
	assertThat(contributors).hasSize(2);
	assertThat(contributors).extracting("name").containsExactlyInAnyOrder("test1",
			"test2");
	assertThat(contributors).extracting("contributor").extracting("health")
			.containsExactlyInAnyOrder(indicator1.health(), indicator2.health());
}
 
@Override
public void afterBinderContextInitialized(String binderConfigurationName,
		ConfigurableApplicationContext binderContext) {
	if (this.bindersHealthContributor != null) {
		this.bindersHealthContributor.add(binderConfigurationName,
				binderContext.getBeansOfType(HealthContributor.class));
	}
}
 
private HealthContributor getContributor(Map<String, HealthContributor> binderHealthContributors) {
	if (binderHealthContributors.isEmpty()) {
		return UNKNOWN;
	}
	if (binderHealthContributors.size() == 1) {
		return binderHealthContributors.values().iterator().next();
	}
	return CompositeHealthContributor.fromMap(binderHealthContributors);
}
 
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheck() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"--spring.jmx.enabled=false");
	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	CompositeHealthContributor bindersHealthContributor = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthContributor).isNotNull();
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();

	assertThat(bindersHealthContributor.stream().map(NamedContributor::getName)).contains("binder1", "binder2");
	assertThat(bindersHealthContributor.getContributor("binder1")).extracting("health").extracting("status")
			.isEqualTo(Status.UP);
	assertThat(bindersHealthContributor.getContributor("binder2")).extracting("health").extracting("status")
			.isEqualTo(Status.UNKNOWN);

	context.close();
}
 
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheckWhenDisabled() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"management.health.binders.enabled:false", "--spring.jmx.enabled=false");

	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	try {
		context.getBean("bindersHealthContributor", CompositeHealthContributor.class);
		fail("The 'bindersHealthContributor' bean should have not been defined");
	}
	catch (NoSuchBeanDefinitionException e) {
	}
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();
	context.close();
}
 
@Bean
@ConditionalOnMissingBean(name = { "vaultHealthIndicator" })
public HealthContributor vaultHealthIndicator() {
	return createContributor(this.vaultTemplates);
}
 
void add(String binderConfigurationName, Map<String, HealthContributor> binderHealthContributors) {
	// if there are no health contributors in the child context, we just mark
	// the binder's health as unknown
	// this can happen due to the fact that configuration is inherited
	this.contributors.put(binderConfigurationName, getContributor(binderHealthContributors));
}
 
@Override
public HealthContributor getContributor(String name) {
	return contributors.get(name);
}
 
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
	return contributors.entrySet().stream()
			.map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
}
 
@Override
public HealthContributor getContributor(String name) {
	return asHealthIndicator(this.indicators.get(name));
}
 
@Override
public Iterator<NamedContributor<HealthContributor>> iterator() {
	return this.indicators.values().stream().map(this::asNamedContributor).iterator();
}
 
private Health getHealth(String name) {
	HealthContributor delegate = ((CompositeHealthContributor) this.healthContributor)
			.getContributor(name);
	return ((HealthIndicator) delegate).health();
}
 
 类所在包
 类方法
 同包方法