zookeeper注册中心
使用zookeeper作为注册中心就不需要像eureka一样,在写一个eureka-server的服务了,因为zookeeper本身就是一个服务端,只需要编写需要进行服务注册的客户端即可
依赖
xml
<!-- zookeeper 注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
主程序
java
@SpringBootApplication
@EnableDiscoveryClient
public class ZookeeperApp {
public static void main(String[] args) {
SpringApplication.run(ZookeeperApp.class, args);
}
}
配置zookeeper注册中心
yml
spring:
application:
name: provide-zookeeper
cloud:
zookeeper:
connect-string: localhost:2181 # zookeeper连接地址
启动该服务后,zookeeper中就可以看到services目录下有了一个provide-zookeeper文件夹,provide-zookeeper就是指的provide-zookeeper服务,下面会有一个个的实例,可以获取到实例的信息
zk
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
https://zhhll.icu/2021/框架/微服务/springcloud/注册中心/zookeeper/1.zookeeper注册中心/