Skip to content

Commit cf45c47

Browse files
committed
📝 Writing docs.
1 parent 550a245 commit cf45c47

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Kafka 安装部署
2+
3+
环境要求:JDK8、ZooKeeper
4+
5+
## 下载解压
6+
7+
进入官方下载地址:http://kafka.apache.org/downloads,选择合适版本。
8+
9+
解压到本地:
10+
11+
```
12+
> tar -xzf kafka_2.11-1.1.0.tgz
13+
> cd kafka_2.11-1.1.0
14+
```
15+
16+
现在您已经在您的机器上下载了最新版本的 Kafka。
17+
18+
## 启动服务器
19+
20+
由于 Kafka 依赖于 ZooKeeper,所以运行前需要先启动 ZooKeeper
21+
22+
```
23+
> bin/zookeeper-server-start.sh config/zookeeper.properties
24+
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
25+
...
26+
```
27+
28+
然后,启动 Kafka
29+
30+
```
31+
> bin/kafka-server-start.sh config/server.properties
32+
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
33+
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties)
34+
...
35+
```
36+
37+
## 停止服务器
38+
39+
执行所有操作后,可以使用以下命令停止服务器
40+
41+
```
42+
$ bin/kafka-server-stop.sh config/server.properties
43+
```
44+
45+
## 创建主题
46+
47+
创建一个名为 test 的 Topic,这个 Topic 只有一个分区以及一个备份:
48+
49+
```
50+
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
51+
```
52+
53+
## 生产者生产消息
54+
55+
运行生产者,然后可以在控制台中输入一些消息,这些消息会发送到服务器:
56+
57+
```
58+
> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
59+
This is a message
60+
This is another message
61+
```
62+
63+
## 消费者消费消息
64+
65+
启动消费者,然后获得服务器中 Topic 下的消息:
66+
67+
```
68+
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
69+
This is a message
70+
This is another message
71+
```
72+
73+
## 集群部署
74+
75+
复制配置为多份(Windows 使用 copy 命令代理):
76+
77+
```
78+
> cp config/server.properties config/server-1.properties
79+
> cp config/server.properties config/server-2.properties
80+
```
81+
82+
修改配置:
83+
84+
```
85+
config/server-1.properties:
86+
broker.id=1
87+
listeners=PLAINTEXT://:9093
88+
log.dir=/tmp/kafka-logs-1
89+
90+
config/server-2.properties:
91+
broker.id=2
92+
listeners=PLAINTEXT://:9094
93+
log.dir=/tmp/kafka-logs-2
94+
```
95+
96+
其中,broker.id 这个参数必须是唯一的。
97+
98+
端口故意配置的不一致,是为了可以在一台机器启动多个应用节点。
99+
100+
根据这两份配置启动三个服务器节点:
101+
102+
```
103+
> bin/kafka-server-start.sh config/server.properties &
104+
...
105+
> bin/kafka-server-start.sh config/server-1.properties &
106+
...
107+
> bin/kafka-server-start.sh config/server-2.properties &
108+
...
109+
```
110+
111+
创建一个新的 Topic 使用 三个备份:
112+
113+
```
114+
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
115+
```
116+
117+
查看主题:
118+
119+
```
120+
> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
121+
Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:
122+
Topic: my-replicated-topic Partition: 0 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
123+
```
124+
125+
- leader - 负责指定分区的所有读取和写入的节点。每个节点将成为随机选择的分区部分的领导者。
126+
- replicas - 是复制此分区日志的节点列表,无论它们是否为领导者,或者即使它们当前处于活动状态。
127+
- isr - 是“同步”复制品的集合。这是副本列表的子集,该列表当前处于活跃状态并且已经被领导者捕获。

0 commit comments

Comments
 (0)