elasticsearch初始化SearchSourceBuilder失败问题

1 问题表现

  1. 本地测试没有一切正常,一旦发布到服务器请求的时候会出现如下异常:
java 复制代码
类型 异常报告

消息 Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.search.builder.SearchSourceBuilder

描述 服务器遇到一个意外的情况,阻止它完成请求。

例外情况

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.search.builder.SearchSourceBuilder
	org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1305)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:979)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:529)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
根本原因。

java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.search.builder.SearchSourceBuilder
	com.baidu.bpit.kylin.core.es.EsClientUtils.searchIndex(EsClientUtils.java:583)
	com.baidu.bpit.kylin.core.web.KylinApiController.searchES(KylinApiController.java:286)
	java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.base/java.lang.reflect.Method.invoke(Method.java:566)
	org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:178)
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:529)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
根本原因。

java.lang.ExceptionInInitializerError: Exception java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager [in thread "http-nio-8922-exec-2"]
	org.elasticsearch.search.builder.SearchSourceBuilder.<clinit>(SearchSourceBuilder.java:84)
	com.baidu.bpit.kylin.core.es.EsClientUtils.searchIndex(EsClientUtils.java:583)
	com.baidu.bpit.kylin.core.web.KylinApiController.searchES(KylinApiController.java:286)
	java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.base/java.lang.reflect.Method.invoke(Method.java:566)
	org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:178)
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:529)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

2 原因分析

  1. 根据服务启动的报第一次这个java.lang.NoClassDefFoundError可知,jvm本来是要加载 SearchSourceBuilder 这个类,这个类初始化时却依赖了 log4j 的 API,而 log4j 的依赖包在项目lib里面有缺失,导致最后类 SearchSourceBuilder 一直无法初始化。

3 解决方案

解决办法是升级pom的log配置,如下:

XML 复制代码
<slf4j.version>2.0.16</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <log4j.logging.version>2.17.1</log4j.logging.version>       



<!-- logging begin -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-web</artifactId>
                <version>${log4j.logging.version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>log4j-api</artifactId>
                        <groupId>org.apache.logging.log4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j-core</artifactId>
                        <groupId>org.apache.logging.log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${log4j.logging.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>${log4j.logging.version}</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <artifactId>log4j-api</artifactId>
                        <groupId>org.apache.logging.log4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j-core</artifactId>
                        <groupId>org.apache.logging.log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${log4j.logging.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.25</version>
                <scope>compile</scope>
            </dependency>
            <!-- logging end -->

升级后实测问题解决

相关推荐
huaqianzkh18 分钟前
了解Hadoop:大数据处理的核心框架
大数据·hadoop·分布式
Kika写代码43 分钟前
【Hadoop】【hdfs】【大数据技术基础】实验三 HDFS 基础编程实验
大数据·hadoop·hdfs
okmacong3 小时前
2024.11.12_大数据的诞生以及解决的问题
大数据
Java资深爱好者5 小时前
数据湖与数据仓库的区别
大数据·数据仓库·spark
heromps5 小时前
hadoop报错找不到主类
大数据·hadoop·eclipse
未 顾7 小时前
day12:版本控制器
大数据·elasticsearch·搜索引擎
CherishTaoTao7 小时前
Git别名设置
大数据·git
Dreams°1238 小时前
【大数据测试HBase数据库 — 详细教程(含实例与监控调优)】
大数据·功能测试·单元测试
Elastic 中国社区官方博客12 小时前
Lucene 和 Elasticsearch 中更好的二进制量化 (BBQ)
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索·lucene