SpringCloud-03-Zookeeper

在前面介绍SpringCloud Eureka已经停止更新,如果担心Eureka技术过时,可以选用 Zookeeper 作为注册中心,作为服务注册与发现。其实了解Dubbo的朋友应该知道。在Dubbo中,也是推荐使用Zookeeper作为注册中心,Dubbo是一门RPC框架,用于远程过程调用,在这里就不对Dubbo进行介绍。
ZooKeeper 是一个开放源代码的分布式协调服务,在之前,我已经写过几篇关于Zookeeper的文章,在系列文章中讲解了Zookeeper的作用,原理,安装和简单使用。不了解Zookeeper的工作原理的朋友,需要先去翻翻前面我写的Zookeeper相关的文章。

接下来就演示如何使用 Zookeeper 作为注册中心,实现服务注册与发现。
业务场景:用户服务UserService通过Zookeeper注册中心,获取微信服务WechatService的服务列表,然后发送http请求,获取用户的微信用户信息。
整个过程如图所示:

1 搭建Zookeeper集群

1.1 配置和启动 192.168.43.51上的Zookeeper服务

上传 zookeeper安装包并解压,复制 zoo_sample.cfg 为配置文件 zoo.cfg。

[tag@localhost ~]$ ll apache-zookeeper-3.6.1-bin.tar.gz 
-rw-r--r--. 1 tag tag 12436328 8月  10 2020 apache-zookeeper-3.6.1-bin.tar.gz
[tag@localhost ~]$ tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz 
[tag@localhost ~]$ mv apache-zookeeper-3.6.1-bin zk-3.6.1
[tag@localhost ~]$ cd zk-3.6.1/conf
[tag@localhost conf]$ cp zoo_sample.cfg zoo.cfg
[tag@localhost conf]$ 

修改配置文件 zoo.cfg

[tag@localhost conf]$ vim zoo.cfg 
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
#配置 ZooKeeper 服务器存储快照文件的目录
dataDir=/home/tag/zk-3.6.1/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

server.1=192.168.43.51:2888:3888
server.2=192.168.43.52:2888:3888
server.3=192.168.43.53:2888:3888

新增 myid 文件

[tag@localhost conf]$ mkdir /home/tag/zk-3.6.1/data
[tag@localhost conf]$ cd /home/tag/zk-3.6.1/data
[tag@localhost data]$ vi myid
1
~    

启动zookeeper服务,可以看到 2181 端口已启动成功

[tag@localhost data]$ /home/tag/zk-3.6.1/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/tag/zk-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[tag@localhost data]$ ss -nultp | grep 2181
tcp    LISTEN     0      50       :::2181                 :::*                   users:(("java",pid=31393,fd=56))

1.2 配置和启动 192.168.43.52 和 192.168.43.53上 的Zookeeper服务

根据192.168.43.51机器上的zookeeper服务安装步骤,搭建zookeeper。
**注意:**在192.168.43.52上修改数据目录/home/tag/zk-3.6.1/data下面的myid文件内容为2,而192.168.43.53中的myid文件内容为3。

修改好了myid文件后,分别启动zookeeper服务。

查看集群状态

192.168.43.51

[tag@localhost ~]$ /home/tag/zk-3.6.1/bin/zkServer.sh status
Zo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值