【问题标题】:Failed to load class "org.slf4j.impl.StaticLoggerBinder" in kafka stream?无法在 kafka 流中加载类“org.slf4j.impl.StaticLoggerBinder”?
【发布时间】:2019-05-26 09:16:10
【问题描述】:

我正在 GCP 机器中用 Java 实现 apache kafka 流应用程序。我正在使用 kafka 2.12 版 参考网址 - https://kafka.apache.org/22/documentation/streams/tutorial

在这里我可以设置 maven 项目。然后我可以在我的 kafka 目录中看到 stream.example 的树结构。在下一步中,我正在尝试执行 Pipe java 类但是​​在这里我卡在了这一步。 我能够执行使构建成功的 mvn clean package。 下一个命令是 mvn exec:java -Dexec.mainClass=myapps.Pipe 正在发出警告并卡住了...见附件截图。

这里我们可以看到我的 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>streams.examples</groupId>
<artifactId>streams.examples</artifactId>
<version>0.1</version>
<packaging>jar</packaging>

<name>Kafka Streams Quickstart :: Java</name>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <kafka.version>2.2.0</kafka.version>
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
</properties>

<repositories>
    <repository>
        <id>apache.snapshots</id>
        <name>Apache Development Snapshot Repository</name>
        <url>https://repository.apache.org/content/repositories/snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<!--
            Execute "mvn clean package -Pbuild-jar"
            to build a jar file out of this project!
    -->

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>  
             <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerId>jdt</compilerId>
                </configuration>
                <dependencies>
                     <dependency>
                         <groupId>org.slf4j</groupId>
                         <artifactId>slf4j-simple</artifactId>
                         <version>1.7.25</version>
                     </dependency>
                     <dependency>
                        <groupId>org.eclipse.tycho</groupId>
                        <artifactId>tycho-compiler-jdt</artifactId>
                        <version>0.21.0</version>
                    </dependency>
                    </dependencies>
            </plugin>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-assembly-plugin</artifactId>
                                    <versionRange>[2.4,)</versionRange>
                                    <goals>
                                        <goal>single</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore/>
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-compiler-plugin</artifactId>
                                    <versionRange>[3.1,)</versionRange>
                                    <goals>
                                        <goal>testCompile</goal>
                                        <goal>compile</goal>
                                    </goals>
                                    </pluginExecutionFilter>
                                <action>
                                    <ignore/>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>
    <!-- Apache Kafka dependencies -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams</artifactId>
        <version>${kafka.version}</version>
    </dependency>
</dependencies>

【问题讨论】:

    标签: java maven apache-kafka slf4j apache-kafka-streams


    【解决方案1】:

    为了更清楚,请首先参考以下链接。

    StaticLogBinder

    引用,它说

    当 org.slf4j.impl.StaticLoggerBinder 类无法加载到 记忆。当找不到合适的 SLF4J 绑定时会发生这种情况 在类路径上。放置一个(也是唯一一个)slf4j-nop.jar slf4j-simple.jar、slf4j-log4j12.jar、slf4j-jdk14.jar 或 类路径上的 logback-classic.jar 应该可以解决问题。

    作为解决方案的一部分,我建议您使用 logback。

    在maven pom.xml中,添加如下依赖。

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.26</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    

    创建一个 logback.xml 文件并将其放在你的项目 src/main/resources 中

    如果您不想使用 logback,那么只需将以下依赖项与 slf4j api 一起添加即可。

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-nop</artifactId>
        <version>1.7.26</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 2011-11-17
      • 2017-02-19
      • 1970-01-01
      相关资源
      最近更新 更多