服務注冊
引入相關依賴:
<?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進行呼叫,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281947.html
標籤:其他
上一篇:Python 條件控制
