Use Zookeeper to build a service governance center

Zookeeper is a sub-project of Apache Hadoop. It is a tree-shaped directory service that supports change push. It is suitable as a registration center for Dubbo services. It has high industrial strength and is recommended for use in production environments.

The following describes the application of Zookeeper in service registration and discovery in combination with the above figure:

As shown in the figure above, the root Root of the overall Zookeeper is Dubbo, indicating that the established Zookeeper group is Dubbo, and the second layer of the tree is the Service layer used to represent specific interface services, here is the com.test.UserServiceBo interface service, the third layer of the tree The Type layer is used to distinguish whether it is a service provider, a service consumer, or a routing rule. The fourth-level URL of the tree is a list of machines corresponding to a specific service provider or consumer or a specific routing rule.

Assuming that the service provider provides services of the implementation class of com.test.UserServiceBo, when the service provider starts, the provider will write its own URL in the /dubbo/com.test.UserServiceBo/providers directory of the Zookeeper server through ZKClient address. If there are multiple service providers, there will be multiple addresses under providers. It should be noted here that the tree directory here is not necessarily a binary tree. If there are 100 service providers, there will be 100 under providers. node's.

Assuming that the service consumer needs to use the service of the com.test.UserServiceBo interface, then when the service consumer starts, the consumer will subscribe to the URL of the service provider provided in the /dubbo/com.test.UserServiceBo/providers directory of the Zookeeper server through ZKClient List of addresses. And write your own URL address in the /dubbo/com.test.UserServiceBo/consumers directory. The address of the service consumer should be recorded here for when the address list of the service provider changes (such as adding a new machine, reducing the machine) At times, Zookeeper can notify subscribers who subscribe to the service.

The monitoring center and the management console will subscribe to the contents of the /dubbo/com.test.UserServiceBo/ directory of the Zookeeper server through ZKClient at startup, and distinguish which are the service provider list and which are the service consumers through the providers and consumers subdirectories In addition, the management console can also write routing rules to the /dubbo/com.test.UserServiceBo/routers directory, and these routing rules will also be pushed to the service consumer.

When a machine in the service provider cluster hangs, Zookeeper can discover the machine hangs through long chain disconnection in time, and delete the machine from the providers directory of the service provider in the Zookeeper registry, and then notify the service consumer. Update the list of available service provider addresses. Since zookeeper supports persistent storage, service registration information can be automatically restored when zookeeper is restarted.

Installation of Zookeeper

Next, using Apache Zookeeper as the service registry, we must first build Zookeeper.

First, you need to download the installation package from the Apache official website, the address is http://zookeeper.apache.org/releases.html , this article uses zookeeper-3.4.11 version, as shown below:

 

After decompressing the package, it will look like this:

Then modify the zoo.cfg file in the zookeeper-3.4.11/conf folder.

  • Set the configuration item dataDir to an existing directory ending with data;
  • Set Zookeeper's listening port clientPort=2181
  • Set Zookeeper heartbeat check interval tickTime=2000
  • Set the number of heartbeat intervals that can be tolerated when the data is synchronized from the Leader when the Follower server starts. initLimit=5

Set the timeout time syncLimit=2 for the follower to reply to the leader after the leader synchronizes data to the follower during the running process. If the leader exceeds the time length of syncLimit tickTime and has not received the follower response, then the follower is considered to be offline. As shown below:

Finally, running sh zkServer.sh start-foreground under zookeeper-3.4.11/bin will start Zookeeper, and there will be the following output:

It can be seen that Zookeeper is listening on port 2181, and the service registration center has been built.

Use of Zookeeper

The service provider and the caller need to import the jar package of ZKClient to access the Zookeeper server, and the following dependencies need to be added to the pom.

<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
</dependency>

Zookeeper stand-alone configuration: Specify a Zookeeper ip as the service registry.

<dubbo:registry address="zookeeper://12.22.123.101:2181" />

or:

<dubbo:registry protocol="zookeeper" address="12.22.123.101:2181" />

Among them, Zookeeper indicates that Zookeeper is used as the service registry, and 12.22.123.101:2181 is the server address and service listening port number of Zookeeper.

Zookeeper cluster configuration: Specify multiple IPs as service registries:

<dubbo:registry address="zookeeper://11.10.13.10:2181?backup=11.20.153.111:2181,11.30.
133.112:2181" />

or:

<dubbo:registry protocol="zookeeper" address="11.10.13.10:2181,11.20.153.111:2181,11.30.
133.112:2181" /> 

In addition you can also divide multiple groups on the same Zookeeper server, such as the following:

<dubbo:registry id="registry1" protocol="zookeeper" address="10.20.153.10:2181" gr
oup="registry1" />
<dubbo:registry id="registry2" protocol="zookeeper" address="10.20.153.10:2181" gro
up="registry2" />

As shown in the above code, two groups are divided on the same Zookeeper server, that is, there will be two tree directories, and the tree roots are registry1 and registry2 respectively.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027994&siteId=291194637