SpringBoot整合ElasticSearch8.x 踩坑记录

背景

jdk版本 openjdk-17
springboot版本 2.6.11

pom.xml

<!-- ElasticSearch提供的依赖 -->
<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.6.2</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.2</version>
</dependency>

<dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.1.1</version>
</dependency>

报错语句

java.lang.IllegalStateException: Failed to load ApplicationContext

''''' 中间省略 ''''''

Caused by: java.lang.NoSuchMethodError: 'org.elasticsearch.client.RequestOptions$Builder org.elasticsearch.client.RequestOptions$Builder.removeHeader(java.lang.String)'
	at co.elastic.clients.transport.rest_client.RestClientOptions.addBuiltinHeaders(RestClientOptions.java:170)
	at co.elastic.clients.transport.rest_client.RestClientOptions.<init>(RestClientOptions.java:64)
	at com.noking.search.config.MallElasticSearchConfig.esRestClient(MallElasticSearchConfig.java:34)
	at com.noking.search.config.MallElasticSearchConfig$$EnhancerBySpringCGLIB$$620ce3f7.CGLIB$esRestClient$0(<generated>)
	at com.noking.search.config.MallElasticSearchConfig$$EnhancerBySpringCGLIB$$620ce3f7$$FastClassBySpringCGLIB$$338beeeb.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
	at com.noking.search.config.MallElasticSearchConfig$$EnhancerBySpringCGLIB$$620ce3f7.esRestClient(<generated>)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)

排查过程

ElasticSearch装得是8.6.2版本的,使用Spring整合的时候,发现一直报一个RequestOptions.Builder类中removeHeader方法不存在的错误,查看源码发现,哎我去,真有问题,源码中没有这个方法,看到这,马上想到肯定是我的问题,官方包怎么可能有问题。

然后去排查是不是自己导包的版本有问题,查阅官方文档后,和官方一样,没问题。

随后进一步查看源码,发现elasticsearch-java包中的RestClientTransport类调用了一个名叫elasticsearch-rest-client的包中的方法,而这个包的版本是7.15.2。看到这个版本号,就知道问题肯定出在这。继续排查,得知这个包是由SpringBoot维护的,只需要在pom.xml中显示得引用8.6.2版本来覆盖掉springboot维护的就可以解决问题。

具体代码如下:

RestClientTransport.java

image-20230411174109673

RestClientOptions.java

image-20230411174121524

image-20230411173739844

解决办法

依赖中引入elasticsearch-rest-client:8.6.2覆盖springboot维护的包,最终pom.xml如下:

<!-- ElasticSearch提供的依赖 -->
<dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>8.6.2</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.2</version>
</dependency>

<dependency>
    <groupId>jakarta.json</groupId>
    <artifactId>jakarta.json-api</artifactId>
    <version>2.1.1</version>
</dependency>

<!-- 覆盖springboot维护的7.x版本 -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>8.6.2</version>
</dependency>

over!

posted @ 2023-04-11 17:46  浪漫主义程序员  阅读(1981)  评论(0编辑  收藏  举报