实践 Zookeeper


本文包含的内容:

  • 如何安装ZK
  • 如何启动ZK
  • 如何查看ZK的数据
  • ZK有哪些用途

安装ZK

不需要安装。跟下载tomcat、kafka等等一样,解压即可,不过它需要依赖jdk,因此PATH环境变量中要包含java。

方法1:从官网下载,解压之后,bin目录下,有window和linux的运行命令,window的命令结尾是 xx.cmd,linux和mac是 xxx.sh。

$ tree -L 1
.
├── bin
│ ├── README.txt
│ ├── zkCleanup.sh
│ ├── zkCli.cmd
│ ├── zkCli.sh
│ ├── zkEnv.cmd
│ ├── zkEnv.sh
│ ├── zkServer.cmd
│ ├── zkServer-initialize.sh
│ ├── zkServer.sh
│ ├── zkSnapshotComparer.cmd
│ ├── zkSnapshotComparer.sh
│ ├── zkSnapShotToolkit.cmd
│ ├── zkSnapShotToolkit.sh
│ ├── zkTxnLogToolkit.cmd
│ └── zkTxnLogToolkit.sh
├── conf
│ ├── configuration.xsl
│ ├── logback.xml
│ └── zoo_sample.cfg
├── docs
├── lib
├── LICENSE.txt
├── NOTICE.txt
├── README.md
└── README_packaging.md

方法2: 下载kafka之后,它里面自带ZK。例如:~/soft/kafka_2.13-2.8.0

可以看到kafka的bin目录下面,有启动和停止ZK相关的命令:zookeeper-server-start.sh,zookeeper-server-stop.sh。

~/soft/kafka_2.13-2.8.0
├── bin
│ ├── connect-distributed.sh
│ ├── connect-mirror-maker.sh
│ ├── connect-standalone.sh
│ ├── kafka-acls.sh
│ ├── kafka-broker-api-versions.sh
│ ├── kafka-cluster.sh
│ ├── kafka-configs.sh
│ ├── kafka-console-consumer.sh
│ ├── kafka-console-producer.sh
│ ├── kafka-consumer-groups.sh
│ ├── kafka-consumer-perf-test.sh
│ ├── kafka-delegation-tokens.sh
│ ├── kafka-delete-records.sh
│ ├── kafka-dump-log.sh
│ ├── kafka-features.sh
│ ├── kafka-leader-election.sh
│ ├── kafka-log-dirs.sh
│ ├── kafka-metadata-shell.sh
│ ├── kafka-mirror-maker.sh
│ ├── kafka-preferred-replica-election.sh
│ ├── kafka-producer-perf-test.sh
│ ├── kafka-reassign-partitions.sh
│ ├── kafka-replica-verification.sh
│ ├── kafka-run-class.sh
│ ├── kafka-server-start.sh
│ ├── kafka-server-stop.sh
│ ├── kafka-storage.sh
│ ├── kafka-streams-application-reset.sh
│ ├── kafka-topics.sh
│ ├── kafka-verifiable-consumer.sh
│ ├── kafka-verifiable-producer.sh
│ ├── trogdor.sh
│ ├── windows
│ ├── zookeeper-security-migration.sh
│ ├── zookeeper-server-start.sh
│ ├── zookeeper-server-stop.sh
│ └── zookeeper-shell.sh

├── config
│ ├── connect-console-sink.properties
│ ├── connect-console-source.properties
│ ├── connect-distributed.properties
│ ├── connect-file-sink.properties
│ ├── connect-file-source.properties
│ ├── connect-log4j.properties
│ ├── connect-mirror-maker.properties
│ ├── connect-standalone.properties
│ ├── consumer.properties
│ ├── kraft
│ ├── log4j.properties
│ ├── producer.properties
│ ├── server1.properties
│ ├── server.properties
│ ├── tools-log4j.properties
│ ├── trogdor.conf
│ └── zookeeper.properties
├── libs
├── LICENSE
├── licenses
├── logs
├── NOTICE
└── site-docs

如何启动ZK

这个更简单了,bin目录下,有启动脚本,直接执行即可,不过需要注意启动参数。

