Zookeeper系列(5):使用Zookeeper作为注册中心
服务注册
引入相关依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>zkdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zkdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件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() {
List<ServiceInstance> list = discoveryClient.getInstances("zkdemo");
list.stream().forEach(obj -> System.out.println(obj.getUri().toString()));
}
}
输出结果:
http://localhost:64401
http://localhost:64475
http://localhost:64514
服务调用这里就不演示了,可以使用Feign或RestTemplate进行调用。