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目录,执行命令:
mac/linux :./zkServer.sh status
windows:zkServer.cmd status
查看当前zookeeper的运行状态,如下图所示,是没有启动的信息
xx:bin lin$ ./zkServer.sh status
ZooKeeper 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.
执行命令启动zookeeper命令
mac/linux :./zkServer.sh start
windows:zkServer.cmd start
控制台输出如下信息说明zookeeper启动成功
xxx:bin lin$ ./zkServer.sh start
ZooKeeper JMX enabled by defaultUsing config: /Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
控制台输入zkServer.cmd status 查看启动结果:
xxx:bin lin$ ./zkServer.sh status
ZooKeeper JMX enabled by defaultUsing config: /Users/lin/Documents/temp/zookeeper-3.4.12 2/bin/../conf/zoo.cfg
Mode: standalone
如上所示zookeeper正在以standalone模式运行中;
接下来用客户端命令来链接服务端,执行如下命令:
mac/linux :./zkCli.sh
windows:zkCli.cmd
查看一下控制台输出结果:
xxx:bin lin$ ./zkCli.sh
Connecting 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
Welcome 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)
JLine 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]
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
好了客户端链接上服务器端了。
创建znode节点,执行命令
#创建一个zone节点node_1 对应的value值为“abc”
create /node_1 abc
#控制台输出
[zk: localhost:2181(CONNECTED) 5] create /node_1 abc
Created /node_1
查询创建的znode 节点
#创建一个zone节点node_1 对应的value值为“abc”
get /node_1#控制台输出
[zk: localhost:2181(CONNECTED) 6] get /node_1
abc
cZxid = 0x9
ctime = Fri May 25 19:00:02 CST 2018
mZxid = 0x9
mtime = Fri May 25 19:00:02 CST 2018
pZxid = 0x9
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
zookeeper 常用命令总结
#服务命令
1. 启动ZK服务: sh bin/zkServer.sh start
2. 查看ZK服务状态: sh bin/zkServer.sh status
3. 停止ZK服务: sh bin/zkServer.sh stop
4. 重启ZK服务: sh bin/zkServer.sh restart
#客户端命令
1. 显示根目录下、文件: ls / 使用 ls 命令来查看当前 ZooKeeper 中所包含的内容
2. 显示根目录下、文件: ls2 / 查看当前节点数据并能看到更新次数等数据
3. 创建文件,并设置初始内容: create /node_1 "test" 创建一个新的 znode节点“ node_1 ”以及与它关联的字符串
4. 获取文件内容: get /node_1 确认 znode 是否包含我们所创建的字符串
5. 修改文件内容: set /node_1 "zkbak" 对 zk 所关联的字符串进行设置
6. 删除文件: delete /node_1 将刚才创建的 zone 删除
7. 退出客户端: quit
8. 帮助命令: help
关注我们
更多精彩内容请关注“IT实战联盟”公众号,也可以加入交流群和作者互撩哦~~~
注意:本文归作者所有,未经作者允许,不得转载