SpringBoot+zk+dubbo架构实践(一):本地部署zookeeper

SpringBoot+zk+dubbo架构实践系列实现目标

本地部署zookeeper

SpringBoot 集成 zookeeper

Dubbo-admin管理平台安装

SpringBoot+ zookeeper +dubbo 框架搭建

架构业务实践(实现电商部分业务)

备注:本系列偏向于动手实践,不讲太多理论。

前言

本文操作都是在Mac上进行的,如果大家用的windows 可以相对应的进行修改一下。

下载zookeeper

首先去官网下载zookeeper包,这里使用3.4.12版本(也可以选择其他的),在这里下载:http://apache.claz.org/zookeeper/zookeeper-3.4.12/
下载并解压,目录如下图所示:

下载后在本地解压,进入zookeeper-3.4.12/conf目录下,将zoo_sample.cfg文件改名为zoo.cfg,再用文本编辑器打开这个文件,修改如下红框位置的内容,改成本地一个可以访问的已有目录:

备注:如果是windows可以选择磁盘路径

打开命令行窗口,进入zookeeper-3.4.12/bin目录,执行命令:

1mac/linux :./zkServer.sh status 2windows:zkServer.cmd status 3

查看当前zookeeper的运行状态,如下图所示,是没有启动的信息

1xx:bin lin$ ./zkServer.sh status 2ZooKeeper JMX enabled by defaultUsing config: /Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf/zoo.cfgError contacting service. It is probably not running. 3

执行命令启动zookeeper命令

1mac/linux :./zkServer.sh start 2windows:zkServer.cmd start 3

控制台输出如下信息说明zookeeper启动成功

1xxx:bin lin$ ./zkServer.sh start 2ZooKeeper JMX enabled by defaultUsing config: /Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf/zoo.cfg 3Starting zookeeper ... STARTED 4

控制台输入zkServer.cmd status 查看启动结果:

1xxx:bin lin$ ./zkServer.sh status 2ZooKeeper JMX enabled by defaultUsing config: /Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf/zoo.cfg 3Mode: standalone 4

如上所示zookeeper正在以standalone模式运行中;

接下来用客户端命令来链接服务端,执行如下命令:

1mac/linux :./zkCli.sh 2windows:zkCli.cmd 3

查看一下控制台输出结果:

1xxx:bin lin$ ./zkCli.sh 2Connecting to localhost:21812018-05-25 17:27:58,908 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT2018-05-25 17:27:58,914 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=192.168.2.162018-05-25 17:27:58,914 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_912018-05-25 17:27:58,916 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation2018-05-25 17:27:58,916 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../build/classes:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../build/lib/*.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/slf4j-log4j12-1.7.25.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/slf4j-api-1.7.25.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/netty-3.10.6.Final.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/log4j-1.2.17.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/jline-0.9.94.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../lib/audience-annotations-0.5.0.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../zookeeper-3.4.12.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../src/java/lib/*.jar:/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf:2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/Users/lin/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/cf/q_dg400d5ql4vxlv7c4h_qvm0000gn/T/2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Mac OS X2018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=x86_642018-05-25 17:27:58,917 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=10.11.32018-05-25 17:27:58,918 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=lin2018-05-25 17:27:58,918 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/Users/lin2018-05-25 17:27:58,918 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/Users/lin/Documents/temp/zookeeper-3.4.12 2/bin2018-05-25 17:27:58,919 [myid:] - INFO  [main:ZooKeeper@441] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5a2e4553 3Welcome to ZooKeeper!2018-05-25 17:27:58,944 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) 4JLine support is enabled2018-05-25 17:27:59,022 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@878] - Socket connection established to localhost/127.0.0.1:2181, initiating session2018-05-25 17:27:59,056 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100448cddc80000, negotiated timeout = 30000[zk: localhost:2181(CONNECTED) 0] 5WATCHER:: 6 7WatchedEvent state:SyncConnected type:None path:null 8

好了客户端链接上服务器端了。

创建znode节点,执行命令

1#创建一个zone节点node_1 对应的value值为“abc” 2create /node_1 abc 3#控制台输出 4[zk: localhost:2181(CONNECTED) 5] create /node_1 abc 5Created /node_1 6

查询创建的znode 节点

1#创建一个zone节点node_1 对应的value值为“abc” 2get /node_1#控制台输出 3[zk: localhost:2181(CONNECTED) 6] get /node_1 4abc 5cZxid = 0x9 6ctime = Fri May 25 19:00:02 CST 2018 7mZxid = 0x9 8mtime = Fri May 25 19:00:02 CST 2018 9pZxid = 0x9 10cversion = 0 11dataVersion = 0 12aclVersion = 0 13ephemeralOwner = 0x0 14dataLength = 3 15numChildren = 0 16

zookeeper 常用命令总结

1#服务命令 21. 启动ZK服务:       sh bin/zkServer.sh start 32. 查看ZK服务状态: sh bin/zkServer.sh status 43. 停止ZK服务:       sh bin/zkServer.sh stop 54. 重启ZK服务:       sh bin/zkServer.sh restart 6#客户端命令 71. 显示根目录下、文件: ls / 使用 ls 命令来查看当前 ZooKeeper 中所包含的内容 82. 显示根目录下、文件: ls2 / 查看当前节点数据并能看到更新次数等数据 93. 创建文件,并设置初始内容: create /node_1 "test" 创建一个新的 znode节点“ node_1 ”以及与它关联的字符串 104. 获取文件内容: get /node_1 确认 znode 是否包含我们所创建的字符串 115. 修改文件内容: set /node_1 "zkbak" 对 zk 所关联的字符串进行设置 126. 删除文件: delete /node_1 将刚才创建的 zone 删除 137. 退出客户端: quit 148. 帮助命令: help 15

代码交流 2021