import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.Producer; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.clients.producer.RecordMetadata; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Properties; @Component @Slf4j public class KafkaUtil { @Autowired private KafkaConfigEntity kafkaConfig; public Producer initProducer(){ return new KafkaProducer(getProperties(kafkaConfig.getBrokerList())); } public Properties getProperties(String brokerList){ Properties props = new Properties(); props.put("bootstrap.servers", kafkaConfig.getBrokerList());//xxx服务器ip props.put("acks", "all");//所有follower都响应了才认为消息提交成功,即"committed" props.put("retries", 0);//retries = MAX 无限重试,直到你意识到出现了问题:) props.put("batch.size", 16384);//producer将试图批处理消息记录,以减少请求次数.默认的批量处理消息字节数 //batch.size当批量的数据大小达到设定值后,就会立即发送,不顾下面的linger.ms props.put("linger.ms", 1);//延迟1ms发送,这项设置将通过增加小的延迟来完成--即,不是立即发送一条记录,producer将会等待给定的延迟时间以允许其他消息记录发送,这些消息记录可以批量处理 props.put("buffer.memory", 33554432);//producer可以用来缓存数据的内存大小。 props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); return props; } public void send(String topic,String msg,String brokerList) throws Exception{ Producer<String,String> producer = new KafkaProducer(getProperties(brokerList)); ProducerRecord<String, String> prorec = new ProducerRecord<>(topic, msg); try { RecordMetadata recordMetadata = producer.send(prorec).get(); log.info(""+recordMetadata.topic()); }catch (Exception e){ log.info("",e); producer.close(); e.printStackTrace(); } producer.close(); } }
java 发送kafka 工具类
最新推荐文章于 2024-09-05 21:25:37 发布