Maven必须配置
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.4.1</version>
</dependency>
Java实现(一次单条消息)
package com.qf.kafka;
import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
public class MyProducer {
// 定义TOPIC名称
private final static String TOPIC_NAME = "my-replicated-topic";
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 1.设置参数
// 使用 Properties对象 存放键值对(参数配置信息)
Properties props = new Properties();
// 键值对中存入 bootstrap_server => kafka节点或集群(用逗号隔开)
props.put(ProducerConfig.BOOTSTRAP_SERVE