1. 开启进程
[node123]systemctl stop firewalld
[node123]zkServer.sh start
[node123]kafka-server-start.sh /opt/app/kafka-0.11.0.0/config/server.properties &
[node1]kafka-console-consumer.sh --zookeeper node1:2181 --topic demo
2. java导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>kafkacode</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.12</artifactId>
<version>0.11.0.0</version>
</dependency>
</dependencies>
</project>
3. java代码
package new_out;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
// 新版本生产者代码--建议大家使用的版本
public class NewProducer {
public static void main(String[] args) {
Properties props = new Properties();
// Kafka服务端的主机名和端口号
props.put("bootstrap.servers", "192.168.200.111:9092, 192.168.200.112:9092, 192.168.200.113:9092");
// 等待所有副本节点的应答
props.put("acks", "all");
// 消息发送最大尝试次数
props.put("retries", 0);
// 一批消息处理大小
props.put("batch.size", 16384);
// 请求延时
props.put("linger.ms", 1);
// 发送缓存区内存大小
props.put("buffer.memory", 33554432);
// key序列化
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// value序列化
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<Object, Object> producer = new KafkaProducer<Object, Object>(props);
for (int i = 0; i < 100; i++) {
producer.send(new ProducerRecord<Object, Object>("demo", "hello--"+i));
}
// 必须有下面的语句才行,二选一
// producer.commitTransaction();
producer.close();
}
}
标签:producer,--,kafka,生产者,版本,props,put,org
From: https://www.cnblogs.com/jsqup/p/16665760.html