我在连接到远程弹性搜索服务器时使用spring-boot + spring-data-elasticsearch在启动时遇到此异常:
Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.elasticsearch.client.Client]: Factory method 'elasticsearchClient' threw exception;
nested exception is java.lang.NoSuchMethodError: org.elasticsearch.common.settings.Settings.settingsBuilder()Lorg/elasticsearch/common/settings/Settings$Builder;
我的相关gradle deps是:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.5.8.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-elasticsearch', version: '3.0.1.RELEASE'
compile('org.elasticsearch.client:transport:5.5.0')
Elasticsearch 5.5.0是服务器版本。我遇到了与5.2.2和5.6.4相同的问题。我也尝试使用最新的spring-boot里程碑2.0.0.M6。
相关代码如下,紧跟弹性搜索docs:
@Configuration
@EnableElasticsearchRepositories("com.me.repository.elasticsearch")
public class ElasticSearchConfig {
@Value("${spring.data.elasticsearch.cluster-name:elasticsearch}")
private String clusterName;
@Value("${spring.data.elasticsearch.cluster-nodes:es-server:9300}")
private String clusterNodes;
@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
String server = clusterNodes.split(":")[0];
Integer port = Integer.parseInt(clusterNodes.split(":")[1]);
Settings settings = Settings.builder().put("cluster.name", clusterName).build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port));
return new ElasticsearchTemplate(client);
}
}
我在ElasticSearch论坛here上看到了这个问题的提及,并且描述了Spring引用已弃用的settingsBuilder()方法的问题,但没有提供解决方案。根据{{3}},我使用了正确版本的spring-data-elasticsearch,给出了我的ElasticSearch版本。
有趣的是,我发现在Spring启动之前,在抛出异常之前建立了与elasticsearch服务器的连接。