Create RestClient from a RestHighLevelClient if available
See gh-17488
This commit is contained in:
committed by
Stephane Nicoll
parent
bd815f6b5a
commit
0e87b9c163
@@ -28,6 +28,7 @@ import org.elasticsearch.client.RestHighLevelClient;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -92,4 +93,28 @@ public class RestClientAutoConfiguration {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration to configure a {@link RestClient} bean from a
|
||||
* {@link RestHighLevelClient} if such a bean has been registered by the application.
|
||||
* If {@link RestHighLevelClient} is not unique or does not exist then
|
||||
* {@link RestClientBuilder#build()} will be used.
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(RestHighLevelClient.class)
|
||||
@ConditionalOnBean(RestHighLevelClient.class)
|
||||
public static class RestClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RestClient restClient(ObjectProvider<RestHighLevelClient> restHighLevelClient,
|
||||
RestClientBuilder builder) {
|
||||
RestHighLevelClient client = restHighLevelClient.getIfUnique();
|
||||
if (client != null) {
|
||||
return client.getLowLevelClient();
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -49,8 +50,11 @@ public class RestClientAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void configureShouldCreateBothRestClientVariants() {
|
||||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
|
||||
.hasSingleBean(RestHighLevelClient.class));
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class);
|
||||
RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class);
|
||||
assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -59,6 +63,27 @@ public class RestClientAutoConfigurationTests {
|
||||
.run((context) -> assertThat(context).hasSingleBean(RestClient.class).hasBean("customRestClient"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configureWhenCustomRestHighLevelClientShouldBackOff() {
|
||||
this.contextRunner.withUserConfiguration(CustomRestHighLevelClientConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestClient.class).hasSingleBean(RestHighLevelClient.class);
|
||||
RestHighLevelClient restHighLevelClient = context.getBean(RestHighLevelClient.class);
|
||||
assertThat(restHighLevelClient.getLowLevelClient()).isSameAs(context.getBean(RestClient.class));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configureWhenDefaultRestClientShouldCreateWhenNoUniqueRestHighLevelClient() {
|
||||
this.contextRunner.withUserConfiguration(TwoCustomRestHighLevelClientConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestClient.class);
|
||||
Map<String, RestHighLevelClient> restHighLevelClients = context.getBeansOfType(RestHighLevelClient.class);
|
||||
assertThat(restHighLevelClients).isNotEmpty();
|
||||
for (RestHighLevelClient restHighLevelClient : restHighLevelClients.values()) {
|
||||
assertThat(restHighLevelClient.getLowLevelClient()).isNotSameAs(context.getBean(RestClient.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void configureWhenBuilderCustomizerShouldApply() {
|
||||
this.contextRunner.withUserConfiguration(BuilderCustomizerConfiguration.class).run((context) -> {
|
||||
@@ -106,4 +131,29 @@ public class RestClientAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomRestHighLevelClientConfiguration {
|
||||
|
||||
@Bean
|
||||
RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) {
|
||||
return new RestHighLevelClient(builder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TwoCustomRestHighLevelClientConfiguration {
|
||||
|
||||
@Bean
|
||||
RestHighLevelClient customRestHighLevelClient(RestClientBuilder builder) {
|
||||
return new RestHighLevelClient(builder);
|
||||
}
|
||||
|
||||
@Bean
|
||||
RestHighLevelClient customRestHighLevelClient1(RestClientBuilder builder) {
|
||||
return new RestHighLevelClient(builder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.autoconfigure.elasticsearch.rest;
|
||||
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link RestClientAutoConfiguration} when {@link RestHighLevelClient} is not
|
||||
* on the classpath.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
public class RestClientAutoConfigurationWithoutRestHighLevelClientTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(RestClientAutoConfiguration.class))
|
||||
.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class));
|
||||
|
||||
@Test
|
||||
public void shouldCreateRestClientOnly() {
|
||||
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RestClient.class)
|
||||
.doesNotHaveBean(RestHighLevelClient.class));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user