依赖
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> </dependency>
代码
package com.perfect; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import org.junit.jupiter.api.Test; import java.util.Properties; public class kafkaProducerTest { @Test public void sendmessagetest(){ Properties props = new Properties(); props.put("bootstrap.servers","localhost:9092"); props.put("acks","all"); props.put("retries",3); props.put("batch.size",16384); props.put("linger.ms",1); props.put("buffer.memory",33554432); props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer"); props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer"); KafkaProducer<String,String> p = new KafkaProducer<String, String>(props); for(int i = 0; i < 10; i++){ p.send(new ProducerRecord<>("test1","message"+i)); } p.close(); } }
原文:https://www.cnblogs.com/abuduri/p/13341902.html