Spring Boot 2.0.0.M2和Spring Data Elasticsearch配置

时间:2017-07-23 08:22:28

标签: spring maven spring-boot spring-data-elasticsearch

我正在尝试将项目移至Spring Boot 2.0.0.M2

这是我以前的Spring Data Elasticsearch配置:

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Profile("production")
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.domain.repository.elasticsearch")
public class ElasticsearchConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Bean
    public Client client() throws Exception {
        return TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }

}

现在我面临以下问题:

1. The method builder() is undefined for the type TransportClient
2. InetSocketTransportAddress cannot be resolved to a type

在Maven类路径上,我有Spring Data Elasticsearch 3.0.0.M4

enter image description here

如何正确配置Spring Data Elasticsearch的当前版本?

已更新

对于我的测试,我使用Embedded Elasticsearch和以下application.properties:

#Elasticsearch
spring.data.elasticsearch.properties.http.enabled=true
spring.data.elasticsearch.properties.http.port=9250
spring.data.elasticsearch.properties.path.home=target/test-elasticsearch-db
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=60s

这是我的ES测试配置:

@Profile("test")
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.domain.repository.elasticsearch")
public class ElasticsearchTestConfig {
}

现在测试失败并出现以下错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.elasticsearch.client.Client' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:726) ~[spring-beans-5.0.0.RC2.jar:5.0.0.RC2]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458) ~[spring-beans-5.0.0.RC2.jar:5.0.0.RC2]

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.elasticsearch.client.Client' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1478) ~[spring-beans-5.0.0.RC2.jar:5.0.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1089) ~[spring-beans-5.0.0.RC2.jar:5.0.0.RC2]

这里有什么问题以及如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

Spring Boot 2.0使用Elasticsearch 5,其中包含一些重大的API更改。如果您使用Spring Boot的自动配置而不是尝试自己编写,那么您将不受这些更改的影响。

所需要的只是spring.data.elasticsearch.cluster-nodes属性的值。有了这个,Spring Boot将自动配置TransportClientElasticsearchTemplate

答案 1 :(得分:1)

从spring-boot 1.5.6迁移到2.0.0时遇到类似问题。原因似乎是先前对嵌入式弹性搜索的支持不仅仅是supported,而且在spring-boot中也反映出来。

以前将以下属性留空

spring.data.elasticsearch.cluster-nodes

使用以下内容

spring.data.elasticsearch.properties.path.home

和spring-boot在目标文件夹中创建给定目录,以便运行嵌入式模式。使用spring-boot 2.0.0(准确地说是spring-boot-autoconfigure-2.0.0.RELEASE.jar),群集节点已经变为非空值,因此无法在引擎盖下创建elasticsearchTemplate bean。

这就是为什么你的应用程序突然停止工作的原因。