美文网首页
kafka简单使用

kafka简单使用

作者: 侧耳倾听y | 来源:发表于2021-04-12 10:40 被阅读0次

    单机安装部署部署

    1. 下载kafka: kafka_2.13-2.7.0.tgz,解压。

    2.13是Scala的版本,2.7.0是kafka的版本

    1. 修改配置文件:
    vim config/server.properties
    

    打开 listeners=PLAINTEXT://localhost:9092

    1. 启动zookeeper:
    bin/zookeeper-server-start.sh config/zookeeper.properties
    

    4.启动kafka

    bin/kafka-server-start.sh config/server.properties
    
    1. 创建kafka的topic:
    bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic test_topic --partitions 4 -- replication-factor 1
    

    创建名为test_topic的主题,4分区,1副本

    1. 查看kafka的topic:
    bin/kafka-topics.sh --zookeeper localhost:2181 --list
    
    1. 查看test_topic主题的信息:
    bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic test_topic
    
    1. 生产消息:
    bin/kafka-console-producer.sh --bootstrap-server localhost:9003 --topic test_topic
    
    1. 从头查看(消费)test_topic主题下的消息:
    bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test_topic
    
    1. 测试kafka性能:prducer发送消息:
    bin/kafka-producer-perf-test.sh --topic test_topic --num-records 100000 --record-size 1000 --throughput 2000 --producer-props bootstrap.servers=localhost:9092
    

    向test_topic主题,发送十万条消息,每条消息1000字节大小,限速每秒发送2000条


    单机生产

    修改throughput参数为200000:


    单机消费
    可以看出来kafka的性能是很高的,单机的kafka1s能发送十万条左右的消息
    1. 测试kafka性能:consumer消费消息:
    bin/kafka-consumer-perf-test.sh --bootstrap-server localhost:9092 --topic test_topic -- fetch-size 1048576 --messages 100000 --threads 1
    

    消费test_topic主题的消息,消费10万条,每次向kafka请求消费1048576字节数据



    可以看出来消费用了不到一秒

    集群部署

    1. 首先添加配置文件:


    kafka9001.properties:

    broker.id=1
    # 处理网络请求的最大线程数
    num.network.threads=3
    # 处理磁盘I/O的线程数
    num.io.threads=8
    # socket的发送缓冲区(SO_SNDBUF)
    socket.send.buffer.bytes=102400
    # socket的接收缓冲区 (SO_RCVBUF)
    socket.receive.buffer.bytes=102400
    # socket请求的最大字节数。为了防止内存溢出,message.max.bytes必然要小于
    socket.request.max.bytes=104857600
    # 日志存储路径
    log.dirs=/tmp/kafka/kafka-logs1
    # 全局分区设置
    num.partitions=1
    # 每个数据目录用于在启动时进行日志恢复和在关闭时进行刷新的线程数,默认值为: 1
    num.recovery.threads.per.data.dir=1
    # offset提交主题的副本数
    offsets.topic.replication.factor=1
    # 事务主题的复制因子
    transaction.state.log.replication.factor=1
    # 覆盖事务主题的min.insync.replicas配置
    transaction.state.log.min.isr=1
    # 日志保存时间 (hours|minutes),默认为7天(168小时)。超过这个时间会根据policy处理数据。bytes和minutes无论哪个先达到都会触发。
    log.retention.hours=168
    # 控制日志segment文件的大小,超出该大小则追加到一个新的日志segment文件中(-1表示没有限制)
    log.segment.bytes=1073741824
    # 日志片段文件的检查周期,查看它们是否达到了删除策略的设置(log.retention.hours或log.retention.bytes)
    log.retention.check.interval.ms=300000
    # 连接zk的超时时间
    zookeeper.connection.timeout.ms=6000000
    delete.topic.enable=true
    group.initial.rebalance.delay.ms=0
    # 消息体的最大大小,单位是字节
    message.max.bytes=5000000
    # replicas每次获取数据的最大字节数
    replica.fetch.max.bytes=5000000
    listeners=PLAINTEXT://localhost:9001
    advertised.listeners=PLAINTEXT://localhost:9001
    broker.list=localhost:9001,localhost:9002,localhost:9003
    zookeeper.connect=localhost:2181
    
    1. 启动zookeeper:

    启动前需要注意,如果和单机kafka使用的是一个zookeeper,需要先清除kafka的数据,可使用ZooInspector,删除除了zookeeper外的文件夹

    bin/zookeeper-server-start.sh config/zookeeper.properties
    
    1. 分别启动kafka:
    ./bin/kafka-server-start.sh kafka9001.properties
    ./bin/kafka-server-start.sh kafka9002.properties
    ./bin/kafka-server-start.sh kafka9003.properties
    
    1. 创建主题:
    bin/kafka-topics.sh --zookeeper localhost:2181 --create --topic test_topic --partitions 3 - -replication-factor 2
    

    创建名为test_topic的主题,3分区,2副本

    1. 测试性能,生产:
    bin/kafka-producer-perf-test.sh --topic test32 --num-records 100000 --record-size 1000 --throughput 200000 --producer-props bootstrap.servers=localhost:9002
    
    集群生产

    可以看出来比起单机版性能甚至下降了,这是因为我在一台机器部署了这三个broker导致的。

    1. 测试性能,消费:
    bin/kafka-consumer-perf-test.sh --bootstrap-server localhost:9002 --topic test32 --fetch-size 1048576 --messages 100000 --threads 1
    
    集群消费

    消费性能也有所下降,用了1秒多

    相关文章

      网友评论

          本文标题:kafka简单使用

          本文链接:https://www.haomeiwen.com/subject/yeaakltx.html