Springboot with Elasticsearch 5.0 client - Log4j2

2019-07-21 21:03发布

问题:

I am trying to use Elasticsearch 5.0 transport client with a sprintboot application. Elasticsearch 5.0 client uses log4j2 and when I launch my springboot service, ES client throws error as it could not find log4j2 classes. I have tried many combinations but no luck yet. Has anybody used ES 5 client within a springboot application?

Here is my 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>

Error ..

 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

回答1:

Please add dependencies

  1. log4j-api
  2. log4j-core
  3. log4j-slf4j-impl

Currently the latest version is 2.8.

You can remove the log4j-over-slf4j dependency, this is for the old Log4j 1.2.



回答2:

What if you have your dependency as this with the version :

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

Source



回答3:

I am using spring boot with ES client. Try the following dependencies

<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> 

Also in the properties in your pom.xml add the following

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


回答4:

Check this link, apparently no Springboot release has been done yet supporting elastic search 5.0.0 version, you might want to downgrade your es version to 2.xx.xx

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