如果使用kafka集成的ZK,启动命令(在bin目录下,window系统):zookeeper-server-start.bat ..\..\config\zookeeper.properties

针对 Window 环境,可以在user home目录定义一个脚本(名字:startZK.bat),然后从Start菜单打开cmd,直接执行脚本即可快速启动ZK(省去了切换目录,然后敲命令),然后关闭的话,直接 Ctrl + C 即可停掉ZK。

@echo on
cd C:\develop\soft\kafka_2.13-2.8.0\bin\windows
zookeeper-server-start.bat ..\..\config\zookeeper.properties
pause

如何查看ZK的数据

要查看ZooKeeper中的数据,你可以使用ZooKeeper自带的CLI(命令行接口)客户端,名为zkCli.sh(在Unix/Linux上)或zkCli.cmd(在Windows上)。这是一个交互式的shell,可以用来执行各种ZooKeeper命令。

以下是如何使用zkCli来查看ZooKeeper中的数据的基本步骤:

  1. 启动zkCli客户端
    在你的ZooKeeper安装目录中,打开终端或命令行窗口,然后运行对应的zkCli命令。
  2. 连接到ZooKeeper服务器
    默认情况下,zkCli会尝试连接到localhost:2181。如果你的ZooKeeper服务器配置在其他地址或端口,可以使用-server参数指定,如:
zkCli.sh -server your.zookeeper.server:2181
  • 列出ZooKeeper中的znodes
    一旦连接到ZooKeeper服务器,你可以使用ls命令来查看根目录下的znodes:
[zk: your.zookeeper.server:2181(CONNECTED) 0] ls / 
  • 查看znode的数据
    使用get命令 followed by the znode路径来查看znode的数据:
[zk: your.zookeeper.server:2181(CONNECTED) 1] get /myznode
  • 执行其他操作
    你可以使用其他命令(如create, set, delete等)来在ZooKeeper中执行操作。

  • 退出zkCli客户端
    使用quit命令来退出客户端:

[zk: your.zookeeper.server:2181(CONNECTED) 2] quit

此外,也有第三方工具和UI界面,如ZooInspectorKazoo, 供你查看和管理ZooKeeper数据。但是,对于简单的操作,zkCli应该足够了。

ZK有哪些用途

Dubbo应用使用ZK作为注册中心

代码来源:Spring Boot 快速开发 Dubbo 服务 | Apache Dubbo

在Dubbo应用程序中,一般使用ZK作为注册中心,启动ZK之后,再接着启动provider和consumer两个服务,他们会把自己注册到ZK中,然后通过相同的名字找到彼此。

# provider启动输出的log
[main] org.apache.dubbo.config.ServiceConfig : [DUBBO] Successfully registered interface application mapping for service org.apache.dubbo.springboot.demo.DemoService, dubbo version: 3.2.0, current host: 172.17.0.1
[main] o.a.d.r.zookeeper.ZookeeperRegistry : [DUBBO] Notify urls for subscribe url provider://172.17.0.1:20881/org.apache.dubbo.metadata.MetadataService?anyhost=true&application=dubbo-springboot-demo-provider&background=false&bind.ip=172.17.0.1&bind.port=20881&category=configurators&check=false&delay=0&deprecated=false&dubbo=2.0.2&dynamic=true&executor-management-mode=isolation&file-cache=true&generic=false&getAndListenInstanceMetadata.1.callback=true&getAndListenInstanceMetadata.return=true&getAndListenInstanceMetadata.sent=true&group=dubbo-springboot-demo-provider&interface=org.apache.dubbo.metadata.MetadataService&methods=exportInstanceMetadata,getAndListenInstanceMetadata,getExportedServiceURLs,getExportedURLs,getExportedURLs,getExportedURLs,getExportedURLs,getExportedURLs,getInstanceMetadataChangedListenerMap,getMetadataInfo,getMetadataInfos,getMetadataURL,getServiceDefinition,getServiceDefinition,getSubscribedURLs,isMetadataService,serviceName,toSortedStrings,toSortedStrings,version&pid=428346&prefer.serialization=fastjson2,hessian2&qos.enable=true&register=false&release=3.2.0&revision=3.2.0&service-name-mapping=true&side=provider&timestamp=1694605314901&version=1.0.0, url size: 1, dubbo version: 3.2.0, current host: 172.17.0.1

