【问题标题】:Elasticsearch java client initialization failsElasticsearch java客户端初始化失败
【发布时间】:2019-05-05 18:12:32
【问题描述】:

我在尝试运行连接到 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());

【问题讨论】:

    标签: java maven spring-boot elasticsearch


    【解决方案1】:

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

    @EnableAutoConfiguration(exclude={ElasticsearchAutoConfiguration.class, RestClientAutoConfiguration.class})
    

    【讨论】:

    • ElasticsearchRestHealthIndicator 仍然失败 - 我不得不禁用它
    • 谢谢。就我而言,不包括 @EnableAutoConfiguration(exclude = {ElasticsearchRestClientAutoConfiguration.class}) 有效。
    【解决方案2】:

    通过查看异常

    java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)
    

    RestHighLevelClient 中没有将RestClientBuilder 作为&lt;version&gt;5.6.3&lt;/version&gt; 中的参数的构造函数。

    您是否尝试使用版本&lt;version&gt;7.0.0-alpha1&lt;/version&gt;

    更新:

    异常An attempt was made to call the method org.elasticsearch.client.RestHighLevelClient.&lt;init&gt;(Lorg/elasticsearch/client/RestClientBuilder; 告诉代码它正在尝试执行属于 elasticsearch 版本 6 的方法。 在您的情况下,您可能在运行时提供了多个版本的 Elasticsearch 库,或者您的代码可能由版本 6 编译,但在运行时 提供了第 5 版。

    【讨论】:

    • 我使用的是5.6.3,因为底层elasticsearch集群的版本是5.6.3。但是如果版本是问题,它应该不会编译并抛出错误吧?
    • 确实,只有 Elasticsearch 版本 6+ org.Elasticsearch.client.RestHighLevelClient 允许您将 org.Elasticsearch.client.RestClient 作为构造函数参数传递。 Elasticsearch 版本 5 没有这样的构造函数。
    • 根据这些文档,它有这样一个构造函数 - artifacts.elastic.co/javadoc/org/elasticsearch/client/…
    • 更正org.Elasticsearch.client.RestClient应该是org.Elasticsearch.clien.RestClientBuilder
    • 正如我在您的文档 artifacts.elastic.co/javadoc/org/elasticsearch/client/… 中所说的,没有构造函数可以将 org/elasticsearch/client/RestClientBuilder 传递给 RestHighLevelClient
    【解决方案3】:

    如果您使用的是 Spring Boot 2.3.x + Elasticsearch 客户端 5.6.6,这里有一个配置参考,看看它是否有帮助。
    我们最近将 Spring Boot 升级到 2.3.3,而 ELK 仍在 5.6.6 上。错误是关于 Spring 自动配置 ES RestClient,所以另一种解决方案是“手动配置”,以后可以在其他地方使用。

    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:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2016-12-25
      • 2011-02-17
      • 2017-07-05
      相关资源
      最近更新 更多