找回密码
 注册
查看: 388|回复: 2

Kafka环境搭建及使用教程

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2023-1-3 10:58:57 | 显示全部楼层 |阅读模式
kafka里自带了一个zookeeper,可以不需要再去下载zookeeper。在启动kafka前,需要先启动zookeeper。
9 Z9 |0 C' q) c  K( S3 _3 k, B
! N9 d# C8 L: t* E! q# u+ n& Q9 }(1)首先去修改config目录下的zookeeper.properties% b2 l8 s$ v7 Q! r& _, |
(2)修改config目录下的server.properties,修改log.dirs和zookeeper.connect。log.dirs是日志存放文件夹,zookeeper.connect是zookeeper连接地址(端口和clientPort保持一致)。
9 L; Q; ~0 V1 Q7 N( D  @  {' G$ I/ V+ i% G3 n  v$ K8 R
至此,kafka单机版环境已搭建完毕。(如果对里面的参数没修改的话,可不修改,直接用默认的)1 Y  C$ ?" n* W- U: ^
5 J9 V+ q) q! o
3 q, s- P# {% Q% P0 Y. R
3 b& O$ ^9 n  `, {5 Z8 r" q" U
kafka使用教程2 s- D" k' I: j
(1)启动kafka自带的zookeeper(加了-daemon,可以让这个进程挂在后台进行,否则会发现是个阻塞进程,只能再开一个终端进行其他操作). f; F; ~! `5 P" e6 q9 s
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties* J3 n# U) R2 K/ W7 p* r( H

# Z3 ?1 V' x6 A  U& f: g" T(2)启动kafka+ C/ _0 C/ T- G0 a
bin/kafka-server-start.sh -daemon config/server.properties+ t9 c- }4 t* u
2 L% t2 i( n! ^2 W6 Y
(3)创建一个topic5 U, }+ R8 R( N$ f- z$ m
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
: Q1 d8 P# i3 `0 \
: ?8 }" ?. f8 S: S# b: e- J; `, }6 U! R
(4)查看topic列表
  Q, a) s' e" x( I% r9 cbin/kafka-topics.sh -list -zookeeper localhost:2181
7 R# y* w* l9 f) g# }( d7 x% g, |! V' F0 c4 ~7 n" `" [
(5)创建生产者进程  (9092是默认的broker-list端口)
  @# X/ c! N$ Q) K" Qbin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
