java无法连接7.7.1版本elasticsearch

最近在学习es的过程中遇到一些问题,特来记录一下

前序

因为笔者是mac笔记本,所以直接用brew安装的elasticsearch,是7.7.1版本

image-20200618141122805

查看es的官网文档可以看到7.7.1版本的maven依赖,官方推荐使用high-level的版本

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.7.1</version>
</dependency>

然后我们再看他的初始化配置

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));

我们在java项目中写一个配置类,如下

@Configuration
public class GulimallElasticSearchConfig {

    @Bean
    public RestHighLevelClient esRestClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http")));
        return  client;
    }
}

然后基本的yml配置

server:
  port:9001


spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: search

然后启动

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'esRestClient' defined in class path resource [com/atguigu/gulimall/search/config/GulimallElasticSearchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'esRestClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
	
image-20200618141907712

发现其报错了

其中最主要的一句报错就是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'esRestClient' defined in class path resource [com/atguigu/gulimall/search/config/GulimallElasticSearchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'esRestClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

解决

笔者查看了一些stackoverflow,具体网址

不仅需要加这个依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.7.1</version>
</dependency>

仍然需要加这两个依赖

		<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.7.1</version>
        </dependency>

然后重新clean,install,重启

image-20200618143808157

可见已经启动

WRITTEN BY: