Quick Tour
先决条件:您必须安装并运行 Apache Kafka。然后,你必须将 Spring for Apache Kafka (spring-kafka
) JAR 及其所有依赖项放在你的 classpath 中。最简单的方法是为你的构建工具申明一个依赖项。
Prerequisites: You must install and run Apache Kafka.
Then you must put the Spring for Apache Kafka (spring-kafka
) JAR and all of its dependencies on your classpath.
The easiest way to do that is to declare a dependency in your build tool.
如果你没有使用 Spring Boot,则在你的项目中申明 spring-kafka
jar 作为依赖项。
If you are not using Spring Boot, declare the spring-kafka
jar as a dependency in your project.
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>{project-version}</version>
</dependency>
compile 'org.springframework.kafka:spring-kafka:{project-version}'
使用 Spring Boot 时(并且您尚未使用 start.spring.io 创建您的项目),省略版本,Boot 将自动引入与您的 Boot 版本兼容的正确版本:
When using Spring Boot, (and you haven’t used start.spring.io to create your project), omit the version and Boot will automatically bring in the correct version that is compatible with your Boot version:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
implementation 'org.springframework.kafka:spring-kafka'
但是,最快的入门方法是使用 @"14"(或 Spring Tool Suits 和 Intellij IDEA 中的向导)并创建一个项目,选择“适用于 Apache Kafka 的 Spring”作为依赖项。
However, the quickest way to get started is to use start.spring.io (or the wizards in Spring Tool Suits and Intellij IDEA) and create a project, selecting 'Spring for Apache Kafka' as a dependency.
Compatibility
此快速游览适用于以下版本:
This quick tour works with the following versions:
-
Apache Kafka Clients 3.6.x
-
Spring Framework 6.1.x
-
Minimum Java version: 17
Getting Started
最简单的入门方法是使用 @"15"(或 Spring Tool Suits 和 Intellij IDEA 中的向导)并创建一个项目,选择“适用于 Apache Kafka 的 Spring”作为依赖项。有关其基础结构 bean 的自主自动配置的详细信息,请参阅 @"16"。
The simplest way to get started is to use start.spring.io (or the wizards in Spring Tool Suits and Intellij IDEA) and create a project, selecting 'Spring for Apache Kafka' as a dependency. Refer to the Spring Boot documentation for more information about its opinionated auto configuration of the infrastructure beans.
这是一个最小的消费者应用程序。
Here is a minimal consumer application.
Spring Boot Consumer App
-
Java
-
Kotlin
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$java-examples/org/springframework/kafka/jdocs/started/consumer/Application.java[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$kotlin-examples/org/springframework/kafka/kdocs/started/consumer/Application.kt[]
spring.kafka.consumer.auto-offset-reset=earliest
NewTopic
bean 导致在代理中创建主题;如果主题已经存在,则不需要此 bean。
The NewTopic
bean causes the topic to be created on the broker; it is not needed if the topic already exists.
Spring Boot Producer App
-
Java
-
Kotlin
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$java-examples/org/springframework/kafka/jdocs/started/producer/Application.java[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$kotlin-examples/org/springframework/kafka/kdocs/started/producer/Application.kt[]
With Java Configuration (No Spring Boot)
Spring for Apache Kafka 被设计为在 Spring 应用程序上下文中使用。例如,如果您在 Spring 上下文之外创建监听器容器,除非您满足容器实现的所有 …Aware
接口,否则并非所有函数都能正常工作。
Spring for Apache Kafka is designed to be used in a Spring Application Context.
For example, if you create the listener container yourself outside of a Spring context, not all functions will work unless you satisfy all of the …Aware
interfaces that the container implements.
以下是一个不使用 Spring Boot 的应用程序示例;该应用程序同时具有 Consumer
和 Producer
。
Here is an example of an application that does not use Spring Boot; it has both a Consumer
and Producer
.
-
Java
-
Kotlin
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$java-examples/org/springframework/kafka/jdocs/started/noboot/Sender.java[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$java-examples/org/springframework/kafka/jdocs/started/noboot/Listener.java[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$java-examples/org/springframework/kafka/jdocs/started/noboot/Config.java[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$kotlin-examples/org/springframework/kafka/kdocs/started/noboot/Sender.kt[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$kotlin-examples/org/springframework/kafka/kdocs/started/noboot/Listener.kt[]
Unresolved include directive in modules/ROOT/pages/quick-tour.adoc - include::example$kotlin-examples/org/springframework/kafka/kdocs/started/noboot/Config.kt[]
如你所见,如果你不使用 Spring Boot,则必须定义若干基础架构 bean。
As you can see, you have to define several infrastructure beans when not using Spring Boot.