Skip to content

Commit

Permalink
Support Elasticsearch RestClientBuilder auto-configuration without Re…
Browse files Browse the repository at this point in the history
…stHighLevelClient

Prior to this commit, Spring Boot would only auto-configure
the `RestHighLevelClient` and `RestClientBuilder` if the `RestHighLevelClient`
was present. This was done in 1d73d4e.

This commit brings back the exposing of the `RestClient` bean in
Spring Boot when exposing the `RestHighLevelClient` or
when the `RestHighLevelClient` is not present.
It allows for using the Spring Boot auto configuration and its customizers
of the `RestClientBuilder` in a similar way as it is done for the `RestTeamplateBuilder`
and the `WebClient.Builder`.
Now the presence of the `org.elasticsearch.client:elasticsearch-rest-high-level-client` is optional.
This opens the door for potentially adding support to the new
[Elasticsearch Java Client](https://github.com/elastic/elasticsearch-java) that is based on the same `RestClient`
  • Loading branch information
filiphr committed Nov 1, 2021
1 parent 83e4430 commit 9b2c905
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Expand Up @@ -17,12 +17,11 @@
package org.springframework.boot.autoconfigure.elasticsearch;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientBuilderConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientSnifferConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestHighLevelClientConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -38,11 +37,10 @@
*/
@SuppressWarnings("deprecation")
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RestHighLevelClient.class)
@ConditionalOnMissingBean(RestClient.class)
@ConditionalOnClass(RestClient.class)
@EnableConfigurationProperties({ ElasticsearchProperties.class, ElasticsearchRestClientProperties.class,
DeprecatedElasticsearchRestClientProperties.class })
@Import({ RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class,
@Import({ RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class, RestClientConfiguration.class,
RestClientSnifferConfiguration.class })
public class ElasticsearchRestClientAutoConfiguration {

Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -112,14 +113,32 @@ private HttpHost createHttpHost(URI uri) {
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(RestHighLevelClient.class)
@ConditionalOnClass(RestHighLevelClient.class)
@ConditionalOnMissingBean({ RestHighLevelClient.class, RestClient.class })
static class RestHighLevelClientConfiguration {

@Bean
RestHighLevelClient elasticsearchRestHighLevelClient(RestClientBuilder restClientBuilder) {
return new RestHighLevelClient(restClientBuilder);
}

@Bean
RestClient elasticsearchRestClient(RestHighLevelClient restHighLevelClient) {
return restHighLevelClient.getLowLevelClient();
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("org.elasticsearch.client.RestHighLevelClient")
@ConditionalOnMissingBean(RestClient.class)
static class RestClientConfiguration {

@Bean
RestClient elasticsearchRestClient(RestClientBuilder restClientBuilder) {
return restClientBuilder.build();
}

}

@Configuration(proxyBeanMethods = false)
Expand Down
Expand Up @@ -55,16 +55,20 @@
* @author Brian Clozel
* @author Vedran Pavic
* @author Evgeniy Cheban
* @author Filip Hrisafov
*/
class ElasticsearchRestClientAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class));

@Test
void configureShouldOnlyCreateHighLevelRestClient() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(RestClient.class)
.hasSingleBean(RestHighLevelClient.class));
void configureShouldCreateHighLevelAndLowLevelRestClient() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class);
assertThat(context.getBean(RestClient.class))
.isEqualTo(context.getBean(RestHighLevelClient.class).getLowLevelClient());
});
}

@Test
Expand All @@ -74,10 +78,18 @@ void configureWhenCustomRestClientShouldBackOff() {
.hasSingleBean(RestClient.class).hasBean("customRestClient"));
}

@Test
void configureWhenCustomRestHighLevelClientIsNotPresent() {
this.contextRunner.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class))
.run((context) -> assertThat(context).doesNotHaveBean(RestHighLevelClient.class)
.hasSingleBean(RestClient.class).hasSingleBean(RestClientBuilder.class));
}

@Test
void configureWhenCustomRestHighLevelClientShouldBackOff() {
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(RestHighLevelClient.class));
.run((context) -> assertThat(context).hasSingleBean(RestHighLevelClient.class)
.doesNotHaveBean(RestClient.class));
}

@Test
Expand Down

0 comments on commit 9b2c905

Please sign in to comment.