您的位置:首页 > 游戏 > 手游 > kafka基础知识(持续更新中~)

kafka基础知识(持续更新中~)

2024/12/23 7:58:14 来源:https://blog.csdn.net/m0_74969835/article/details/141858250  浏览:    关键词:kafka基础知识(持续更新中~)

#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());

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com