Dubbo + zookeeper 引起日志log4j冲突

1,406 阅读1分钟

前言

小试Dubbo+zookeeper,出现了log4j包冲突,原因是zookeeper的日志包和 dubbo、springboot的冲突了。

解决步骤

SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory
	at org.springframework.util.Assert.instanceCheckFailed(Assert.java:696)
	at org.springframework.util.Assert.isInstanceOf(Assert.java:596)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:281)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:104)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:239)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
	at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:70)
	at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:47)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at com.demo.dubbo.App.main(App.java:16)

mvn dependency:tree 打印 pom 的依赖树。

+- org.apache.zookeeper:zookeeper:jar:3.6.0:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.6:compile
[INFO] |  +- org.apache.zookeeper:zookeeper-jute:jar:3.6.0:compile
[INFO] |  +- org.apache.yetus:audience-annotations:jar:0.5.0:compile
[INFO] |  +- io.netty:netty-handler:jar:4.1.43.Final:compile
[INFO] |  |  +- io.netty:netty-common:jar:4.1.43.Final:compile
[INFO] |  |  +- io.netty:netty-buffer:jar:4.1.43.Final:compile
[INFO] |  |  +- io.netty:netty-transport:jar:4.1.43.Final:compile
[INFO] |  |  |  \- io.netty:netty-resolver:jar:4.1.43.Final:compile
[INFO] |  |  \- io.netty:netty-codec:jar:4.1.43.Final:compile
[INFO] |  +- io.netty:netty-transport-native-epoll:jar:4.1.43.Final:compile
[INFO] |  |  \- io.netty:netty-transport-native-unix-common:jar:4.1.43.Final:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.29:compile         // 删除
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.29:compile     // 删除
[INFO] |  \- log4j:log4j:jar:1.2.16:compile

根据报错,找到slf4j的依赖,通过exclusions标签去掉。

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.6.0</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

完毕。