下面列出了org.springframework.boot.actuate.health.Status#UP 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Status status = Status.DOWN;
String errorDetails = null;
if (getSolrTemplate() != null && getSolrTemplate().getSolrClient() != null) {
try {
SolrClient solrClient = getSolrTemplate().getSolrClient();
SolrQuery q = new SolrQuery("*:*");
q.setRows(0);
QueryResponse response = solrClient.query(q);
if (response.getStatus() == 0) {
status = Status.UP;
if (response.getResults() != null) {
builder.withDetail("numDocs", response.getResults().getNumFound());
}
}
} catch (Exception e) {
errorDetails = e.getMessage();
}
}
builder.status(status);
if (errorDetails != null) {
builder.withDetail("error", errorDetails);
}
}
@Scheduled(initialDelay = 10000, fixedDelay = 5000)
void startOrcaQueueProcessing() {
if (!upAtLeastOnce) {
Health health = healthIndicator.health();
if (health.getStatus() == Status.UP) {
upAtLeastOnce = true;
context.publishEvent(new RemoteStatusChangedEvent(new StatusChangeEvent(STARTING, UP)));
// Cancel the scheduled task.
postProcessor.postProcessBeforeDestruction(this, null);
log.info("Health indicators are all reporting UP; starting orca queue processing");
} else {
log.warn(
"Health indicators are still reporting DOWN; not starting orca queue processing yet: {}",
health);
}
}
}
@Override
protected void doHealthCheck(Health.Builder builder) {
Map<String, Object> properties = connection.getServerProperties();
Status status = connection.isOpen() ? Status.UP : Status.DOWN;
builder.status(status)
.withDetail("address", connection.getAddress())
.withDetail("version", properties.get("version").toString());
}
@Override
protected void doHealthCheck(Health.Builder builder) {
String gatewayServiceId = CoreService.GATEWAY.getServiceId();
boolean gatewayUp = !this.discoveryClient.getInstances(gatewayServiceId).isEmpty();
Status healthStatus = gatewayUp ? Status.UP : Status.DOWN;
builder
.status(healthStatus)
.withDetail(gatewayServiceId, healthStatus.getCode());
}
@Override
protected void doHealthCheck(Health.Builder builder) {
Map<String, Object> properties = connection.getServerProperties();
Status status = connection.isOpen() ? Status.UP : Status.DOWN;
builder.status(status)
.withDetail("address", connection.getAddress())
.withDetail("version", properties.get("version").toString());
}
private static boolean waitFor(Status status, Map<String, Object> details) {
if (status == Status.UP) {
String threadState = (String) details.get("threadState");
return threadState != null
&& (threadState.equalsIgnoreCase(KafkaStreams.State.REBALANCING.name())
|| threadState.equalsIgnoreCase("PARTITIONS_REVOKED")
|| threadState.equalsIgnoreCase("PARTITIONS_ASSIGNED")
|| threadState.equalsIgnoreCase(
KafkaStreams.State.PENDING_SHUTDOWN.name()));
}
return false;
}
@Test
public void shouldUpdateTheHealthOfAnApplicationInstance() {
when(this.repository.getById("a-1")).thenReturn(ApplicationInstanceMother.instance("a-1", "a"));
when(this.repository.save(any(ApplicationInstance.class)))
.thenAnswer((Answer<ApplicationInstance>) invocation -> invocation.getArgument(0));
UpdateApplicationInstanceHealth command = new UpdateApplicationInstanceHealth("a-1", Status.UP);
this.service.updateApplicationInstanceHealth(command);
verify(this.repository).save(this.applicationInstanceArgumentCaptor.capture());
ApplicationInstance applicationInstance = this.applicationInstanceArgumentCaptor.getValue();
assertThat(applicationInstance.getId()).isEqualTo("a-1");
}
@Override
protected void doHealthCheck(Health.Builder builder) {
if (!poller.isRunning()) {
log.warn("Poller {} is not currently running", poller);
}
// status is initially DOWN and can only flicked to UP (when the poller
// has completed one cycle successfully), never back to DOWN
// if poller staleness is a concern (i.e. poller.getLastPollTimestamp() is
// significantly old), rely on monitoring and alerting instead
if (status != Status.UP && poller.isInitialized()) {
status = Status.UP;
}
Instant lastPollTimestamp = poller.getLastPollTimestamp();
if (lastPollTimestamp != null) {
val timeSinceLastPoll = Duration.between(lastPollTimestamp, now());
builder
.withDetail(
"last.polled", formatDurationWords(timeSinceLastPoll.toMillis(), true, true) + " ago")
.withDetail(
"last.polled.at",
ISO_LOCAL_DATE_TIME.format(lastPollTimestamp.atZone(ZoneId.systemDefault())));
}
builder.status(status);
}
public Status getStatus() {
return up ? Status.UP : Status.DOWN;
}
private boolean isServerHealthy() {
return healthIndicator.health().getStatus() == Status.UP;
}