kafka简述与集群配置

一、kafka简述

       官网:http://kafka.apache.org/

       文档:http://kafka.apache.org/082/documentation.html

       使用的版本是kafka_2.11-0.8.2.1

       Kafka® is a distributed, partitioned, replicated commit log service. It provides the functionality of a messaging system, but with a unique design.

       kafka是一个分布式、分区的、具有副本的一个提交日志的服务

2、集群介绍

                                      

(1)Kafka架构是由producer(消息生产者)、consumer(消息消费者)、borker(kafka集群的server,负责处理消息读、写请求,存储消息,在kafka cluster这一层这里,其实里面是有很多个broker)、topic(消息队列/分类相当于队列,里面有生产者和消费者模型)、zookeeper(元数据信息存在zookeeper中,包括:存储消费偏移量,topic话题信息,partition信息) 这些部分组成。

(2)kafka里面的消息是有topic来组织的,简单的我们可以想象为一个队列,一个队列就是一个topic,然后它把每个topic又分为很多个partition,这个是为了做并行的,在每个partition内部消息强有序,相当于有序的队列,其中每个消息都有个序号offset,比如0到12,从前面读往后面写。一个partition对应一个broker,一个broker可以管多个partition,比如说,topic有6个partition,有两个broker,那每个broker就管3个partition。这个partition可以很简单想象为一个文件,当数据发过来的时候它就往这个partition上面append,追加就行,消息不经过内存缓冲,直接写入文件,kafka和很多消息系统不一样,很多消息系统是消费完了我就把它删掉,而kafka是根据时间策略删除,而不是消费完就删除,在kafka里面没有一个消费完这么个概念,只有过期这样一个概念。

(3)producer自己决定往哪个partition里面去写,这里有一些的策略,譬如如果hash,不用多个partition之间去join数据了。consumer自己维护消费到哪个offset,每个consumer都有对应的group,group内是queue消费模型(各个consumer消费不同的partition,因此一个消息在group内只消费一次),group间是publish-subscribe消费模型,各个group各自独立消费,互不影响,因此一个消息在被每个group消费一次。
 

安装:kafka

       1.前提:jdk,scala,zookeeper

       2.kafka的安装(伪分布式)

              -1.下载组件

                     https://archive.apache.org/dist/kafka/0.8.2.1/kafka_2.11-0.8.2.1.tgz

              -2.上传tar包(服务器)

              -3.解压tar包

                     tar -zxvf kafka_2.11-0.8.2.1.tgz -C /opt/modules/

              -4.配置文件---server.properties

                     备注:每个server.properties文件的内容是不一样的

                     broker.id:表示每个kafka的broker服务的具体的id,要求非负数

                     port:表示每个kafka的broker服务监控的端口号,默认是9092

                     host.name:表示每个kafka的broker服务绑定在哪一台主机上。

                     listeners:表示指定该broker服务监听的主机名和端口号,默认使用的就是上面的配置

                     log.dirs:表示该broker的服务所管理的数据储存的目录

                     zookeeper.connect:表示给定kafka集群元数据管理的zk的地址以及储存元数据的目录

编辑配置server.properties

       这里重点修改三个参数broker.id标识本机、listeners监听地址、log.dirs是kafka接收消息存放路径、zookeeper.connect指定连接的zookeeper集群地址

           1、broker.id=0

           2、listeners=PLAINTEXT://XXXX:9092

           3、log.dirs=/opt/cdh/kafka_2.11-0.11.0.1/data/0

           4、zookeeper.connect=XXXX:2181/topic           

           注:XXXX:是你的hostname

其他参数保持默认即可,也可自己根据情况修改

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

# Switch to enable topic deletion or not, default value is false
#delete.topic.enable=true

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://XXXX:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma seperated list of directories under which to store log files
log.dirs=/opt/cdh/kafka_2.11-0.11.0.1/data/0

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Internal Topic Settings  #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended for to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=XXXX:2181/topic

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000


############################# Group Coordinator Settings #############################

# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0

同样的配置文件copy 4份 修改如下不分即可:

       broker.id分别为0、1、2、3   

       port分别为9092/9093/9094/9095 

       log.dirs=/opt/cdh/kafka_2.11-0.11.0.1/data/*    *分别为0,1,2,3

              -5.启动服务

                     1.首先必须先启动zk的服务

        bin/zkServer.sh start

                     2.启动kafka的broker的服务

        bin/kafka-server-start.sh
        
        bin/kafka-server-start.sh  -daemon config/server0.properties

        bin/kafka-server-start.sh  -daemon config/server1.properties

        bin/kafka-server-start.sh  -daemon config/server2.properties

        bin/kafka-server-start.sh  -daemon config/server3.properties

                     启动服务的时候,必须给定server.properties的信息,参数[-daemon],就是后台进程

              -6.关闭服务

        bin/kafka-server-stop.sh

3、kafka基本操作

--1.开启服务

       1.首先必须先启动zk的服务

       2.其次启动kafka的broker的服务

--2.topic的使用

          bin/kafka-topics.sh 返回一些参数列表  Create, delete, describe, or change a topic.

       --2.1创建topic(Create)

        bin/kafka-topics.sh --create --zookeeper XXXX:2181/topic  --topic topicName  --partitions  2 --replication-factor 2

        bin/kafka-topics.sh --create --zookeeper XXXX:2181/topic  --topic topicName  --partitions  5 --replication-factor 2

        bin/kafka-topics.sh --create --zookeeper XXXX:2181/topic  --topic topicName  --partitions  5 --replication-factor 5

              报错:kafka.admin.AdminOperationException: replication factor: 5 larger than available brokers: 4

              原因:副本的个数不可以超过broker集群的个数

              注意

                     1.一般应用中,一个topic的分区的数目为broker的个数的1-2倍

                     2.一般应用中,一个分区的副本数不超过3个,不能大于broker的分数

                     3.一旦一个topic被创建,一般是不会修改的

 

       --2.2列出topic的信息 --list

        bin/kafka-topics.sh  --list  --zookeeper  XXXX:2181/topic

      --2.3查看topic详细的信息 --describe

        bin/kafka-topics.sh --describe --zookeeper XXXX:2181/topic

        bin/kafka-topics.sh --describe --zookeeper XXXX:2181/topic  --topic  topicName

       --2.4修改topic  --alter(一般不建议修改,只允许修改topic的配置信息,以及增加分区数)             

        bin/kafka-topics.sh --alter --zookeeper  XXXX:2181/topic  --topic  topicName  --config "max.message.bytes=102400"

        bin/kafka-topics.sh --alter --zookeeper  XXXX:2181/topic  --topic  topicName  --delete-config "max.message.bytes"

        bin/kafka-topics.sh --alter --zookeeper  XXXX:2181/topic  --topic  topicName  --partitions 10

       --2.5删除topic --delete

        bin/kafka-topics.sh --delete --zookeeper  XXXX:2181/topic  --topic  topicName

              Topic topicName  is marked for deletion.

              Note: This will have no impact if delete.topic.enable is not set to true.

              注意:这个删除,只是表示禁用当前的topic,但是并没有彻底删除

              如果你想彻底删除:

                     1.在配置文件里面配置delete.topic.enable=true

                     2.先执行删除语句,然后在zkcli里面删除该topic即可

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值