#broker.id属性在kafka集群中必须要是唯⼀
broker.id=0
#kafka部署的机器ip和提供服务的端⼝号
listeners=PLAINTEXT://192.168.65.60:9092
#kafka的消息存储⽂件
log.dir=/usr/local/data/kafka-logs
#kafka连接zookeeper的地址
zookeeper.connect=192.168.65.60:2181
./kafka-server-start.sh -daemon ../config/server.properties
./kafka-topics.sh --create --zookeeper 172.16.253.35:2181 --replication-factor 1 --partitions 1 --topic test
./kafka-topics.sh --list --zookeeper 172.16.253.35:2181 test
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --topic test
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --topic test
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --from-beginning --topic test
/usr/local/kafka/data/kafka-logs/主题-分区/00000000.log
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --consumer-property group.id=testGroup --topic test
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --
consumer-property group.id=testGroup1 --topic test
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092 --consumer-property group.id=testGroup2 --topic test
/kafka-consumer-groups.sh --bootstrap-server 172.16.253.38:9092 --describe --group testGroup
./kafka-topics.sh --create --zookeeper 172.16.253.35:2181 --replication-factor 1 --partitions 2 --topic test1
1# 0 1 2
2broker.id=2
3// 9092 9093 9094
4listeners=PLAINTEXT://192.168.65.60:9094
5//kafka-logs kafka-logs-1 kafka-logs-2
6log.dir=/usr/local/data/kafka-logs-2
1./kafka-server-start.sh -daemon ../config/server.properties
2./kafka-server-start.sh -daemon ../config/server1.properties
3./kafka-server-start.sh -daemon ../config/server2.properties
./kafka-topics.sh --create --zookeeper 172.16.253.35:2181 --replication-factor 3 --partitions 2 --topic my-replicated-topic
# 查看topic情况 ./kafka-topics.sh --describe --zookeeper 172.16.253.35:2181 --topic myreplicated-topic
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092,172.16.253.38:9093,172.16.253.38:9094 --frombeginning --consumer-property group.id=testGroup1 --topic my-replicatedtopic
./kafka-console-producer.sh --broker-list 172.16.253.38:9092,172.16.253.38:9093,172.16.253.38:9094 --topic myreplicated-topic
./kafka-console-consumer.sh --bootstrap-server 172.16.253.38:9092,172.16.253.38:9093,172.16.253.38:9094 --frombeginning --consumer-property group.id=testGroup1 --topic my-replicatedtopic
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>2.4.1</version></dependency>
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 MySimpleProducer {private final static String TOPIC_NAME = "my-replicated-topic";public static void main(String[] args) throws ExecutionException,
InterruptedException {//1.设置参数Properties props = new Properties();props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
"172.16.253.38:9092,172.16.253.38:9093,172.16.253.38:9094");//把发送的key从字符串序列化为字节数组props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());//把发送消息value从字符串序列化为字节数组props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());//2.创建⽣产消息的客户端,传⼊参数Producer<String,String> producer = new KafkaProducer<String,
String>(props);//3.创建消息//key:作⽤是决定了往哪个分区上发,value:具体要发送的消息内容ProducerRecord<String,String> producerRecord = new ProducerRecord<>
(TOPIC_NAME,"mykeyvalue","hellokafka");//4.发送消息,得到消息发送的元数据并输出RecordMetadata metadata = producer.send(producerRecord).get();System.out.println("同步⽅式发送消息结果:" + "topic-" +
metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" + metadata.offset());}
}
RecordMetadata metadata = producer.send(producerRecord).get();System.out.println("同步⽅式发送消息结果:" + "topic-" +
metadata.topic() + "|partition-"+ metadata.partition() + "|offset-" + metadata.offset());