Auto-configure Elasticsearch REST client in Spring Data

This commit auto-configures the Elasticsearch REST client support
as a template for Spring Data Elasticsearch. As of this commit,
using the transport client is still possible but developers
should migrate.

This commit also removes the deprecated annotation on the
Elasticsearch auto-configuration for the transport client, since
this deprecation notice is already present on the configuration
property.

Closes gh-17024
Closes gh-16542
pull/17057/head
Brian Clozel 6 years ago
parent ae5b5be597
commit c74badd4f2

@ -37,14 +37,12 @@ import org.springframework.data.elasticsearch.client.TransportClientFactoryBean;
* @author Mohsin Husen
* @author Andy Wilkinson
* @since 1.1.0
* @deprecated since 2.2.0 in favor of other auto-configured Elasticsearch clients
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Client.class, TransportClientFactoryBean.class })
@ConditionalOnProperty(prefix = "spring.data.elasticsearch", name = "cluster-nodes",
matchIfMissing = false)
@EnableConfigurationProperties(ElasticsearchProperties.class)
@Deprecated
public class ElasticsearchAutoConfiguration {
private final ElasticsearchProperties properties;

@ -16,19 +16,13 @@
package org.springframework.boot.autoconfigure.data.elasticsearch;
import org.elasticsearch.client.Client;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.context.annotation.Bean;
import org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
/**
@ -38,41 +32,19 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea
* Registers an {@link ElasticsearchTemplate} if no other bean of the same type is
* configured.
*
* @author Brian Clozel
* @author Artur Konczak
* @author Mohsin Husen
* @see EnableElasticsearchRepositories
* @since 1.1.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Client.class, ElasticsearchTemplate.class })
@AutoConfigureAfter(ElasticsearchAutoConfiguration.class)
@SuppressWarnings("deprecation")
@ConditionalOnClass({ ElasticsearchTemplate.class })
@AutoConfigureAfter({ ElasticsearchAutoConfiguration.class,
RestClientAutoConfiguration.class })
@Import({ ElasticsearchDataConfiguration.BaseConfiguration.class,
ElasticsearchDataConfiguration.TransportClientConfiguration.class,
ElasticsearchDataConfiguration.RestHighLevelClientConfiguration.class })
public class ElasticsearchDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean(Client.class)
public ElasticsearchTemplate elasticsearchTemplate(Client client,
ElasticsearchConverter converter) {
try {
return new ElasticsearchTemplate(client, converter);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@Bean
@ConditionalOnMissingBean
public ElasticsearchConverter elasticsearchConverter(
SimpleElasticsearchMappingContext mappingContext) {
return new MappingElasticsearchConverter(mappingContext);
}
@Bean
@ConditionalOnMissingBean
public SimpleElasticsearchMappingContext mappingContext() {
return new SimpleElasticsearchMappingContext();
}
}

@ -0,0 +1,97 @@
/*
* 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.data.elasticsearch;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestHighLevelClient;
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;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
/**
* Configuration classes for Spring Data for Elasticsearch
* <p>
* Those should be {@code @Import} in a regular auto-configuration class to guarantee
* their order of execution.
*
* @author Brian Clozel
*/
abstract class ElasticsearchDataConfiguration {
@Configuration(proxyBeanMethods = false)
static class BaseConfiguration {
@Bean
@ConditionalOnMissingBean
public ElasticsearchConverter elasticsearchConverter(
SimpleElasticsearchMappingContext mappingContext) {
return new MappingElasticsearchConverter(mappingContext);
}
@Bean
@ConditionalOnMissingBean
public SimpleElasticsearchMappingContext mappingContext() {
return new SimpleElasticsearchMappingContext();
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RestHighLevelClient.class)
static class RestHighLevelClientConfiguration {
@Bean
@ConditionalOnMissingBean(value = ElasticsearchOperations.class,
name = "elasticsearchTemplate")
@ConditionalOnBean(RestHighLevelClient.class)
public ElasticsearchRestTemplate elasticsearchTemplate(RestHighLevelClient client,
ElasticsearchConverter converter) {
return new ElasticsearchRestTemplate(client, converter);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Client.class)
static class TransportClientConfiguration {
@Bean
@ConditionalOnMissingBean(value = ElasticsearchOperations.class,
name = "elasticsearchTemplate")
@ConditionalOnBean(Client.class)
public ElasticsearchTemplate elasticsearchTemplate(Client client,
ElasticsearchConverter converter) {
try {
return new ElasticsearchTemplate(client, converter);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
}
}

@ -16,97 +16,102 @@
package org.springframework.boot.autoconfigure.data.elasticsearch;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link ElasticsearchDataAutoConfiguration}.
*
* @author Phillip Webb
* @author Artur Konczak
* @author Brian Clozel
*/
@SuppressWarnings("deprecation")
@Testcontainers
public class ElasticsearchDataAutoConfigurationTests {
@Container
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private AnnotationConfigApplicationContext context;
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchAutoConfiguration.class,
RestClientAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class));
@AfterEach
public void close() {
if (this.context != null) {
this.context.close();
}
@Test
public void defaultTransportBeansAreRegistered() {
this.contextRunner
.withPropertyValues(
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.run((context) -> assertThat(context)
.hasSingleBean(ElasticsearchTemplate.class)
.hasSingleBean(SimpleElasticsearchMappingContext.class)
.hasSingleBean(ElasticsearchConverter.class));
}
@Test
public void templateBackOffWithNoClient() {
this.context = new AnnotationConfigApplicationContext(
ElasticsearchDataAutoConfiguration.class);
assertThat(this.context.getBeansOfType(ElasticsearchTemplate.class)).isEmpty();
public void defaultTransportBeansNotRegisteredIfNoTransportClient() {
this.contextRunner.run((context) -> assertThat(context)
.doesNotHaveBean(ElasticsearchTemplate.class));
}
@Test
public void templateExists() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchTemplate.class);
public void defaultRestBeansRegistered() {
this.contextRunner.run((context) -> assertThat(context)
.hasSingleBean(ElasticsearchRestTemplate.class)
.hasSingleBean(ElasticsearchConverter.class));
}
@Test
public void mappingContextExists() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(SimpleElasticsearchMappingContext.class);
public void customTransportTemplateShouldBeUsed() {
this.contextRunner.withUserConfiguration(CustomTransportTemplate.class)
.run((context) -> assertThat(context)
.getBeanNames(ElasticsearchTemplate.class).hasSize(1)
.contains("elasticsearchTemplate"));
}
@Test
public void converterExists() {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues
.of("spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster")
.applyTo(this.context);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class);
this.context.refresh();
assertHasSingleBean(ElasticsearchConverter.class);
public void customRestTemplateShouldBeUsed() {
this.contextRunner.withUserConfiguration(CustomRestTemplate.class)
.run((context) -> assertThat(context)
.getBeanNames(ElasticsearchRestTemplate.class).hasSize(1)
.contains("elasticsearchTemplate"));
}
@Configuration
static class CustomTransportTemplate {
@Bean
ElasticsearchTemplate elasticsearchTemplate() {
return mock(ElasticsearchTemplate.class);
}
}
@Configuration
static class CustomRestTemplate {
@Bean
ElasticsearchRestTemplate elasticsearchTemplate() {
return mock(ElasticsearchRestTemplate.class);
}
private void assertHasSingleBean(Class<?> type) {
assertThat(this.context.getBeanNamesForType(type)).hasSize(1);
}
}

@ -16,22 +16,21 @@
package org.springframework.boot.autoconfigure.data.elasticsearch;
import org.elasticsearch.client.Client;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.alt.elasticsearch.CityElasticsearchDbRepository;
import org.springframework.boot.autoconfigure.data.elasticsearch.city.City;
import org.springframework.boot.autoconfigure.data.elasticsearch.city.CityRepository;
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import static org.assertj.core.api.Assertions.assertThat;
@ -41,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Brian Clozel
*/
@Testcontainers
public class ElasticsearchRepositoriesAutoConfigurationTests {
@ -48,59 +48,44 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@Container
public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
private AnnotationConfigApplicationContext context;
@AfterEach
public void close() {
this.context.close();
}
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchAutoConfiguration.class,
RestClientAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class))
.withPropertyValues("spring.elasticsearch.rest.uris=localhost:"
+ elasticsearch.getMappedHttpPort());
@Test
public void testDefaultRepositoryConfiguration() {
load(TestConfiguration.class);
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
assertThat(this.context.getBean(Client.class)).isNotNull();
this.contextRunner.withUserConfiguration(TestConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(CityRepository.class)
.hasSingleBean(ElasticsearchRestTemplate.class));
}
@Test
public void testNoRepositoryConfiguration() {
load(EmptyConfiguration.class);
assertThat(this.context.getBean(Client.class)).isNotNull();
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.run((context) -> assertThat(context)
.hasSingleBean(ElasticsearchRestTemplate.class));
}
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
load(CustomizedConfiguration.class);
assertThat(this.context.getBean(CityElasticsearchDbRepository.class)).isNotNull();
}
private void load(Class<?> config) {
this.context = new AnnotationConfigApplicationContext();
addElasticsearchProperties(this.context);
this.context.register(config, ElasticsearchAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
}
private void addElasticsearchProperties(AnnotationConfigApplicationContext context) {
TestPropertyValues.of(
"spring.data.elasticsearch.cluster-nodes:localhost:"
+ elasticsearch.getMappedTransportPort(),
"spring.data.elasticsearch.cluster-name:docker-cluster").applyTo(context);
this.contextRunner.withUserConfiguration(CustomizedConfiguration.class)
.run((context) -> assertThat(context)
.hasSingleBean(CityElasticsearchDbRepository.class));
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(City.class)
protected static class TestConfiguration {
static class TestConfiguration {
}
@Configuration(proxyBeanMethods = false)
@TestAutoConfigurationPackage(EmptyDataPackage.class)
protected static class EmptyConfiguration {
static class EmptyConfiguration {
}
@ -108,7 +93,7 @@ public class ElasticsearchRepositoriesAutoConfigurationTests {
@TestAutoConfigurationPackage(ElasticsearchRepositoriesAutoConfigurationTests.class)
@EnableElasticsearchRepositories(
basePackageClasses = CityElasticsearchDbRepository.class)
protected static class CustomizedConfiguration {
static class CustomizedConfiguration {
}

@ -4932,15 +4932,16 @@ To take full control over the registration, define a `JestClient` bean.
[[boot-features-connecting-to-elasticsearch-spring-data]]
==== Connecting to Elasticsearch by Using Spring Data
To connect to Elasticsearch, you must provide the address of one or more cluster nodes.
The address can be specified by setting the `spring.data.elasticsearch.cluster-nodes`
To connect to Elasticsearch, you must provide the address of one or more Elasticsearch
instances. The address can be specified by setting the `spring.elasticsearch.rest.uris`
property to a comma-separated `host:port` list. With this configuration in place, an
`ElasticsearchTemplate` or `TransportClient` can be injected like any other Spring bean,
`ElasticsearchRestTemplate` or `RestHighLevelClient` can be injected like any other Spring bean,
as shown in the following example:
[source,properties,indent=0]
----
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.elasticsearch.rest.uris=localhost:9200
----
[source,java,indent=0]
@ -4948,9 +4949,9 @@ as shown in the following example:
@Component
public class MyBean {
private final ElasticsearchTemplate template;
private final ElasticsearchRestTemplate template;
public MyBean(ElasticsearchTemplate template) {
public MyBean(ElasticsearchRestTemplate template) {
this.template = template;
}
@ -4959,8 +4960,8 @@ as shown in the following example:
}
----
If you add your own `ElasticsearchTemplate` or `TransportClient` `@Bean`, it replaces the
default.
If you add your own `ElasticsearchRestTemplate` or `ElasticsearchOperations` `@Bean`,
it replaces the default given it is named `"elasticsearchTemplate""`.

@ -26,16 +26,20 @@ import java.time.Duration;
public class ElasticsearchContainer extends Container {
public ElasticsearchContainer() {
super("elasticsearch:6.4.3", 9200,
super("elasticsearch:6.7.2", 9200,
(container) -> container.withStartupTimeout(Duration.ofSeconds(120))
.withStartupAttempts(5).withEnv("discovery.type", "single-node")
.addExposedPort(9300));
.addExposedPorts(9200, 9300));
}
public int getMappedTransportPort() {
return getContainer().getMappedPort(9300);
}
public int getMappedHttpPort() {
return getContainer().getMappedPort(9200);
}
@Override
public void start() {
System.setProperty("es.set.netty.runtime.available.processors", "false");

@ -1 +1 @@
spring.data.elasticsearch.cluster-nodes=localhost:9200
spring.elasticsearch.rest.uris=localhost:9200

@ -16,7 +16,8 @@
package sample.data.elasticsearch;
import org.elasticsearch.client.transport.NoNodeAvailableException;
import java.net.ConnectException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -51,7 +52,7 @@ class SampleElasticsearchApplicationTests {
private boolean elasticsearchRunning(Exception ex) {
Throwable candidate = ex;
while (candidate != null) {
if (candidate instanceof NoNodeAvailableException) {
if (candidate instanceof ConnectException) {
return false;
}
candidate = candidate.getCause();

Loading…
Cancel
Save