kafka+springboot整合代码(推荐)

pom文件依赖:
首先添加依赖包,注意kafka的版本要和安装的kafka版本一致。


	org.springframework.kafka
	spring-kafka

一.application.properties配置:

spring.kafka.bootstrap-servers=127.0.0.1:9092
#spring.kafka.consumer.group-id=group1
spring.kafka.consumer.enable-auto-commit=false
spring.kafka.consumer.auto-commit-interval=1000
spring.kafka.consumer.max-poll-records=10

spring.kafka.producer.retries=1
spring.kafka.producer.batch-size=4096
spring.kafka.producer.buffer-memory=40960

1.spring.kafka.consumer.group-id没有指定groupId是因为我在下面的config写死了。
2.消费者最好设置手动提交游标,否则出现消费失败情况就麻烦了

二.生产者配置:

话不多说,上代码开始干

package cn.shipeng.friend.config;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * kafka生产者配置
 */
@EnableKafka
@Configuration
public class KafkaProducerConfig {
	
	@Value("${spring.kafka.bootstrap-servers}")
	private String servers;
	@Value("${spring.kafka.producer.retries}")
	private int retries;
	@Value("${spring.kafka.producer.batch-size}")
	private int batchSize;
	@Value("${spring.kafka.producer.buffer-memory}")
	private int bufferMemory;
	
	public Map producerConfigs() {
		Map props = new HashMap<>();
		props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
		props.put(ProducerConfig.RETRIES_CONFIG, retries);
		props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
		props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
		props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
		props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
		return props;
	}
	
	@Bean
	public KafkaTemplate kafkaTemplate() {
		return new KafkaTemplate(producerFactory());
	}
	
	public ProducerFactory producerFactory() {
		return new DefaultKafkaProducerFactory<>(producerConfigs());
	}
}

生产者发消息:

package cn.shipeng.friend.controller.api;

import cn.shipeng.base.service.controller.BaseController;
import cn.shipeng.common.core.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * 测试生产者
 * @author hecj
 */
@RestController
public class TestApiController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(TestApiController.class);
	
	@Autowired
	KafkaTemplate kafkaTemplate;
	
	/**
	 * 发消息
	 * @author hecj
	 */
	@RequestMapping("/api/test/push")
	public Result testPush() {
		
		ListenableFuture listenableFuture = kafkaTemplate.send("GroupQueue", UUID.randomUUID().toString(),"发送一条新消息");
		//发送成功后回调
		SuccessCallback successCallback = new SuccessCallback() {
			@Override
			public void onSuccess(Object result) {
				System.out.println("发送成功");
			}
		};
		//发送失败回调
		FailureCallback failureCallback = new FailureCallback() {
			@Override
			public void onFailure(Throwable ex) {
				System.out.println("发送失败");
			}
		};
		
		listenableFuture.addCallback(successCallback,failureCallback);
		return new Result(Result.CODE.SUCCESS);
	}
}

三.消费者配置:

package cn.shipeng.friend.config;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.AbstractMessageListenerContainer;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;

import java.util.HashMap;
import java.util.Map;

/**
 * kafka消费者配置
 * @author hecj
 */
@EnableKafka
@Configuration
class KafkaConsumerConfig {
	
	@Value("${spring.kafka.bootstrap-servers}")
	private String servers;
//	@Value("${spring.kafka.consumer.group-id}")
//	private String groupId;
	@Value("${spring.kafka.consumer.enable-auto-commit}")
	private boolean enableAutoCommit;
	@Value("${spring.kafka.consumer.auto-commit-interval}")
	private String autoCommitInterval;
	@Value("${spring.kafka.consumer.max-poll-records}")
	private int maxPollRecordsConfig;
	
	public Map consumerConfigs() {
		Map propsMap = new HashMap<>();
		propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
		propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
		propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
		propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
		propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
		// 组名
		propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
		propsMap.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecordsConfig);
		return propsMap;
	}
	
	@Bean
	public KafkaListenerContainerFactory> kafkaListenerContainerFactory() {
		ConcurrentKafkaListenerContainerFactory factory = new ConcurrentKafkaListenerContainerFactory<>();
		factory.setConsumerFactory(consumerFactory());
//		factory.setConcurrency(10);
		factory.getContainerProperties().setPollTimeout(1500);
		factory.setBatchListener(true);
		factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE);
		return factory;
	}
	
	public ConsumerFactory consumerFactory() {
		return new DefaultKafkaConsumerFactory<>(consumerConfigs());
	}
}

消费者接收消息:

package cn.shipeng.friend.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * kafka消费者测试
 */
@Component
public class GroupConsumer {

    @KafkaListener(topics = {"GroupQueue"},containerFactory = "kafkaListenerContainerFactory")
    public void listen(List records , Acknowledgment ack) {
        try {
            for (ConsumerRecord record : records) {
                System.out.println(String.format("offset = %d, key = %s, value = %s%n \n", record.offset(), record.key(), record.value()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //手动提交偏移量
            ack.acknowledge();
        }
    }
}

你可能感兴趣的:(kafka,springboot)