问题
之前在做SpringBoot整合Elasticsearch客户端的时候遇到下面的问题
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'highLevelClient' defined in class path resource [cn/paohe/framework/configs/EsConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'highLevelClient' threw exception;
这个是因为SpringBoot中默认Elasticsearch包版本引起的。
从上图中可以看出Elasticsearch使用的是7.5.2,再看看Eclipse引入的是什么
由于使用的SpringBoot版本是2.0.3版本,默认引用的Elasticsearch版本是5.6.10,这时在启动的时候就会产生版本不一致的情况,没有找到类。
那要怎样做呢?直接在pom中加入Elasticsearch7.5.2版本包就可以了么?
NO
直接加上是不行的
在pom中将其加入后
可以看出Maven还是5.6.10替换了7.5.2,这个时候就需要手动先排除掉5.6.10版本后再加上7.5.2版本才行
即使version标签存在警告也不要管,不然不填7.5.2版本号又将会重新引入5.6.10版本了。
结论
Elasticsearch版本号要与Rest Client的版本号一致,而Maven不一定会引用正确的需要时刻留意pom依赖树展示的内容。