Spring 启动 2.0.0.M2 和 Spring 数据 Elasticsearch 配置
Spring Boot 2.0.0.M2 and Spring Data Elasticsearch configuration
我正在尝试将我的项目移动到 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
:
如何正确配置 Spring Data Elasticsearch 的当前版本?
已更新
对于我的测试,我使用带有以下内容的嵌入式 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]
这里有什么问题以及如何解决这个问题?
Spring Boot 2.0 使用 Elasticsearch 5,其中包含一些重大的 API 更改。如果您使用 Spring Boot 的自动配置而不是尝试编写您自己的配置,您将不会受到这些更改的影响。
所需要的只是 spring.data.elasticsearch.cluster-nodes
属性 的值。有了它,Spring Boot 将自动配置 TransportClient
和 ElasticsearchTemplate
.
从 spring-boot 1.5.6 迁移到 2.0.0 时遇到类似问题。原因似乎是以前对嵌入式elasticsearch的支持不多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 准确地说)cluster-nodes 已断言 non-null 值导致 elasticsearchTemplate bean 不在后台创建。
这就是您的应用突然停止运行的原因。
我正在尝试将我的项目移动到 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
:
如何正确配置 Spring Data Elasticsearch 的当前版本?
已更新
对于我的测试,我使用带有以下内容的嵌入式 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]
这里有什么问题以及如何解决这个问题?
Spring Boot 2.0 使用 Elasticsearch 5,其中包含一些重大的 API 更改。如果您使用 Spring Boot 的自动配置而不是尝试编写您自己的配置,您将不会受到这些更改的影响。
所需要的只是 spring.data.elasticsearch.cluster-nodes
属性 的值。有了它,Spring Boot 将自动配置 TransportClient
和 ElasticsearchTemplate
.
从 spring-boot 1.5.6 迁移到 2.0.0 时遇到类似问题。原因似乎是以前对嵌入式elasticsearch的支持不多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 准确地说)cluster-nodes 已断言 non-null 值导致 elasticsearchTemplate bean 不在后台创建。
这就是您的应用突然停止运行的原因。