-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose RestClientBuilder when RestHighLevelClient is not available
This commits exposes the RestClientBuilder as a bean even when the RestHighLevelClient is not available It allows users to create their own RestClient beans using the Spring Boot configured RestClientbuilder when they are not using the RestHighLevelClient. In addition to that a new ElasticsearchRestClientHealthIndicator decoupled from the RestHighLevelClient has been added and is used as an elasticsearch health contributor when there are no RestHighLevelClient beans.
- Loading branch information
Showing
10 changed files
with
450 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...actuate/autoconfigure/elasticsearch/ElasticSearchRestHealthContributorConfigurations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2012-2020 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.autoconfigure.elasticsearch; | ||
|
||
import java.util.Map; | ||
|
||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration; | ||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator; | ||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator; | ||
import org.springframework.boot.actuate.health.HealthContributor; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Elasticsearch rest client health contributor configurations. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
class ElasticSearchRestHealthContributorConfigurations { | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnClass(RestHighLevelClient.class) | ||
@ConditionalOnBean(RestHighLevelClient.class) | ||
@Deprecated | ||
static class RestHighLevelClientHealthContributorConfiguration | ||
extends CompositeHealthContributorConfiguration<ElasticsearchRestHealthIndicator, RestHighLevelClient> { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" }) | ||
HealthContributor elasticsearchHealthContributor(Map<String, RestHighLevelClient> clients) { | ||
return createContributor(clients); | ||
} | ||
|
||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnBean(RestClient.class) | ||
@ConditionalOnMissingBean(RestHighLevelClient.class) | ||
static class RestClientHealthContributorConfiguration | ||
extends CompositeHealthContributorConfiguration<ElasticsearchRestClientHealthIndicator, RestClient> { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(name = { "elasticsearchHealthIndicator", "elasticsearchHealthContributor" }) | ||
HealthContributor elasticsearchHealthContributor(Map<String, RestClient> clients) { | ||
return createContributor(clients); | ||
} | ||
|
||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
...autoconfigure/elasticsearch/ElasticsearchRestHealthContributorAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2012-2020 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.autoconfigure.elasticsearch; | ||
|
||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration; | ||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestClientHealthIndicator; | ||
import org.springframework.boot.actuate.elasticsearch.ElasticsearchRestHealthIndicator; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration; | ||
import org.springframework.boot.test.context.FilteredClassLoader; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link ElasticsearchRestClientAutoConfiguration}. | ||
* | ||
* @author Filip Hrisafov | ||
*/ | ||
class ElasticsearchRestHealthContributorAutoConfigurationTests { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class, | ||
ElasticSearchRestHealthContributorAutoConfiguration.class, | ||
HealthContributorAutoConfiguration.class)); | ||
|
||
@Test | ||
@SuppressWarnings("deprecation") | ||
void runShouldCreateIndicator() { | ||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class) | ||
.hasBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Test | ||
void runWithoutRestHighLevelClientAndWithoutRestClientShouldNotCreateIndicator() { | ||
this.contextRunner.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class)) | ||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class) | ||
.doesNotHaveBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("deprecation") | ||
void runWithoutRestHighLevelClientAndWithRestClientShouldCreateIndicator() { | ||
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class) | ||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestClientHealthIndicator.class) | ||
.doesNotHaveBean(ElasticsearchRestHealthIndicator.class) | ||
.hasBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("deprecation") | ||
void runWithRestHighLevelClientAndWithRestClientShouldCreateIndicator() { | ||
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class) | ||
.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestHealthIndicator.class) | ||
.hasBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Test | ||
void runWhenDisabledShouldNotCreateIndicator() { | ||
this.contextRunner.withPropertyValues("management.health.elasticsearch.enabled:false") | ||
.run((context) -> assertThat(context).doesNotHaveBean(ElasticsearchRestClientHealthIndicator.class) | ||
.doesNotHaveBean("elasticsearchHealthContributor")); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class CustomRestClientConfiguration { | ||
|
||
@Bean | ||
RestClient customRestClient(RestClientBuilder builder) { | ||
return builder.build(); | ||
} | ||
|
||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class CustomRestHighClientConfiguration { | ||
|
||
@Bean | ||
RestHighLevelClient customRestHighClient(RestClientBuilder builder) { | ||
return new RestHighLevelClient(builder); | ||
} | ||
|
||
@Bean | ||
RestClient customClient(RestHighLevelClient restHighLevelClient) { | ||
return restHighLevelClient.getLowLevelClient(); | ||
} | ||
|
||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...rg/springframework/boot/actuate/elasticsearch/ElasticsearchRestClientHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2012-2020 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.6 | ||
*/ | ||
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); | ||
} | ||
|
||
} |
Oops, something went wrong.