[main] org.apache.dubbo.config.ServiceConfig : [DUBBO] Try to register interface application mapping for service dubbo-springboot-demo-provider/org.apache.dubbo.metadata.MetadataService:1.0.0, dubbo version: 3.2.0, current host: 172.17.0.1
[main] org.apache.dubbo.config.ServiceConfig : [DUBBO] Successfully registered interface application mapping for service dubbo-springboot-demo-provider/org.apache.dubbo.metadata.MetadataService:1.0.0, dubbo version: 3.2.0, current host: 172.17.0.1
[main] .c.m.ConfigurableMetadataServiceExporter : [DUBBO] The MetadataService exports urls : [dubbo://172.17.0.1:20881/org.apache.dubbo.metadata.MetadataService?anyhost=true&application=dubbo-springboot-demo-provider&background=false&bind.ip=172.17.0.1&bind.port=20881&delay=0&deprecated=false&dubbo=2.0.2&dynamic=true&executor-management-mode=isolation&file-cache=true&generic=false&getAndListenInstanceMetadata.1.callback=true&getAndListenInstanceMetadata.return=true&getAndListenInstanceMetadata.sent=true&group=dubbo-springboot-demo-provider&interface=org.apache.dubbo.metadata.MetadataService&methods=exportInstanceMetadata,getAndListenInstanceMetadata,getExportedServiceURLs,getExportedURLs,getExportedURLs,getExportedURLs,getExportedURLs,getExportedURLs,getInstanceMetadataChangedListenerMap,getMetadataInfo,getMetadataInfos,getMetadataURL,getServiceDefinition,getServiceDefinition,getSubscribedURLs,isMetadataService,serviceName,toSortedStrings,toSortedStrings,version&pid=428346&prefer.serialization=fastjson2,hessian2&qos.enable=true&register=false&release=3.2.0&revision=3.2.0&service-name-mapping=true&side=provider&timestamp=1694605314901&version=1.0.0], dubbo version: 3.2.0, current host: 172.17.0.1
[main] o.a.d.r.c.m.ServiceInstanceMetadataUtils : [DUBBO] Start registering instance address to registry., dubbo version: 3.2.0, current host: 172.17.0.1
[main] org.apache.dubbo.metadata.MetadataInfo : [DUBBO] metadata revision changed: null -> f32e16da1c6a0dbceb5e063f52ece15a, app: dubbo-springboot-demo-provider, services: 1, dubbo version: 3.2.0, current host: 172.17.0.1
[main] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] Dubbo Application[1.1](dubbo-springboot-demo-provider) is ready., dubbo version: 3.2.0, current host: 172.17.0.1
[main] o.a.d.s.d.provider.ProviderApplication : Started ProviderApplication in 2.549 seconds (JVM running for 2.89)
[pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener : [Dubbo] Current Spring Boot Application is await...

# provider 退出输出log
[ionShutdownHook] o.a.d.registry.support.RegistryManager : [DUBBO] Close all registries [zookeeper://jack-win10.lan:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-springboot-demo-provider&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&pid=352356&qos.enable=true&release=3.2.0, zookeeper://jack-win10.lan:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-springboot-demo-provider&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&pid=352356&qos.enable=true&release=3.2.0], dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.r.zookeeper.ZookeeperRegistry : [DUBBO] Destroy registry:zookeeper://jack-win10.lan:2181/org.apache.dubbo.registry.RegistryService?application=dubbo-springboot-demo-provider&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&pid=352356&qos.enable=true&release=3.2.0, dubbo version: 3.2.0, current host: 172.17.0.1
[tor-Framework-0] o.a.c.f.imps.CuratorFrameworkImpl : backgroundOperationsLoop exiting
[ionShutdownHook] org.apache.zookeeper.ZooKeeper : Session: 0x100006915a60007 closed

consumer那边的输出:

# 启动时的log
[ main] f.a.ReferenceAnnotationBeanPostProcessor : [DUBBO] Register dubbo reference bean: demoService = ReferenceBean:org.apache.dubbo.springboot.demo.DemoService() at private org.apache.dubbo.springboot.demo.DemoService org.apache.dubbo.springboot.demo.consumer.Task.demoService, dubbo version: 3.2.0, current host: 172.17.0.1
[ main] f.a.ReferenceAnnotationBeanPostProcessor : [DUBBO] class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!, dubbo version: 3.2.0, current host: 172.17.0.1
[ main] o.a.d.c.s.c.DubboConfigBeanInitializer : [DUBBO] loading dubbo config beans ..., dubbo version: 3.2.0, current host: 172.17.0.1
[ main] o.a.d.c.s.c.DubboConfigBeanInitializer : [DUBBO] dubbo config beans are loaded., dubbo version: 3.2.0, current host: 172.17.0.1
[ main] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] No value is configured in the registry, the DynamicConfigurationFactory extension[name : zookeeper] supports as the config center, dubbo version: 3.2.0, current host: 172.17.0.1
[ main] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] The registry[<dubbo:registry address="zookeeper://jack-win10.lan:2181" protocol="zookeeper" port="2181" />] will be used as the config center, dubbo version: 3.2.0, current host: 172.17.0.1
[ main] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] use registry as config-center: <dubbo:config-center highestPriority="false" id="config-center-zookeeper-jack-win10.lan-2181" address="zookeeper://jack-win10.lan:2181" protocol="zookeeper" port="2181" parameters="{client=null}" />, dubbo version: 3.2.0, current host: 172.17.0.1
[ main] org.apache.curator.utils.Compatibility : Running in ZooKeeper 3.4.x compatibility mode

