Kafka初涉

来源:互联网 发布:国外网上购物知乎 编辑:程序博客网 时间:2024/05/17 19:18

Kafka简介


kafka是消息队列中的一种,对于消息队列,通俗一点说就是消息的队列,消息传输过程中的容器,这不是废话么。。。。
主要分为两种:
P2P:也就是点对点,个人对个人,类似于两个人说悄悄话,这悄悄话就作为消息(主要模块:发送者,接受者,消息)
发布订阅系统:一个人发布相关主题的消息,一堆人去订阅。类似于老师讲课,数学老师在讲关于数学这个主题的消息,订阅数学这个主题的学生就会收到消息(主要模块:主题,发布者,订阅者,消息队列)

消息队列一般遵循消费者生产者模式,而第一种只有一个生产者和一个消费者,对于第二种一个生产者多个消费者
kafka就属于后者

kafka是一种分布式发布-订阅消息系统,具有高吞吐量,持久化,分布式的优点

主要包含组件:
Topic:消息的分类主题,哪一类消息(逻辑概念)
Producer:生产者,消息的生产者
Consumer:消息的消费者,订阅某一类topic的消费者
Broker:消息的代理,也就是存储消息的地方,kafka集群的一个节点,就代表一个broker

partition:基于Topic上,物理的分区,一类消息可以有多个分区,一个topic可以有多个partition,一个partition只能属于一个topic
message:消息,一个partition里面可以有多个消息

Kafka基于zookeepr运行





Kaka安装


版本:0.9  承上启下


解压 tar -zxvf scala-2.10.4.tgz

kafka有内置的zookeeper,所以可以直接启动内置zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties  >> /opt/logs/kafka-zk.log>&1 & 然后启动kafkabin/kafka-server-start.sh config/server.properties  >> /opt/logs/kafka-server.log 2>&1 &3116 Kafka3058 QuorumPeerMain3138 Jps




bin/kafka-topics.sh -h
可以进行topic操作
Command must include exactly one action: --list, --describe, --create, --alter or --delete


单机模式只有一个broker,所以就等于只有一个节点,副本数超过节点数直接报错
bin/kafka-topics.sh --create --topic hello --zookeeper localhost:2181 --partitions 2 --replication-factor 2
Error while executing topic command : replication factor: 2 larger than available brokers: 1
[2016-11-26 22:53:55,970] ERROR kafka.admin.AdminOperationException: replication factor: 2 larger than available brokers: 1
    at kafka.admin.AdminUtils$.assignReplicasToBrokers(AdminUtils.scala:77)
    at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:236)
    at kafka.admin.TopicCommand$.createTopic(TopicCommand.scala:105)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:60)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)


[root@itcast01 kafka_2.10-0.9.0.0]# bin/kafka-topics.sh --create --topic hello --zookeeper localhost:2181 --partitions 2 --replication-factor 1Created topic "hello".[root@itcast01 kafka_2.10-0.9.0.0]# bin/kafka-topics.sh --create --topic helloworld --zookeeper localhost:2181 --partitions 3 --replication-factor 1Created topic "helloworld".[root@itcast01 kafka_2.10-0.9.0.0]# bin/kafka-topics.sh --list --zookeeper localhost:2181hellohelloworld[root@itcast01 kafka_2.10-0.9.0.0]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic helloTopic:hello    PartitionCount:2    ReplicationFactor:1    Configs:    Topic: hello    Partition: 0    Leader: 0    Replicas: 0    Isr: 0    Topic: hello    Partition: 1    Leader: 0    Replicas: 0    Isr: 0leader: partition的leader,主要用到的brokerid 0(0是在配置文件config/server.properties 里面定义的),无论 生产和消费都往这个里面去消费和生产(主要消费对象)replicas: partition的总的副本的brokerid的集合Isr partition 可用的副本brokerid的集合 0修改分区数 1035  bin/kafka-topics.sh --alter --partitions 1 --zookeeper localhost:2181 --topic hello删除topic1038  bin/kafka-topics.sh --delete  --zookeeper localhost:2181 --topic helloworld只是标记删除,需要修改配置文件config/server.properties增加:delete.topic.enable=true


 

demo演示:


启动kafka生产者端:
bin/kafka-console-producer.sh --topic hello --broker-list localhost:9092
因为是生产者,所以要将消息存放到broker上

启动消费者:
bin/kafka-console-consumer.sh --topic hello --zookeeper localhost:2181
消费者就无需加broker参数

启动后,如果要获取到生产者第一条生产的消息,则加上参数--from-beginning



0 0
原创粉丝点击