java - 无法在kafka流中加载类 "org.slf4j.impl.StaticLoggerBinder"?

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

我正在 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 发出警告并被卡住...请参阅随附的屏幕截图。 kafka mvn command error screenshot

在这里我们可以看到我的 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>

最佳答案

为了更清楚起见,请先引用以下链接。

StaticLogBinder

要引用,它说

This warning message is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

作为解决方案的一部分,我建议您使用 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>

关于java - 无法在kafka流中加载类 "org.slf4j.impl.StaticLoggerBinder"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312197/

相关文章:

maven - 如何在 Maven 中运行单个测试?

java - 在 Java 9 迁移期间未找到不兼容的类型、等式约束和方法

java - 通过时间戳创建,让数据库管理它更好,还是让应用程序管理更好?

Java正则表达式模式匹配花费太多时间

java - 字典顺序快速排序

rest - 无法为 Kafka Connect REST API 配置 SSL

ssl - Apache kafka 消费者 java.security.cert.CertificateException : No subject alternative names present

java - Play Framework Java - 包 play.db 不存在

java - Jenkins 无法构建 Maven 项目

java - 仅 Spring Kafka 事务消费者