[nStateManager-0] o.a.d.r.z.c.CuratorZookeeperClient : [DUBBO] Curator zookeeper client instance initiated successfully, session id is 100006915a60014, dubbo version: 3.2.0, current host: 172.17.0.1

[ main] o.a.dubbo.config.context.ConfigManager : [DUBBO] <dubbo:registry address="zookeeper://jack-win10.lan:2181" protocol="zookeeper" port="2181" />, dubbo version: 3.2.0, current host: 172.17.0.1

# 退出时的log
[ionShutdownHook] org.apache.zookeeper.ZooKeeper : Session: 0x100006915a60014 closed
[ain-EventThread] org.apache.zookeeper.ClientCnxn : EventThread shut down for session: 0x100006915a60014
[ionShutdownHook] o.apache.dubbo.rpc.model.FrameworkModel : [DUBBO] Destroying default framework model: Dubbo Framework[1], dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.apache.dubbo.rpc.model.FrameworkModel : [DUBBO] Dubbo Framework[1] is destroying ..., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] Dubbo Application[1.0](DUBBO_INTERNAL_APPLICATION) is stopping., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.deploy.DefaultModuleDeployer : [DUBBO] Dubbo Module[1.0.0] is stopping., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.deploy.DefaultModuleDeployer : [DUBBO] Dubbo Module[1.0.0] has stopped., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.registry.support.RegistryManager : [DUBBO] Close all registries [], dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.t.m.DefaultExecutorRepository : [DUBBO] destroying application executor repository .., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.d.DefaultApplicationDeployer : [DUBBO] Dubbo Application[1.0](DUBBO_INTERNAL_APPLICATION) has stopped., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.apache.dubbo.rpc.model.FrameworkModel : [DUBBO] Dubbo Framework[1] is destroyed, dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.apache.dubbo.rpc.model.FrameworkModel : [DUBBO] Reset global default framework from Dubbo Framework[1] to null, dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.r.GlobalResourcesRepository : [DUBBO] Destroying global resources ..., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.r.GlobalResourcesRepository : [DUBBO] Dubbo is completely destroyed, dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.t.m.FrameworkExecutorRepository : [DUBBO] destroying framework executor repository .., dubbo version: 3.2.0, current host: 172.17.0.1
[ionShutdownHook] o.a.d.c.s.c.DubboSpringInitializer : [DUBBO] Unbind Dubbo Module[1.1.1] from spring container: org.springframework.beans.factory.support.DefaultListableBeanFactory@e45f292, dubbo version: 3.2.0, current host: 172.17.0.1

文章作者: IT神助攻
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IT神助攻 !
  目录