尝试运行连接到Elasticsearch的应用程序时出现此错误消息。
An attempt was made to call the method org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)V but it does not exist. Its class, org.elasticsearch.client.RestHighLevelClient, is available from the following locations:
jar:file:/path/application/target/application-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/elasticsearch-rest-high-level-client-5.6.3.jar!/org/elasticsearch/client/RestHighLevelClient.class
It was loaded from the following location:
jar:file:/path/application/target/application-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/elasticsearch-rest-high-level-client-5.6.3.jar!/
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.elasticsearch.client.RestHighLevelClient
该应用程序的构建没有错误,并且我的maven存储库中只有一个版本的elasticsearch SDK。
这是我pom的相关部分:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath />
</parent>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>5.6.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
这是我在运行应用程序时遇到的异常:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)V
这是我初始化RestHighLevelClient的方式:
RestClientBuilder builder = RestClient
.builder(new HttpHost(hostname, port, scheme));
builder.setMaxRetryTimeoutMillis(timeout);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder.build());
答案 0 :(得分:3)
Spring Boot将尝试自动配置elasticsearch,该搜索将在内部使用Elastic 6 RestHighLevelClient(org.elasticsearch.client.RestClientBuilder builder)创建弹性客户端。如果要连接到旧版本的elasticsearch,请排除elasticsearch自动配置。
@EnableAutoConfiguration(exclude={ElasticsearchAutoConfiguration.class, RestClientAutoConfiguration.class})
答案 1 :(得分:0)
通过查看异常
java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)
RestHighLevelClient
中没有构造函数,RestClientBuilder
中没有<version>5.6.3</version>
作为参数。
您尝试使用版本<version>7.0.0-alpha1</version>
吗?
更新:
异常An attempt was made to call the method org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;
告诉代码它是在尝试执行属于elasticsearch版本6的方法。
在您的情况下,也许您在运行时提供了多个版本的Elasticsearch库,或者您的代码可能已被版本6编译,但在运行时
提供了版本5。
答案 2 :(得分:0)
如果您使用的是Spring Boot 2.3.x + Elasticsearch客户端5.6.6,请参考以下配置,以了解是否有帮助。
我们最近将Spring Boot升级到2.3.3,而ELK仍在5.6.6上。该错误与Spring自动配置ES tfp.distributions.HiddenMarkovModel()
有关,因此另一种解决方案是“手动配置”,并且可以在以后的其他地方使用。
RestClient
pom:
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EsConfig {
@Value("${elasticsearch.host}")
private String esHost;
@Value("${elasticsearch.port}")
private int esPort;
@Value("${elasticsearch.prefix}")
private String esPrefix;
@Bean
public RestClient lowLevelClient() {
return RestClient.builder(new HttpHost(esHost, esPort, "http")).setPathPrefix(esPrefix)
.setRequestConfigCallback(builder -> builder.setConnectTimeout(1000).setSocketTimeout(5000)).build();
}
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(this.lowLevelClient());
}
// ------ before upgrade
// @Bean
// public RestHighLevelClient restHighLevelClient() {
// RestClient rc = RestClient.builder(new HttpHost(esHost, esPort, "http")).setPathPrefix(esPrefix)
// .setRequestConfigCallback(builder -> builder.setConnectTimeout(1000).setSocketTimeout(5000)).build();
// return new RestHighLevelClient(rc);
// }
}