kafka的安装和简单概念

一.传统的消息通信方式
      a.Socket通信协议
         缺点:服务器端和客户端必须同时在线;传输大量数据时,数据完全不完备,对网络要求极高。
      b.文件服务器(ftp)
         缺点:实时性比较差;本地磁盘IO读写;
      c.共享数据库方式(Database)
         缺点:系统间访问共享数据库比较难实现:1.据库连接池有限的;2.因业务需求很难将数据库共享;
      d.JMS(Java Message Services)协议
         缺点:具体相关业务的需求
      
二:kafka消息队列 系统解耦、削峰填谷、定时任务、异步通知等等
三:kafka
  1.定义:
kafka是用于构建实时数据管道和流媒体应用,它是水平扩展的,容错的,快速的。
  2.相关概念:
    Broker:kafka集群包含一个或多个服务器,这种服务器被称作broker。
    Topic:每条发布到kafka集群的消息队列都有一个类别,这种类别被称为topic。物理上不同topic的消息分开存储,逻辑上一个topic的消息虽然存在一个或多个broker上,但用户只需指定消息的topic即可生产和消费数据而不必关心数据存在何处。
    Partition:partition是物理上的概念,每个topic包含一个或多个partition,创建topic时可以指定partition数量,每个partition对应一个文件夹,该文件夹存储该partition的数据和索引文件。
    Producer:负责发布信息到kafka broker
    Consumer:消费信息
  3.安装
    
a.安装kafka_2.10-0.10.0.1.tgz
       b.复制至{/home/hyxy/soft}
          $>cp /mnt/hgfs/2.安装环境/download/apache-kafka/kafka_2.10-0.10.0.1.tgz /home/hyxy/soft/
       c.解压
          $>tar -zxvf kafka_2.10-0.10.0.1.tgz 
	  $>rm kafka_2.10-0.10.0.1.tgz 
          $>ln -s kafka_2.10-0.10.0.1/ kafka
       d.修改环境变量,追加
          $>gedit ~/.bash_profile
		#Kafka install
		export KAFKA_HOME=/home/hyxy/soft/kafka
		export PATH=$KAFKA_HOME/bin:$PATH
          $>source ~/.bash_profile
四:单节点,单Broker集群
  1.开启zookeeper
      $>zookeeper-server-start.sh /home/hyxy/soft/kafka/config/zookeeper.properties 
      $>jps
	4264 Jps
	3178 QuorumPeerMain
    2.开启Broker
      参照{KAFKA_HOME/config/server.properties}
         broker.id=0                           //必须为整数;brokerID理解为分区号
         log.dirs=/home/hyxy/tmp/kafka-logs    //消息存放位置
         zookeeper.connect=localhost:2181      //注册zookeeper
      $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server.properties
      $>jps
	3424 Kafka
	4264 Jps
	3178 QuorumPeerMain
    3.创建主题:
      $>kafka-topics.sh --create --topic test --zookeeper localhost:2181 --partitions 1 --replication-factor 1
      reated topic "test".
      $>zkCli.sh
      [zk: localhost:2181(CONNECTED) 1] ls /brokers/topics
      [test]
      作用:1.在Zookeeper中注册topic节点;2.在【broker-->log.dirs属性】目录创建toptic主题
    4.开启生成者producer
      $>kafka-console-producer.sh --topic test --broker-list localhost:9092
      $>jps
	3424 Kafka
	4770 Jps
	4531 ConsoleProducer
	3178 QuorumPeerMain
       作用:开启守护进程ConsoleProducer
    5.开启消费者
      $>kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
      $>jps
	3424 Kafka
	5521 Jps
	4531 ConsoleProducer
	5270 ConsoleConsumer
	3178 QuorumPeerMain
      作用:开启守护进程ConsoleConsumer
    6.在producer(生产者一端)发送消息
       producer>hello world
       consumer>hello world
