三、服务注册与发现ZooKeeper
使用ZooKeeper作为注册中心的操作......
·
Zookeeper
是一个分布式协调工具,可以实现注册中心功能。Zookeeper
服务器取代Eureka
,作为服务注册中心。
一、安装zookeeper
①解压zookeeper-3.4.9.tar.gz
:tar zxvf zookeeper-3.4.6.tar.gz
②复制zoo_sample.cfg
配置文件:cp ./zoo_sample.cfg ./zoo.cfg
③新建目录:/opt/zookeeper-3.4.6/data
④vim /opt/zookeeper-3.4.9/conf/zoo.cfg
修改:dataDir=/opt/zookeeper-3.4.9/data
⑤启动ZooKeeper
:
[root@right bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg
查看ZooKeeper
的状态:
[root@right bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone
进入到ZooKeeper
客户端:./zkCli.sh
二、ZooKeeper作为注册中心的使用
添加依赖:
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自带的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.6版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
使用ZooKeeper
作为注册中心时的配置:
#注册zookeeper到注册中心名称
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.182.200:2181
主启动类添加注解:@EnableDiscoveryClient
启动项目,进入到zookeeper
客户端:
查看服务节点:
[zk: localhost:2181(CONNECTED) 4] ls /services
[cloud-provider-payment, cloud-consumer-order]
查看具体的节点:
ls /services/cloud-provider-payment
[b2dc680d-e783-4a59-a462-a6d67c3772bb]
[zk: localhost:2181(CONNECTED) 5] get /services/cloud-provider-payment/b2dc680d-e783-4a59-a462-a6d67c3772bb
{"name":"cloud-provider-payment","id":"b2dc680d-e783-4a59-a462-a6d67c3772bb","address":"LAPTOP-O6D4GJPM","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1586969705981,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
cZxid = 0x20
ctime = Thu Apr 16 00:55:33 CST 2020
mZxid = 0x20
mtime = Thu Apr 16 00:55:33 CST 2020
pZxid = 0x20
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1717ca3134d0009
dataLength = 536
numChildren = 0
对json
格式化
{
"name": "cloud-provider-payment",
"id": "b2dc680d-e783-4a59-a462-a6d67c3772bb",
"address": "LAPTOP-O6D4GJPM",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {}
},
"registrationTimeUTC": 1586969705981,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
可以看到节点的具体信息。
其他的操作同Eureka
差不多。
更多推荐
已为社区贡献11条内容
所有评论(0)