使用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);
}
}
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
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。