西维蜀黍

【Kafka】Config

broker 端配置 broker.id 每个 kafka broker 都有一个唯一的标识来表示,这个唯一的标识符即是 broker.id,它的默认值是 0。这个值在 kafka 集群中必须是唯一的,这个值可以   ...


【Golang】Use Kafka in Golang

  ...


【Kafka】安装

Install

macOS

$ brew install kafka

# install zookeeper
  ...


【RocketMQ】学习

Install

RocketMQ runs on all major operating systems and requires only a Java JDK version 8 or higher to be installed. To check, run java -version:

$ java -version
java version "1.8.0_121"

For Windows users, click here to download the 5.2.0 RocketMQ binary release, unpack it to your local disk, such as D:\rocketmq. For macOS and Linux users, execute following commands:

# Download release from the Apache mirror
$ wget https://dist.apache.org/repos/dist/release/rocketmq/5.2.0/rocketmq-all-5.2.0-bin-release.zip

# Unpack the release
$ unzip rocketmq-all-5.2.0-bin-release.zip

Prepare a terminal and change to the extracted bin directory:

$ cd rocketmq-all-5.2.0-bin-release/bin

1) Start NameServer

NameServer will be listening at 0.0.0.0:9876, make sure that the port is not used by others on the local machine, and then do as follows.

For macOS and Linux users:

### start Name Server
$ nohup sh mqnamesrv &

### check whether Name Server is successfully started
$ tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...

2) Start Broker

For macOS and Linux users:

### start Broker
$ nohup sh bin/mqbroker -n localhost:9876 &

### check whether Broker is successfully started, eg: Broker's IP is 192.168.1.2, Broker's name is broker-a
$ tail -f ~/logs/rocketmqlogs/broker.log
The broker[broker-a, 192.169.1.2:10911] boot success...
  ...


【Kafka】Basic

Components

Topic:Kafka将消息种子(Feed)分门别类, 每一类的消息称之为 Topic。

Partition: 一个 topic 可以被分为若干个分区(partition),同一个 topic 中的不同分区可以不在一个 broker上。即通过部署在多个 broker 上,来实现 kafka 的伸缩性,单一 topic 中的一个partition可以被保证有序,但是无法保证 topic 中所有的分区有序。

  • A topic could have multiple partitions.
  • Each partition is a single log file where records are written to it in an append-only fashion.
  • 一个非常大的 Topic 可以分布到多个 Broker 上,一个 Topic 可以分为多个 Partition 进行存储,每个 Partition 是一个有序的队列。
  • 一个broker可能同时是一个partition的leader,另一个partition的follower。 这样可以平衡负载,避免所有的请求都只让一台或者某几台服务器处理。
  • 每个partition有一个leader,零或多个follower。Leader处理此partition的所有的读写请求而follower被动的复制数据。如果leader当机,其它的一个follower会被推举为新的leader。

Producer: 发布消息的对象称之为 Topic Producer

Consumer: 订阅消息并处理发布的消息的种子的对象称之为 Topic Consumers

Consumer Group:生产者与消费者的关系就如同餐厅中的厨师和顾客之间的关系一样,一个厨师对应多个顾客,也就是一个生产者对应多个消费者,消费者群组(Consumer Group)指的就是由一个或多个消费者组成的群体。

  • 一个topic下的一条消息只会由一个消费者组中的一个消费者消费,而不会被这个消费者组中的其他消费者也同时消费
  • 处于不同消费者组的消费者可以同时消费同一个topic中的同一消息

Broker:已发布的消息保存在一组 Server中,这一组服务器被称为一个Kafka集群。集群中的每一个 Kafka Server都是一个 Broker。消费者可以订阅一个或多个 topic ,并从Broker拉数据,从而消费这些已发布的消息。

Rebalance:消费者组内某个消费者实例挂掉后,其他消费者实例自动重新分配订阅 topic 分区的过程。Rebalance 是 Kafka 消费者端实现高可用的重要手段。

Replica:Kafka 中消息的备份又叫做 副本(Replica),为实现数据备份的功能,保证集群中的某个节点发生故障时,该节点上的 Partition 数据不丢失,且 Kafka 仍然能够继续工作,为此Kafka提供了副本机制,一个 Topic 的每个 Partition 都有若干个副本,一个 Leader 副本和若干个 Follower 副本。

Leader: 每个partition有多个副本,其中有且仅有一个作为Leader,Leader是当前负责数据的读写的partition。

Follower: Follower跟随Leader,所有写请求都通过Leader路由,数据变更会广播给所有Follower,Follower与Leader保持数据同步。如果Leader失效,则从Follower中选举出一个新的Leader。当Follower与Leader挂掉、卡住或者同步太慢,leader会把这个follower从“in sync replicas”(ISR)列表中删除,重新创建一个Follower。

Consumer Offset: an integer value that continually increases—is another piece of metadata that Kafka adds to each message as it is produced.

  • Each message in a given partition has a unique offset, and the following message has a greater offset (though not necessarily monotonically greater).
  • By storing the next possible offset for each partition, typically in Kafka itself, a consumer can stop and restart without losing its place.

Offset:消费者消费的位置信息,监控数据消费到什么位置,当消费者挂掉再重新恢复的时候,可以从消费位置继续消费。

Kafka Controller Broker:其实就是一个 Kafka 集群中一台 Broker,它除了具有普通Broker 的消息发送、消费、同步功能之外,还需承担一些额外的工作。Kafka 使用公平竞选的方式来确定 Controller ,最先在 ZooKeeper 成功创建临时节点 /controller 的Broker会成为 Controller ,一般而言,Kafka集群中第一台启动的 Broker 会成为Controller,并将自身 Broker 编号等信息写入ZooKeeper临时节点/controller。

  ...