1.項目搭建java
2.關鍵代碼與配置spring
3.性能調優apache
注意,本項目基於spring boot 1,若是是spring boot 2有可能會報錯.相應的包須要更新bootstrap
1.項目搭建緩存
kafka版本:kafka_2.11-1.0.0異步
jar包版本:1.1.7.REALEASE性能
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>1.1.7.RELEASE</version> </dependency>
只須要在spring boot工程中加入改jar便可spa
2.關鍵代碼與配置線程
實現生產者消費者須要實現幾個關鍵beancode
類 KafkaProducerConfig:
import org.apache.kafka.clients.producer.ProducerConfig; 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; @Configuration @EnableKafka public class KafkaProducerConfig { @Bean("kafkaTemplate") public KafkaTemplate<String, String> kafkaTemplate() { KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<String, String>(producerFactory()); return kafkaTemplate; } @Value("${spring.kafka.bootstrap-servers}") private String kafkaServers; @Value("${spring.kafka.producer.retries}") private String retry; @Value("${spring.kafka.producer.batch-size}") private String batch; @Value("${spring.kafka.producer.buffer-memory}") private String mem; @Value("${spring.kafka.producer.key-serializer}") private String keySerializer; @Value("${spring.kafka.producer.value-serializer}") private String valueSerializer; public ProducerFactory<String, String> producerFactory() { Map<String, Object> properties = new HashMap<String, Object>(); properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,kafkaServers); properties.put(ProducerConfig.RETRIES_CONFIG, retry); properties.put(ProducerConfig.BATCH_SIZE_CONFIG, batch); properties.put(ProducerConfig.LINGER_MS_CONFIG, 1); properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, mem); properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer); properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer); return new DefaultKafkaProducerFactory<String, String>(properties); } }
幾個關鍵配置:
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG //kafka地址 ProducerConfig.BATCH_SIZE_CONFIG //批量發送配置,單位字節 當多個數據同時發往一個分區時,將被批量控制,減小對服務端的請求 ProducerConfig.BUFFER_MEMORY_CONFIG //生產者緩存,單位字節 生產者對發送數據的緩存總數
如今就構造出了kafkaTemplate對象,能夠用他發送消息
kafkaTemplate.send(topic, 0, gson.toJson(Object));
send能夠只傳三個參數:topic,分區,數據
消費者代碼和配置:
類 KafkaConsumerBatchConfig
package com.newland.dc.kafka.kafka; import org.apache.kafka.clients.consumer.ConsumerConfig; 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; @Configuration @EnableKafka public class KafkaConsumerBatchConfig { @Value("${spring.kafka.bootstrap-servers}") private String servers; @Value("${spring.kafka.consumer.enable-auto-commit}") private boolean auto; @Value("${spring.kafka.consumer.auto-commit-interval}") private int interval; @Value("${spring.kafka.consumer.group-id}") private String group; @Value("${spring.kafka.consumer.auto-offset-reset}") private String reset; @Value("${spring.kafka.consumer.key-deserializer}") private String keyDeserializer; @Value("${spring.kafka.consumer.value-deserializer}") private String valueDeserializer; @Value("${spring.kafka.consumer.max-poll-records:100}") private String maxPollRecords; @Value("${spring.kafka.consumer.max-poll-interval:1000000}") private String maxPollInterval; public ConsumerFactory<String, String> consumerFactory() { Map<String, Object> properties = new HashMap<String, Object>(); properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);//注意這裏修改成kafka的具體配置項目,我這裏只是爲了開發演示方便 properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, auto); properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, interval); properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000"); properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializer); properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer); properties.put(ConsumerConfig.GROUP_ID_CONFIG, group); properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords); properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, reset); properties.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, maxPollInterval); return new DefaultKafkaConsumerFactory<String, String>(properties); } @Bean public KafkaListenerContainerFactory<?> batchFactory() { ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); factory.setConsumerFactory(consumerFactory()); factory.setConcurrency(1); factory.setBatchListener(true);//設置爲批量消費,每一個批次數量在Kafka配置參數中設置ConsumerConfig.MAX_POLL_RECORDS_CONFIG factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.MANUAL_IMMEDIATE);//設置提交偏移量的方式 return factory; } }
關鍵配置:
ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG //因爲此處批量咱們用手動提交,因此該配置改成false ConsumerConfig.MAX_POLL_RECORDS_CONFIG //每次批量消費最大數 factory.setBatchListener(true); //注意把批量消費開啓
消費者代碼:對話題的每一個分區監聽,注意containerFactory配置
@Component public class MyListener { @Autowired private KafkaReceiverBatch kafkaReceiverBatch; private final Log log = LogFactory.getLogger(MyListener.class); @KafkaListener(id = "id0",containerFactory = "batchFactory", topicPartitions = { @TopicPartition(topic = "${consumer.log.topic:log.business}", partitions = { "0" }) }) public void listenPartition0(List<ConsumerRecord<?, ?>> records, Acknowledgment ack) { log.info(LogProperty.LOGCONFIG_DEALID,"partition:0, size " + records.size()); kafkaReceiverBatch.batchConsumer(records,ack); a1 = printNum("0",a += records.size(),a1); } @KafkaListener(id = "id1",containerFactory = "batchFactory", topicPartitions = { @TopicPartition(topic = "${consumer.log.topic:log.business}", partitions = { "1" }) }) public void listenPartition1(List<ConsumerRecord<?, ?>> records, Acknowledgment ack) { log.info(LogProperty.LOGCONFIG_DEALID,"partition:1, size " + records.size()); kafkaReceiverBatch.batchConsumer(records,ack); b1 = printNum("1",b += records.size(),b1); } @KafkaListener(id = "id2",containerFactory = "batchFactory", topicPartitions = { @TopicPartition(topic = "${consumer.log.topic:log.business}", partitions = { "2" }) }) public void listenPartition2(List<ConsumerRecord<?, ?>> records, Acknowledgment ack) { log.info(LogProperty.LOGCONFIG_DEALID,"partition:2, size " + records.size()); kafkaReceiverBatch.batchConsumer(records,ack); c1 = printNum("2",c += records.size(),c1); } static Integer a = 0,b = 0,c = 0; static Integer a1 = 0,b1 = 0 ,c1 = 0 ; private Integer printNum(String threadTag, Integer num, Integer printTimes){ if( num/100000 > printTimes ){ System.out.println("partition:" + threadTag + ",consumer num:" + num); printTimes ++; } return printTimes; } }
消費邏輯也貼個例子:
protected void batchConsumer(List<ConsumerRecord<?, ?>> records, Acknowledgment ack){ for (ConsumerRecord<?, ?> record : records) { try { Optional<?> kafkaMessage = Optional.ofNullable(record.value()); if (kafkaMessage.isPresent()) { Object message = kafkaMessage.get(); AllLogBase allLogBase = gson.fromJson(message.toString(), AllLogBase.class); } } catch (Exception e) { e.printStackTrace(); continue; } } ack.acknowledge();//手動提交偏移量 }
3.性能調優
kafka生產和消費要注意幾個關鍵點:
1.kafka生產者異步:
pool.execute(()->{kafkaTemplate.send(topic, 0, gson.toJson(Object));});
好比此處能夠改成線程池
2.批量寫入,能夠更改生產者的批量發送值和緩存值,加大該值將大幅提高性能
3.消費者分區監聽,並開啓批量消費,提高性能