五:单节点,多Broker集群
  
  1.开启zookeeper
      $>zookeeper-server-start.sh /home/hyxy/soft/kafka/config/zookeeper.properties 
      $>jps
	4264 Jps
	3178 QuorumPeerMain
    2.开启多个Broker:案例模拟采用三个broker服务器
       a.复制server.properties两个,如下;
         $>cp server.properties server-1.properties
	 $>cp server.properties server-2.properties
       b.修改每个server配置,如下:
         【server-1.properties】
	      broker.id=1      
	      port=9093
	      log.dirs=/home/hyxy/tmp/kafka-logs-1
	 【server-2.properties】
	      broker.id=2      
	      port=9094
	      log.dirs=/home/hyxy/tmp/kafka-logs-2
	 【server-2.properties】
	      broker.id=0      
	      port=9092
	      log.dirs=/home/hyxy/tmp/kafka-logs
       c.分别启动kafka服务
          $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server.properties
	  $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server-1.properties
	  $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server-2.properties
       d.查看jps
          $>jps
		7090 Kafka
		7666 Kafka
		7394 Kafka
		6854 QuorumPeerMain
		7927 Jps
    3.创建主题:创建名为“hyxy”的主题,并设置其分区为2,复本为2
       $>kafka-topics.sh --create --topic hyxy --zookeeper localhost:2181 --partitions 2 --replication-factor 2
       Created topic "hyxy".
       验证:
         查看[log.dirs]目录下,主题生成的结果!!
    4.启动Producer
       $>kafka-console-producer.sh --topic hyxy --broker-list localhost:9092,localhost:9093,localhost:9094
    5.启动消费者
       $>kafka-console-consumer.sh --zookeeper localhost:2181 --topic hyxy --from-beginning
    6.属性说明:
       创建名为“topic1”的主题,并设置其分区为3,复本为3
           $>kafka-topics.sh --create --topic topic1 --zookeeper localhost:2181 --partitions 3 --replication-factor 3
       创建名为“topic2”的主题,并设置其分区为2,复本为1
           $>kafka-topics.sh --create --topic topic2 --zookeeper localhost:2181 --partitions 2 --replication-factor 1
       验证:
         查看[log.dirs]目录下,主题生成的结果!!
    7.复本数(--replication-factor)不能大于broker数
       验证:创建名为“topic3”的主题,并设置其分区为4,复本为4
          $>kafka-topics.sh --create --topic topic3 --zookeeper localhost:2181 
	                    --partitions 4 
			    --replication-factor 4
          Error while executing topic command : replication factor: 4 larger than available brokers: 3
	  [2018-09-12 14:08:36,196] ERROR kafka.admin.AdminOperationException: replication factor: 4 larger than available brokers: 3
		at kafka.admin.AdminUtils$.assignReplicasToBrokers(AdminUtils.scala:117)
		at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:403)
		at kafka.admin.TopicCommand$.createTopic(TopicCommand.scala:110)
		at kafka.admin.TopicCommand$.main(TopicCommand.scala:61)
		at kafka.admin.TopicCommand.main(TopicCommand.scala)
	   (kafka.admin.TopicCommand$)

六:多节点,多Broker集群
    1.要求:
        a.搭建两台节点:master和slave1:安装kafka和zookeeper
	b.分别在master和slave1上创建两个Broker:
             master节点操作:
	         复制server.properties为server-1.properties
                 【server.properties】
		      broker.id=0
		      port=9092
		      log.dirs=/home/hyxy/tmp/master-0
		      zookeeper.connect=master:2181,slave1:2181,slave2:2181
		 【server-1.properties】
		      broker.id=1
		      port=9093
		      log.dirs=/home/hyxy/tmp/master-1
		      zookeeper.connect=master:2181,slave1:2181,slave2:2181
             slave1节点操作:
	         复制server.properties为server-1.properties
                 【server.properties】
		      broker.id=2
		      port=9092
		      log.dirs=/home/hyxy/tmp/slave1-0
		      zookeeper.connect=master:2181,slave1:2181,slave2:2181
		 【server-1.properties】
		      broker.id=3
		      port=9093
		      log.dirs=/home/hyxy/tmp/slave1-1
		      zookeeper.connect=master:2181,slave1:2181,slave2:2181
    2.开启zookeeper:分别在master和slave1节点上执行以下命令:
       $>zkServer.sh start
    3.分别开启kafka
      master操作:
         $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server.properties
         $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server-1.properties
      slave1操作:
         $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server.properties
         $>kafka-server-start.sh /home/hyxy/soft/kafka/config/server-1.properties
    4.测试
       创建主题:
       开启Producer
       开启Consumer
原文地址:https://www.cnblogs.com/lyr999736/p/10695931.html