Kafka:手动管理offset

1、Springboot版本:2.0.3.RELEASE


    org.springframework.kafka
    spring-kafka

 2、application.properties

spring.kafka.producer.bootstrap-servers=192.168.1.102:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
 
spring.kafka.consumer.bootstrap-servers=192.168.1.102:9092
spring.kafka.consumer.group-id=hz_log_group
spring.kafka.consumer.enable-auto-commit=false
spring.kafka.consumer.auto-offset-reset=latest
spring.kafka.consumer.max-poll-records=1
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.listener.concurrency=5

3、生产者

package com.cfl.kafka;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class KafkaProducer {
    
    @Autowired
    private KafkaTemplate kafkaTemplate;

    /**
     * 发送消息
     * @param topic
     * @param message
     */
    public void send(String topic, String message) {
        kafkaTemplate.send(topic, message);
    }
}

4、消费者

package com.cfl.kafka;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.listener.AbstractMessageListenerContainer.AckMode;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

@Component
public class KafkaConsumer{

    @Autowired
    private KafkaProducer kafkaProducer;
	
    @KafkaListener(containerFactory = "kafkaListenerContainerFactory", topics = "hz_log")
    public void consumerListener(String message, Acknowledgment ack) {
	System.out.println("consumer: " + message);
	kafkaProducer.send("hz_log_result", message);
	// 手动提交offset
	ack.acknowledge();
    }
    
    @Bean
    public KafkaListenerContainerFactory> kafkaListenerContainerFactory(ConsumerFactory consumerFactory) {
	ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory();
        factory.setConsumerFactory(consumerFactory);
        factory.getContainerProperties().setPollTimeout(1500);
        factory.getContainerProperties().setAckMode(AckMode.MANUAL);
        return factory;
    }
}

 

你可能感兴趣的:(Kafka)