A list,
- As you know, ZK is a file system type data storage service. Based on different features of nodes, operations such as service registration and distributed locking can be implemented.
- Services can be registered through temporary nodes of ZK. When a service instance goes offline, the instance disappears automatically. Therefore, this is A CP (strong consistency, partition fault tolerance), discard A (high availability) operation.
- The Eureka registry was introduced in the previous chapter, and ZK operates similarly as a registry. Unfortunately, ZK is only a “database”, there is no way to monitor through the web page, only through the client to see the node information.
- The installation, operation, and cluster layout of Zookeeper are described in this column
A cluster version registry
1. Start the ZooKeeper cluster
- First, you definitely need to enable multiple ZK services. The tutorial
2. Registered
2.1 guide package
- The most important thing is definitely
spring-cloud-starter-zookeeper-discovery
This is the key to the ZooKeeper registration service. - It is worth mentioning that the ZooKeeper version carried by the starter may be inconsistent with the ZooKeeper version of the server, which may cause exceptions during running. You can use the following methods to pass
<exclusion>
Exclude the original ZK, and then add one of your own that matches the ZK server version<dependency>
<dependencies>
<! -- SpringBoot integrate Web components -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- SpringBoot with ZooKeeper -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<! Zookeeper3.5.3 -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.10版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
2.2 Configuration File
- in
connect-string
On, the cluster has as many ZK servers, write as many addresses
#8004 represents the payment service provider port number registered with the ZooKeeper server
server:
port: 8004
# service alias ---- Register ZooKeeper with the registry name
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
connect-string: 192.168233.137.: 2181192168 233.131:2181192168 233.132:2181
Copy the code
2.3 Main boot class
- Through annotation
@EnableDiscoveryClient
It’s like registering in the registry.
@SpringBootApplication
@EnableDiscoveryClient // This annotation is used to register services when using Consul or ZooKeeper as a registry
public class PaymentMain8004 {
public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); }}Copy the code
2.4 Viewing Nodes
- You can see the services currently registered on the ZK client. This node appears to be persistent.
- You can also view instances of the current service. This node is temporary and the instance goes offline and disappears. As you can see, this node holds a lot of instance information.
{
"name":"cloud-payment-service"."id":"8f7df390-13a2-4d39-b5d6-0930dbcaf860"."address":"localhost"."port":8004."sslPort":null."payload": {"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance"."id":"application-1"."name":"cloud-payment-service"."metadata": {}},"registrationTimeUTC":1621597527900."serviceType":"DYNAMIC"."uriSpec": {"parts":[
{
"value":"scheme"."variable":true
},
{
"value":": / /"."variable":false
},
{
"value":"address"."variable":true
},
{
"value":":"."variable":false
},
{
"value":"port"."variable":true}}}]Copy the code