7 R/ N/ W( I: c4 ]) s  K) N, c0 I& ?( R# j
(6)创建消费者进程
. l( X; c) w  }; [bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
- C+ X6 ]7 R1 n8 o5 [. k9 j8 i: p$ W/ F4 x2 B% e9 M# a
(7)外网访问 vi config/server.properties
/ l* _3 n! X6 q, Jadvertised.listeners=PLAINTEXT://192.168.8.65:9092
& @' Y8 S: l, h( U2 v, ]; W( j

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-1-3 11:28:57 | 显示全部楼层
cat config/zookeeper.properties 1 O7 p# B: @' K) {7 W3 S
# Licensed to the Apache Software Foundation (ASF) under one or more
; _. o$ x5 p( {$ J* R" }: a# contributor license agreements.  See the NOTICE file distributed with
6 x8 r+ }/ ~' X' e# this work for additional information regarding copyright ownership.
% o& h9 t* V) M& u  H# The ASF licenses this file to You under the Apache License, Version 2.0
: o/ K' b6 L/ Q# (the "License"); you may not use this file except in compliance with9 ^% y5 f( i- o6 q# v) T7 r
# the License.  You may obtain a copy of the License at1 _! [# n. d1 S+ S! g& K) K
#
7 _6 P! T3 ?8 H2 f4 P9 v- M3 Z7 Z5 b#    http://www.apache.org/licenses/LICENSE-2.0
$ b* q" R: w# A$ v$ d#
, d6 c9 _! k' g8 _- T# Unless required by applicable law or agreed to in writing, software
0 [/ m- E. l& G7 s# distributed under the License is distributed on an "AS IS" BASIS,* b: k% n1 B- |* H+ Z7 {
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% i, g) `2 }! b0 W# See the License for the specific language governing permissions and4 @' N) U, F1 m$ S9 G/ h
# limitations under the License.
. i2 c' g' [0 U5 z( m) i# the directory where the snapshot is stored.7 e4 P. O3 Z, G/ Q7 V4 l
dataDir=/mnt/kafka_2.13-3.3.1/data/zookeeper( E/ Z, F! ?/ d
# the port at which the clients will connect5 L% f( f6 y# f! y8 I
clientPort=2181  A# O; ^, T2 C4 q4 G1 g% a$ Y3 v
# disable the per-ip limit on the number of connections since this is a non-production config
; H: M! Q8 a9 `1 imaxClientCnxns=0
. T4 f* C, s$ Z: Y) W, ^# Disable the adminserver by default to avoid port conflicts.' q8 R7 @$ C5 I! {8 w! L
# Set the port to something non-conflicting if choosing to enable this
2 `) s8 P  F! M$ o+ n+ Jadmin.enableServer=false
8 K* p8 O6 x% m  Y: Z) I# admin.serverPort=80807 A/ j* S0 S+ G

) P; T2 c; p2 [, l$ D, c  l6 e+ |* Y* s' }
nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
0 S2 T- S& g. H0 z+ R4 G8 m$ ?

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2023-1-3 11:40:15 | 显示全部楼层
[root@ceph2 kafka_2.13-3.3.1]# egrep -v "^#|^$" config/server.properties ( A4 Z4 N1 f' N: Z  O) V) z
broker.id=0" o) ?$ K0 h, ^
listeners=PLAINTEXT://192.168.0.233:90924 f! R3 s# @1 X* F9 X
num.network.threads=3
9 V" Z% K2 p4 w8 \; Lnum.io.threads=8
' G/ b) X8 o4 w) F: [socket.send.buffer.bytes=102400
1 |4 ~  p  V7 dsocket.receive.buffer.bytes=102400
0 J6 X1 z# D6 X. k/ |  J) Esocket.request.max.bytes=104857600. N/ }) ?& X$ ?! H7 k8 g
log.dirs=/var/log/kafka-logs9 U: v8 d1 W0 E( e
num.partitions=1
8 ]! j1 x, r! p' }+ g2 ?- Hnum.recovery.threads.per.data.dir=1/ B$ `+ @" S  i+ ?$ d
offsets.topic.replication.factor=14 x0 I1 \% q, s! b0 o
transaction.state.log.replication.factor=1
! L* }# P. P. w, g8 d1 a+ `) s& u0 y/ Y/ ztransaction.state.log.min.isr=1
) p) Q# x% c- }7 G- @2 [. v% mlog.retention.hours=1683 G: H1 {) U* _/ D7 S7 h
log.retention.check.interval.ms=3000003 g% G; P5 d4 I, q; `. W
zookeeper.connect=localhost:2181
# _* v/ a" I3 M# w( k/ jzookeeper.connection.timeout.ms=180006 j' h! Z; J' ?5 x. |! z+ M
group.initial.rebalance.delay.ms=0" B  j- T5 Y9 }: k4 l$ W2 [
# O; m- r! C7 x" Y) f; g& n
3 V; b8 }6 u3 k- ^
[root@ceph2 kafka_2.13-3.3.1]# nohup bin/kafka-server-start.sh config/server.properties &
5 ?  W% l4 k; J& M
0 E, N; i- d" B
1 N" V9 ^5 x0 O  U+ t* w/ ]
' n; K. L- ]2 {- Hnetstat  -ntlp |grep java4 r0 @0 j# j) j0 m1 o' n/ y6 |1 X
tcp6       0      0 192.168.0.233:9092      :::*                    LISTEN      21222/java          " U! }9 R8 m; x8 u: T/ b" N: ~2 Q
tcp6       0      0 :::2181                 :::*                    LISTEN      20780/java         
6 P6 y* ]) C. V3 E4 Ttcp6       0      0 :::41467                :::*                    LISTEN      21222/java         
2 g' S+ }2 J/ I. D; G0 h- ztcp6       0      0 :::40571                :::*                    LISTEN      20780/java          - t! p$ Y0 A  ^! g2 H" H" C& Y3 ~

1 F$ {" m% P) E/ o# G
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 03:23 , Processed in 0.021258 second(s), 23 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表