1. 程式人生 > >spring boot與kafka

spring boot與kafka

  1.專案搭建

  2.關鍵程式碼與配置

  3.效能調優

 

注意,本專案基於spring boot 1,如果是spring boot 2有可能會報錯.相應的包需要更新

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即可

2.關鍵程式碼與配置

  實現生產者消費者需要實現幾個關鍵bean

  類 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.消費者分割槽監聽,並開啟批量消費,提升效能