2021年4月30日星期五

Zookeeper系列(5):使用Zookeeper作为注册中心

服务注册

引入相关依赖:

<?

配置文件application.yml:

spring: cloud: zookeeper:  # Zookeeper服务器地址,集群则以逗号分隔  connect-string: localhost:2181 application: name: zkdemo# 使用随机端口server: port: 0

主程序类ZkdemoApplication:

@SpringBootApplicationpublic 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 = 0x118ctime = Mon Apr 26 16:06:02 CST 2021mZxid = 0x118mtime = Mon Apr 26 16:06:02 CST 2021pZxid = 0x119cversion = 1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 0numChildren = 1[zk: localhost:2181(CONNECTED) 101] stat /services/zkdemo cZxid = 0x119ctime = Mon Apr 26 16:06:02 CST 2021mZxid = 0x119mtime = Mon Apr 26 16:06:02 CST 2021pZxid = 0x11ecversion = 3dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 0numChildren = 3[zk: localhost:2181(CONNECTED) 102] stat /services/zkdemo/2030bca0-db25-411d-b1f5-84c790bd1d6f cZxid = 0x11actime = Mon Apr 26 16:06:02 CST 2021mZxid = 0x11amtime = Mon Apr 26 16:06:02 CST 2021pZxid = 0x11acversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x100000122cf000ddataLength = 514numChildren = 0

当某个实例出现故障停止时,对应的临时节点也会被删除。当服务节点下所有实例节点被删除时,服务节点也会被删除。当services节点下的所有服务节点被删除时,services节点也会被删除。

服务发现

通过DiscoveryClient从注册中心获取对应服务的实例列表:

@SpringBootTestclass 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://www.shaoqun.com/a/715760.html

跨境电商:https://www.ikjzd.com/

史泰博:https://www.ikjzd.com/w/2112

出口易:https://www.ikjzd.com/w/1317


服务注册引入相关依赖:<?配置文件application.yml:spring:cloud:zookeeper:#Zookeeper服务器地址,集群则以逗号分隔connect-string:localhost:2181application:name:zkdemo#使用随机端口server:port:0主程序类ZkdemoApplication:@SpringBootApplicationp
递四方:https://www.ikjzd.com/w/1066
菜鸟网络:https://www.ikjzd.com/w/1547
beien:https://www.ikjzd.com/w/1336
口述:受不了 嘿咻时老公总让我扮妓女:http://lady.shaoqun.com/m/a/16891.html
AMZ移除差评的方法(feedback):https://www.ikjzd.com/tl/5203
高度预警!近期与伊朗国家船运公司业务合作或将受影响!:https://www.ikjzd.com/home/99669

没有评论:

发表评论