首先引入依赖:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.5.14.RELEASE</version>
</dependency>
添加kafka配置文件
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
retries: 0
batch-size: 16384
buffer-memory: 33554432
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
properties:
linger.ms: 1
consumer:
enable-auto-commit: false
auto-commit-interval: 100ms
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer:
org.apache.kafka.common.serialization.StringDeserializer
properties:
session.timeout.ms: 15000
group-id: test-group-id
初始化topic,并制定分区数量
@Configuration
public class KafkaCreateTopic {
@Bean
public NewTopic initialTopic() {
return new NewTopic("topic.partition",3, (short) 1 );
}
}
2、编写生产者,发送到指定分区
@GetMapping("/kafka/many")
public