引入相关依赖:
<&#63;xml version="1.0" encoding="UTF-8"&#63;>4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.5 com.example zkdemo 0.0.1-SNAPSHOT zkdemo Demo project for Spring Boot 1.8 2020.0.2 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
配置文件application.yml:
spring: cloud: zookeeper: # Zookeeper服务器地址,集群则以逗号分隔 connect-string: localhost:2181 application: name: zkdemo # 使用随机端口 server: port: 0
主程序类ZkdemoApplication:
@SpringBootApplication public class ZkdemoApplication { public static void main(String[] args) { SpringApplication.run(ZkdemoApplication.class, args); } }
由于配置了使用随机端口,这里直接启动三个ZkdemoApplication运行三个zkdemo服务示例。
启动成功后,在zookeeper中查看节点的变化,会发现在根节点下多出来一个services节点,services节点下是以服务名称命名的服务节点,服务节点下是该服务的实例节点。
[zk: localhost:2181(CONNECTED) 96] ls -R / / /services /services/zkdemo /services/zkdemo/2030bca0-db25-411d-b1f5-84c790bd1d6f /services/zkdemo/208834cf-e7e4-496e-a5c0-afcbb78e120f /services/zkdemo/2c4177e1-20fd-4c66-9ee0-eaf21253039d
实例节点中存储了服务名称、ip地址、端口号、实例ID等相关信息:
[zk: localhost:2181(CONNECTED) 97] get /services/zkdemo/208834cf-e7e4-496e-a5c0-afcbb78e120f {"name":"zkdemo","id":"208834cf-e7e4-496e-a5c0-afcbb78e120f","address":"localhost","port":64514,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"zkdemo","name":"zkdemo","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1619424602784,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}} [zk: localhost:2181(CONNECTED) 98] get /services/zkdemo/2030bca0-db25-411d-b1f5-84c790bd1d6f {"name":"zkdemo","id":"2030bca0-db25-411d-b1f5-84c790bd1d6f","address":"localhost","port":64401,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"zkdemo","name":"zkdemo","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1619424361059,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}} [zk: localhost:2181(CONNECTED) 99] get /services/zkdemo/2c4177e1-20fd-4c66-9ee0-eaf21253039d {"name":"zkdemo","id":"2c4177e1-20fd-4c66-9ee0-eaf21253039d","address":"localhost","port":64475,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"zkdemo","name":"zkdemo","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1619424510719,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
通过ephemeralOwner字段可以看出,services节点和服务节点为持久节点,实例节点为临时节点:
[zk: localhost:2181(CONNECTED) 100] stat /services cZxid = 0x118 ctime = Mon Apr 26 16:06:02 CST 2021 mZxid = 0x118 mtime = Mon Apr 26 16:06:02 CST 2021 pZxid = 0x119 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1 [zk: localhost:2181(CONNECTED) 101] stat /services/zkdemo cZxid = 0x119 ctime = Mon Apr 26 16:06:02 CST 2021 mZxid = 0x119 mtime = Mon Apr 26 16:06:02 CST 2021 pZxid = 0x11e cversion = 3 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 3 [zk: localhost:2181(CONNECTED) 102] stat /services/zkdemo/2030bca0-db25-411d-b1f5-84c790bd1d6f cZxid = 0x11a ctime = Mon Apr 26 16:06:02 CST 2021 mZxid = 0x11a mtime = Mon Apr 26 16:06:02 CST 2021 pZxid = 0x11a cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x100000122cf000d dataLength = 514 numChildren = 0
当某个实例出现故障停止时,对应的临时节点也会被删除。当服务节点下所有实例节点被删除时,服务节点也会被删除。当services节点下的所有服务节点被删除时,services节点也会被删除。
通过DiscoveryClient从注册中心获取对应服务的实例列表:
@SpringBootTest class ZkdemoApplicationTests { @Autowired private DiscoveryClient discoveryClient; @Test public void serviceUrl() { Listlist = discoveryClient.getInstances("zkdemo"); list.stream().forEach(obj -> System.out.println(obj.getUri().toString())); } }
输出结果:
http://localhost:64401 http://localhost:64475 http://localhost:64514
服务调用这里就不演示了,可以使用Feign或RestTemplate进行调用。
以上就是SpringCloud使用Zookeeper作为注册中心的详细内容,更多关于Zookeeper作为注册中心的资料请关注其它相关文章!