dubbo错误java.lang.ClassNotFoundException: org.apache.log4j.Logger

dubbo | 2019-09-13 10:02:39

我的框架是spring boot+dubbo,然后在用dubbo的时候报错:

Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at org.I0Itec.zkclient.ZkClient.<clinit>(ZkClient.java:63) ~[zkclient-0.7.jar:0.7]
        at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.<init>(ZkclientZookeeperClient.java:25) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperTransporter.connect(ZkclientZookeeperTransporter.java:10) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter$Adpative.connect(ZookeeperTransporter$Adpative.java) ~[na:2.5.3]
        at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.<init>(ZookeeperRegistry.java:71) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistryFactory.createRegistry(ZookeeperRegistryFactory.java:37) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:94) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.registry.RegistryFactory$Adpative.getRegistry(RegistryFactory$Adpative.java) ~[na:2.5.3]
        at com.alibaba.dubbo.registry.integration.RegistryProtocol.getRegistry(RegistryProtocol.java:190) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:109) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:53) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:54) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.rpc.Protocol$Adpative.export(Protocol$Adpative.java) ~[na:2.5.3]
        at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:485) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:281) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:242) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:143) ~[dubbo-2.5.3.jar:2.5.3]
        at com.alibaba.dubbo.config.spring.AnnotationBean.postProcessAfterInitialization(AnnotationBean.java:195) ~[dubbo-2.5.3.jar:2.5.3]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:438) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1714) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
        ... 20 common frames omitted


很显然dubbo使用了 log4j。
而你的框架可能是其他的日志系统

解决方案:

pom引入:

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.25</version>
</dependency>


引入这个jar就可以了,从异常上来看,依赖的dubbo需要的class  “org.apache.log4j.Log4jLoggerFactory”在log4j-over-slf4j中,而当前项目的日志框架的运行不在是单一日志框架下的slf4j运行模式。查阅slf4j关于bridge介绍得知,log4j-over-slf4j与slf4j-log412无法共存。单一日志框架下的slf4j解决方案。当一个项目存在多种日志框架混合的时候,slf4j提供bridge模式来支持。


PS:slf4j的bridge介绍

一些公司依赖一些具体log框架的API而不是依赖slf4j,而且,在一段时间后的未来,不会选择切换到slf4j。为了支持这种场景,slf4j提供一种桥接的方式支持log4j,jcl,java.util.logging下的API并且将其转化为slf4j实例的API。简单来说就像搭了个桥来支持不同的日志框架。



登录后即可回复 登录 | 注册
    
关注编程学问公众号