附录 A: 重写 Spring Boot 依赖项
当在 Spring Boot 应用程序中使用 Spring for Apache Kafka 时,Apache Kafka 依赖项的版本由 Spring Boot 的依赖管理确定。
如果你希望使用不同版本的 kafka-clients 或 kafka-streams,并且使用嵌入式的 kafka broker 进行测试,就需要覆盖 Spring Boot 依赖管理所使用的版本;设置 kafka.version 属性。
或者,要使用与受支持的 Spring Boot 版本兼容的不同 Spring for Apache Kafka 版本,请设置 spring-kafka.version 属性。
或使用不同的 Apache Kafka 版本与受支持的 Spring Boot 版本一起使用,可以设置 spring-kafka.version 属性。
例如,2.9.13 由默认引入 2.8.x 的 Spring Boot 2.7.x 支持。
Maven
<properties>
<kafka.version>3.6.0</kafka.version>
<spring-kafka.version>2.9.13</spring-kafka.version>
</properties>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- optional - only needed when using kafka-streams -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Required for the embedded broker when using 3.6.0 or later with Boot 2.7.x -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-server-common</artifactId>
<classifier>test</classifier>
<scope>test</scope>
<version>${kafka.version}</version>
</dependency>
Gradle
ext['kafka.version'] = '3.6.0'
ext['spring-kafka.version'] = '2.9.13'
dependencies {
implementation 'org.springframework.kafka:spring-kafka'
implementation 'org.apache.kafka:kafka-streams' // optional - only needed when using kafka-streams
testImplementation 'org.springframework.kafka:spring-kafka-test'
// the following is required for the embedded broker when using 3.6.0 or later with Boot 2.7.x
testImplementation "org.apache.kafka:kafka-server-common:$kafka.version:test"
}
测试范围的依赖项只有在您在测试中使用嵌入式Kafka broker时才需要。