一.生产者
1.加入maven配置
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
2.创建kafka生产类
public class KakfaNotice {
private KafkaProducer producer;
public KakfaNotice( String url) {
this.producer = create(url);
}
public void send(String topic, String key, JSONObject message) {
producer.send(new ProducerRecord<>(topic, key, message.toJSONString()));
}
private KafkaProducer create(String url) {
Properties props = new Properties();
props.put("bootstrap.servers", url);//kafka集群地址
props.put("acks", "all");//所有partition都收到消息,才收到响应
props.put("retries", 0);//重发消息次数为0
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);//缓存队列