我刚学kafka的时候,对这几个概念有时候会混淆,尤为是配置的时候常常搞不清楚它们的区别。这篇文章打算作一个梳理。shell
broker指的是kafka的服务端,能够是一个服务器也能够是一个集群。producer和consumer都至关于这个服务端的客户端。bootstrap
broker-list指定集群中的一个或者多个服务器,通常咱们再使用console producer的时候,这个参数是必备参数,另一个必备的参数是topic,以下示例:windows
C:\kafka\kafka_2.12-1.1.1 λ .\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test20190713 >this is a test >
本地主机若是要模拟多个broker,方法是复制多个server.properties,而后修改里面的端口, broker.id等配置模拟多个broker集群。服务器
好比模拟三个broker的状况,首先把config 目录下的 server.properties 复制两份,分别命名为 server-1.properties 和 server-2.properties,而后修改这两个文件的下列属性,ide
server-1.properties:测试
broker.id=1 listeners=PLAINTEXT://:9093 log.dirs=C:/kafka/broker1
server-2.properties:this
broker.id=2 listeners=PLAINTEXT://:9094 log.dirs=C:/kafka/broker2
broker.id 用来惟一标识每个 broker,每一个broker都有一个惟一的id值用来区分彼此。Kafka在启动时会在zookeeper中/brokers/ids路径下建立一个与当前broker的id为名称的虚节点,Kafka的健康状态检查就依赖于此节点。spa
咱们能够打开一个zk的客户端,经过ls命令来查看下这个路径下的内容:code
λ .\bin\windows\zookeeper-shell.bat localhost:2181 Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is disabled ls WATCHER:: WatchedEvent state:SyncConnected type:None path:null ls /brokers/ids [0]
能够看到咱们默认启动的这个broker.id为0的节点。server
bootstrap-servers指的是目标集群的服务器地址,这个和broker-list功能是同样的,只不过咱们在console producer要求用后者。
之前咱们使用console consumer测试消息收发时会这样写:
C:\kafka\kafka_2.12-1.1.1 λ .\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test20190713 Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
这样能够接收到生产者控制台发送的消息。
如今咱们也能够这样写,
C:\kafka\kafka_2.12-1.1.1 λ .\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test20190713
你能够本身测试下,也是能够收到消息的。
前者是老版本的用法,0.8之前的kafka,消费的进度(offset)是写在zk中的,因此consumer须要知道zk的地址。后来的版本都统一由broker管理,因此就用bootstrap-server了。
bootstrap-server还能够自动发现其它的broker。
欢迎你们关注个人公众号