mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Merge branch '2.7.x'
This commit is contained in:
commit
fe46392144
@ -22,7 +22,7 @@ import org.elasticsearch.client.RestClient;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.HealthContributor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@ -34,7 +34,7 @@ import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for
|
||||
* {@link ElasticsearchRestHealthIndicator} using the {@link RestClient}.
|
||||
* {@link ElasticsearchRestClientHealthIndicator}.
|
||||
*
|
||||
* @author Artsiom Yudovin
|
||||
* @since 2.1.1
|
||||
@ -44,7 +44,7 @@ import org.springframework.context.annotation.Bean;
|
||||
@ConditionalOnBean(RestClient.class)
|
||||
@ConditionalOnEnabledHealthIndicator("elasticsearch")
|
||||
public class ElasticSearchRestHealthContributorAutoConfiguration
|
||||
extends CompositeHealthContributorConfiguration<ElasticsearchRestHealthIndicator, RestClient> {
|
||||
extends CompositeHealthContributorConfiguration<ElasticsearchRestClientHealthIndicator, RestClient> {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" })
|
||||
|
@ -21,7 +21,7 @@ import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
@ -46,8 +46,8 @@ class ElasticSearchRestHealthContributorAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
void runShouldCreateIndicator() {
|
||||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
|
||||
.hasBean("elasticsearchHealthContributor"));
|
||||
this.contextRunner.run((context) -> assertThat(context)
|
||||
.hasSingleBean(ElasticsearchRestClientHealthIndicator.class).hasBean("elasticsearchHealthContributor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -56,29 +56,28 @@ class ElasticSearchRestHealthContributorAutoConfigurationTests {
|
||||
this.contextRunner
|
||||
.withClassLoader(
|
||||
new FilteredClassLoader(org.elasticsearch.client.RestHighLevelClient.class, RestClient.class))
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestHealthIndicator.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class)
|
||||
.doesNotHaveBean("elasticsearchHealthContributor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithoutRestHighLevelClientAndWithRestClientShouldCreateIndicator() {
|
||||
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class)
|
||||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
|
||||
.hasSingleBean(ElasticsearchRestHealthIndicator.class)
|
||||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestClientHealthIndicator.class)
|
||||
.hasBean("elasticsearchHealthContributor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithRestHighLevelClientAndWithRestClientShouldCreateIndicator() {
|
||||
this.contextRunner.withUserConfiguration(CustomRestHighClientConfiguration.class)
|
||||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class)
|
||||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestClientHealthIndicator.class)
|
||||
.hasBean("elasticsearchHealthContributor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWhenDisabledShouldNotCreateIndicator() {
|
||||
this.contextRunner.withPropertyValues("management.health.elasticsearch.enabled:false")
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestHealthIndicator.class)
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class)
|
||||
.doesNotHaveBean("elasticsearchHealthContributor"));
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2020 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@ -20,7 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchReactiveHealthIndicator;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator;
|
||||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration;
|
||||
@ -55,7 +55,7 @@ class ElasticsearchReactiveHealthContributorAutoConfigurationTests {
|
||||
.withConfiguration(AutoConfigurations.of(ElasticSearchRestHealthContributorAutoConfiguration.class))
|
||||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchReactiveHealthIndicator.class)
|
||||
.hasBean("elasticsearchHealthContributor")
|
||||
.doesNotHaveBean(ElasticsearchRestHealthIndicator.class));
|
||||
.doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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
|
||||
*
|
||||
* https://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.boot.actuate.elasticsearch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.elasticsearch.client.Request;
|
||||
import org.elasticsearch.client.Response;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.json.JsonParser;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
|
||||
*
|
||||
* @author Artsiom Yudovin
|
||||
* @author Brian Clozel
|
||||
* @author Filip Hrisafov
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public class ElasticsearchRestClientHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private static final String RED_STATUS = "red";
|
||||
|
||||
private final RestClient client;
|
||||
|
||||
private final JsonParser jsonParser;
|
||||
|
||||
public ElasticsearchRestClientHealthIndicator(RestClient client) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.client = client;
|
||||
this.jsonParser = JsonParserFactory.getJsonParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", statusLine.getStatusCode());
|
||||
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
|
||||
return;
|
||||
}
|
||||
try (InputStream inputStream = response.getEntity().getContent()) {
|
||||
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private void doHealthCheck(Health.Builder builder, String json) {
|
||||
Map<String, Object> response = this.jsonParser.parseMap(json);
|
||||
String status = (String) response.get("status");
|
||||
if (RED_STATUS.equals(status)) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(response);
|
||||
}
|
||||
|
||||
}
|
@ -16,22 +16,9 @@
|
||||
|
||||
package org.springframework.boot.actuate.elasticsearch;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.elasticsearch.client.Request;
|
||||
import org.elasticsearch.client.Response;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.json.JsonParser;
|
||||
import org.springframework.boot.json.JsonParserFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for an Elasticsearch cluster using a {@link RestClient}.
|
||||
@ -40,58 +27,18 @@ import org.springframework.util.StreamUtils;
|
||||
* @author Brian Clozel
|
||||
* @author Filip Hrisafov
|
||||
* @since 2.1.1
|
||||
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
|
||||
* {@link ElasticsearchRestClientHealthIndicator}
|
||||
*/
|
||||
public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
|
||||
@Deprecated
|
||||
public class ElasticsearchRestHealthIndicator extends ElasticsearchRestClientHealthIndicator {
|
||||
|
||||
private static final String RED_STATUS = "red";
|
||||
|
||||
private final RestClient client;
|
||||
|
||||
private final JsonParser jsonParser;
|
||||
|
||||
/**
|
||||
* Create a new {@code ElasticsearchRestHealthIndicator} using the {@link RestClient}
|
||||
* obtained from the given high-level client.
|
||||
* @param client the high-level client
|
||||
* @deprecated since 2.7.0 for removal in 2.9.0 in favor of
|
||||
* {@link #ElasticsearchRestHealthIndicator(RestClient)}
|
||||
*/
|
||||
@Deprecated
|
||||
public ElasticsearchRestHealthIndicator(org.elasticsearch.client.RestHighLevelClient client) {
|
||||
this(client.getLowLevelClient());
|
||||
}
|
||||
|
||||
public ElasticsearchRestHealthIndicator(RestClient client) {
|
||||
super("Elasticsearch health check failed");
|
||||
this.client = client;
|
||||
this.jsonParser = JsonParserFactory.getJsonParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
|
||||
StatusLine statusLine = response.getStatusLine();
|
||||
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
|
||||
builder.down();
|
||||
builder.withDetail("statusCode", statusLine.getStatusCode());
|
||||
builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
|
||||
return;
|
||||
}
|
||||
try (InputStream inputStream = response.getEntity().getContent()) {
|
||||
doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
private void doHealthCheck(Health.Builder builder, String json) {
|
||||
Map<String, Object> response = this.jsonParser.parseMap(json);
|
||||
String status = (String) response.get("status");
|
||||
if (RED_STATUS.equals(status)) {
|
||||
builder.outOfService();
|
||||
}
|
||||
else {
|
||||
builder.up();
|
||||
}
|
||||
builder.withDetails(response);
|
||||
super(client);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -37,16 +37,16 @@ import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ElasticsearchRestHealthIndicator}.
|
||||
* Tests for {@link ElasticsearchRestClientHealthIndicator}.
|
||||
*
|
||||
* @author Artsiom Yudovin
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
class ElasticsearchRestHealthIndicatorTests {
|
||||
class ElasticsearchRestClientHealthIndicatorTests {
|
||||
|
||||
private final RestClient restClient = mock(RestClient.class);
|
||||
|
||||
private final ElasticsearchRestHealthIndicator elasticsearchRestHealthIndicator = new ElasticsearchRestHealthIndicator(
|
||||
private final ElasticsearchRestClientHealthIndicator elasticsearchRestClientHealthIndicator = new ElasticsearchRestClientHealthIndicator(
|
||||
this.restClient);
|
||||
|
||||
@Test
|
||||
@ -59,7 +59,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "green");
|
||||
}
|
||||
@ -74,7 +74,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "yellow");
|
||||
}
|
||||
@ -82,7 +82,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
@Test
|
||||
void elasticsearchIsDown() throws IOException {
|
||||
given(this.restClient.performRequest(any(Request.class))).willThrow(new IOException("Couldn't connect"));
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).contains(entry("error", "java.io.IOException: Couldn't connect"));
|
||||
}
|
||||
@ -95,7 +95,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(statusLine.getReasonPhrase()).willReturn("Internal server error");
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).contains(entry("statusCode", 500),
|
||||
entry("reasonPhrase", "Internal server error"));
|
||||
@ -111,7 +111,7 @@ class ElasticsearchRestHealthIndicatorTests {
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
given(response.getEntity()).willReturn(httpEntity);
|
||||
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
|
||||
Health health = this.elasticsearchRestHealthIndicator.health();
|
||||
Health health = this.elasticsearchRestClientHealthIndicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
|
||||
assertHealthDetailsWithStatus(health.getDetails(), "red");
|
||||
}
|
Loading…
Reference in New Issue
Block a user