首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch java客户端初始化失败

Elasticsearch java客户端初始化失败
EN

Stack Overflow用户
提问于 2018-12-04 10:31:27
回答 3查看 10.3K关注 0票数 5

当我试图运行一个连接到elasticsearch的应用程序时,我得到了这个错误消息。

代码语言:javascript
复制
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。

这是我的pom的相关部分:

代码语言:javascript
复制
    <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>

这是我在运行应用程序时遇到的异常:

代码语言:javascript
复制
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的:

代码语言:javascript
复制
RestClientBuilder builder = RestClient
                .builder(new HttpHost(hostname, port, scheme));
builder.setMaxRetryTimeoutMillis(timeout);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder.build());
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-07 05:14:07

Spring将尝试自动配置elasticsearch,这将在内部使用弹性6 RestHighLevelClient(org.elasticsearch.client.RestClientBuilder构建器)来创建弹性客户端。如果您想连接到旧版本的elasticsearch,请排除elasticsearch自动配置。

代码语言:javascript
复制
@EnableAutoConfiguration(exclude={ElasticsearchAutoConfiguration.class, RestClientAutoConfiguration.class})
票数 17
EN

Stack Overflow用户

发布于 2018-12-04 11:17:48

通过观察异常

代码语言:javascript
复制
java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)

RestHighLevelClient中没有构造函数,它在<version>5.6.3</version>中将RestClientBuilder作为参数。

你试过使用<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。

票数 1
EN

Stack Overflow用户

发布于 2020-09-20 22:00:39

如果您使用SpringBoot2.3.x+ Elasticsearch客户端5.6.6,下面是配置的参考,看看它是否有用。

我们最近将Spring升级为2.3.3,而ELK仍在5.6.6。这个错误与Spring自动配置ES RestClient有关,因此另一个解决方案是“手动配置它”,以后可以在其他地方使用。

代码语言:javascript
复制
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);
//  }
}

pom:

代码语言:javascript
复制
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath /> 
</parent>
<!-- ..................  -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.6.6</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>5.6.6</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>5.6.6</version>
</dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53610891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档