使用Elasticsearch 5.0客户端的Springboot - Log4j2错误

时间:2016-12-17 03:27:35

标签: java maven elasticsearch spring-boot log4j

我正在尝试将Elasticsearch 5.0传输客户端与sprintboot应用程序一起使用。 Elasticsearch 5.0客户端使用log4j2,当我启动springboot服务时,ES客户端抛出错误,因为找不到log4j2类。我尝试了很多组合,但没有运气。 有人在springboot应用程序中使用过ES 5客户端吗?

这是我的pom

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

错误..

 Caused by: java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:105)
    at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:72)
    at org.elasticsearch.common.component.AbstractComponent.<init>(AbstractComponent.java:37)
    at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:110)
    at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:81)
    at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:106)
    at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:228)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:69)
    at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:65)
    at com.qualys.service.config.IndexingServiceConfiguration.indexingClient(IndexingServiceConfiguration.java:149)
    at com.qualys.service.config.IndexingServiceConfiguration$$EnhancerBySpringCGLIB$$67ddb691.CGLIB$indexingClient$1(<generated>)
    at com.qualys.service.config.IndexingServiceConfiguration$$EnhancerBySpringCGLIB$$67ddb691$$FastClassBySpringCGLIB$$806f0fda.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at com.qualys.service.config.IndexingServiceConfiguration$$EnhancerBySpringCGLIB$$67ddb691.indexingClient(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 38 more
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 58 more

4 个答案:

答案 0 :(得分:2)

请添加依赖项

  1. log4j-api
  2. log4j的核
  3. 的log4j-SLF4J-IMPL
  4. 目前最新版本为2.8。

    您可以删除log4j-over-slf4j依赖项,这适用于旧的Log4j 1.2。

答案 1 :(得分:1)

如果您使用dependency

获得version,该怎么办?
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <version>1.2.3.RELEASE</version>
</dependency>

Source

答案 2 :(得分:0)

我正在使用ES客户端的spring boot。 尝试以下依赖项

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!--elastic search client-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.0.1</version>
        </dependency>

        <!--log 4j-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies> 

同样在pom.xml的属性中添加以下内容

<properties>
        <elasticsearch.version>5.0.0</elasticsearch.version>
</properties>

答案 3 :(得分:0)

检查此链接,显然尚未完成Springboot发布,支持弹性搜索5.0.0版本,您可能希望将es版本降级为2.xx.xx

https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix