spring-kafka 多线程消费

  1. 引入依赖
dependencies {
   
    testCompile group: 'junit', name: 'junit', version: '4.12'

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'

// https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka
    compile group: 'org.springframework.kafka', name: 'spring-kafka'
    
    compile 'org.projectlombok:lombok'

}
  1. 生产者和消费者常用配置
    我这里使用了自定义的序列化类

spring.kafka.bootstrap-servers=localhost:9092

### kafka 生产者常用配置
spring.kafka.producer.acks=1
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432
spring.kafka.producer.retries=3
spring.kafka.producer.value-serializer=com.ws.JacksonSerializer
spring.kafka.producer.properties.linger.ms=1


### kafka消费者常用配置
spring.kafka.consumer.enable-auto-commit= true
spring.kafka.consumer.auto-commit-interval=1000
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.max-poll-records=10
spring.kafka.consumer.value-deserializer=com.ws.JacksonDeserializer

示例代码

  1. 生产者


@SpringBootApplication
@RestController
@EnableKafka
public class Application {
   


    @Autowired
    private KafkaTemplate kafkaTemplate;

    public static void main(String[] args) {
   
        SpringApplication.run(Application.class,args);
    }

    @GetMapping("/test")
    public String test(){
   
        return 

你可能感兴趣的:(spring,专栏)