> For the complete documentation index, see [llms.txt](https://www.0xffff.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.0xffff.com/kafka.md).

# 🌈Kafka 集群环境部署与使用

**Kafka相关链接资料**

* <http://kafka.apache.org/documentation/#producerconfigs>

**Kafka消息队列组件**

**Broker**

* 一台kafka服务器就是一个broker。一个集群由多个broker组成。一个broker可以容纳多个topic&#x20;

**Topic**

* 每条发布到Kafka集群的消息都有一个类别，这个类别被称为Topic （物理上不同的Topic消息分开存储，逻辑上一个Topic消息虽然保存于一个或多个broker上，但用户只需要指定消息的Topic 即可生成或消费数据而不必关心数据存于何处） &#x20;

**Partition**

* Partition 是物理上的概念，每个Topic包含一个或多个Partition 取决于kafka服务配置(partitions)
* 一个非常大的topic 可以分布在多个broker（及服务器）上，一个topic可以分为多个partition，每个partition是一个有序的队列
* partition中的每条消息都会分配一个有序ID（offset）。**kafka只保证按一个partition中的顺序将消息发给consumer，不保证一个topic的整体（多个partition间）的顺序**

**Producer**

* 负责发布消息到 Kafka broker

  **Consumer**
* 消息消费者，向kafka broker读取消息的客户端 &#x20;

**Consumer Group**

* 每个Consumer属于一个特定的Consumer Group（可为每个Consumer指定group name，若不指定group name 则属于默认的group）
* 多个Consumer 如果指定多个groupID，此时消息方式将是广播的方式。一个消息将被多个不通groupID的Consumer 消费，同一个groupID 的 多个Consumer只要有一个消费成功，其同一个组内 Consumer 将无法取到数据。

**集群Kafka**

**服务配置信息**

```
#192.168.0.143/broker&zookeeper brokerID 0 / partition 5 / replication-factor: 3
#192.168.0.144/broker&zookeeper brokerID 1 / partition 5 / replication-factor: 3
#192.168.0.145/broker&zookeeper brokerID 2 / partition 5 / replication-factor: 3
#partition 5
```

**partition 的直接表现**

* partition 在kafka的日志目录中体现为：使用 {topic}-{partition} 格式命名的文件系统

  ```
  # 一个由三台kafka组成的集群，设置partition为5，新建topic名称[test-topic],根目录为root
  broker1:root
  test-topic-0
  test-topic-1
  broker2:root
  test-topic-2
  ...
  ```

  **Kafka 集群备份机制**
* 使用命令查看topic信息

  ```
  ./kafka-topics.sh --describe --zookeeper localhost:2181
  ```
* 使用命令创建topic

  ```
   ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 5 --topic par-topic
  ```

  *结果:*

  ![image.png](https://i.loli.net/2019/12/07/tKS6yfFhgiLVqXr.png)

  *描述：*

  *PartitionCount: 5 表示有3个分区*

  *ReplicationFactor: 3 表示复制因子为3*

  *Topic: par-topic   Partition: 0    Leader: 1       Replicas: 1,3,2  Isr: 1,3,2*

  ```
  # Partition: 0 分区下标是从0开始的，也就是第一个分区P0
  # Leader: 1 表示P0的Leader是在broker.id=1的集群节点上
  # Replicas: 1,3,2 列出了所有的副本节点，不管节点是否在服务中
  # Isr: 1,3,2 是正在服务中的节点，如果其中一个节点挂掉了，那么这里就会减少哪个挂掉节点的broker.id
  ```

  *PS*

  ```
  # 1. 当使用producer创建topic时，新产生的topic无法设置复制因子
  # 2. 当使用命令创建topic时，复制因子生效
  ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.0xffff.com/kafka.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
