0%

zookeeper注册中心

zookeeper注册中心

使用zookeeper作为注册中心就不需要像eureka一样,在写一个eureka-server的服务了,因为zookeeper本身就是一个服务端,所以只需要编写需要进行服务注册的客户端即可

依赖

1
2
3
4
5
<!-- zookeeper 注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

主程序

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperApp {

public static void main(String[] args) {
SpringApplication.run(ZookeeperApp.class, args);
}
}

配置zookeeper注册中心

1
2
3
4
5
6
spring:
application:
name: provide-zookeeper
cloud:
zookeeper:
connect-string: localhost:2181 # zookeeper连接地址

启动该服务后,在zookeeper中就可以看到在services目录下有了一个provide-zookeeper文件夹,provide-zookeeper就是指的provide-zookeeper服务,下面会有一个个的实例,可以获取到实例的信息

1
2
get /services/provide-zookeeper/39a78506-f2cb-4c8f-bbab-768f1f9fc702
{"name":"provide-zookeeper","id":"39a78506-f2cb-4c8f-bbab-768f1f9fc702","address":"192.168.1.124","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"provide-zookeeper","metadata":{}},"registrationTimeUTC":1630313091916,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}

服务节点在zookeeper中注册的是临时节点

可以看到该节点的ephemeralOwner不为0