package com.muyu.common.kafka.config;

import com.muyu.common.core.constant.KafkaConstants;
import org.apache.kafka.clients.producer.KafkaProducer;
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 java.util.HashMap;

/**
 * kafka生产者配置信息
 * @Author：杨鹏
 * @Package：com.muyu.common.kafka.config
 * @Project：car-cloud-server
 * @name：KafkaProducerConfig
 * @Date：2024/9/28 17:27
 */
@Configuration
public class KafkaProducerConfig {

    /**
     * kafka地址
     */
    @Value("${spring.kafka.producer.bootstrap-servers}")
    private String bootstrapServers;

    /**
     * 重试次数
     */
    @Value("${spring.kafka.producer.retries}")
    private Integer retries;

    /**
     * 默认批量大小
     */
    @Value("${spring.kafka.producer.batch-size}")
    private Integer batchSize;

    /**
     * 总内存字节数
     */
    @Value("${spring.kafka.producer.buffer-memory}")
    private Integer bufferMemory;

    /**
     * 偏移量
     */
    @Value("${spring.kafka.producer.acks}")
    private String acks;

    @Bean
    public KafkaProducer kafkaProducer(){
        HashMap<String, Object> configs = new HashMap<>();
        configs.put(KafkaConstants.BOOTSTRAP_SERVERS,"111.231.50.146:9092");
        configs.put(KafkaConstants.RETRIES,retries);
        configs.put(KafkaConstants.BATCH_SIZE,batchSize);
        configs.put(KafkaConstants.BUFFER_MEMORY,bufferMemory);
        configs.put(KafkaConstants.ACKS,acks);
        StringSerializer keySerializer = new StringSerializer();
        StringSerializer valueSerializer = new StringSerializer();
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(configs, keySerializer, valueSerializer);
        return kafkaProducer;
    }

}
