One, foreword

Generally speaking, the evolution process of a company’s business system is basically from single application to multiple applications. In monomer applications, different business module calling each other within the local JVM process can be completed directly, and become a multiple applications, the way to communicate with each other is not easy for local calls, because of the different business modules deployed to the JVM process, more often deployed to a different machine, At this time, an efficient and stable RPC remote call framework becomes very important.

Dubbo is an open source high-performance remote service invocation framework developed by Alibaba, which is committed to providing high-performance and transparent RPC remote call service solutions. As the core framework of Alibaba’s SOA servitization governance scheme, it has entered the Apache ovator project, and its prospects are infinitely bright.

2. Build a service registry using ZooKeeper

2.1 Basic Principles of ZooKeeper

Zookeeper is a subproject of Apacahe Hadoop. It is a tree directory service that supports change push. It is suitable for Dubbo service registry.

The following shows the application of Zookeeper in service registration and discovery.

  • In the preceding figure, the Root of the zK is Dubbo, indicating that the zK group is dubbo. The second layer of the tree is Service layer, which represents the specific interface Service. In this case, com.test.UserServiceBo interface Service. The third layer of the tree is the Type layer, which is used to distinguish service providers, service consumers, and routing rules. The fourth layer of the tree URl is the machine list or specific routing rules corresponding to specific service providers or consumers.

  • Assuming that the service provider provides the service of the implementation class of com.test.UserServiceBo externally, when the service provider starts, The Provider will pass zkclient in zk server/dubbo/com. Test. UserServiceBo/will write your own URL directory, if there are multiple service providers, then will have more than one address, It is important to note that the tree directory is not necessarily a binary tree. If the provider has 100 machines, providers will have 100 nodes.

  • Assuming that a service consumer needs a service that uses the com.test.UserServiceBo interface, when the service consumer starts, Consumer will pass zkclient subscription zk server/dubbo/com. Test. UserServiceBo/will directory services provider URL address list. And in dubbo/com. Test. UserServiceBo/consumers directory to write down their url address, here is to record the service consumer address to when service providers address list changes (such as a new machine, reducing the machine) when zk can notify to subscribe to the service subscriber.

  • The monitoring center and the management console at boot time will pass zkclient subscription zk server/dubbo/com. Test. UserServiceBo/directory content, and through the will and consumers subdirectory to distinguish which is a list of service providers, which is a service consumer list, Other management console can also write routing rules to dubbo/com. The test. The UserServiceBo/routers, the routing rules will also be pushed to the service consumer side.

When the service provider in the cluster has a machine hang up after, zk can timely found in a long chain disconnect the machine hang up, and from the zk registry service provider will delete the directory of the machine, and then will inform service consumers update available address list of service providers Because of zk support persistent storage when zk restart, Service registration information can be automatically restored.

2.2 Installing ZooKeeper

This article explains how to set up ZooKeeper when using Apache ZooKeeper as a service registry.

  • First you need to go toZookeeper.apache.org/releases.ht…To download a zK package, the author of this article uses the zookeeper-3.4.11 version, as shown below:



    After decompressing the package, the picture is as follows:

  • CFG file in the zookeeper-3.4.11/conf folder.

    Set the configuration item dataDir to an existing directory ending in data.

    Set zK listening port to clientPort=2181;

    Set zK heartbeat check interval tickTime to 2000.

    Set the number of heartbeat intervals that the Follower server can tolerate after data is synchronized from the Leader when the Follower server starts. InitLimit =5.

    Set the timeout syncLimit=2 for the Follower to reply to the Leader after the Leader synchronizes data to the Follower. If the Leader exceeds the syncLimit tickTime, If no response has been received from a Follower, the Follower is considered to be offline:

  • Sh zkserver. sh start-foreground sh zkserver. sh start-foreground sh zkserver. sh start-foreground

Zk listens on port 2181. The service registry is set up.

2.3 Using ZooKeeper

Service providers and callers need to import the JAR package of ZKClient to access the ZK server. They need to add the following dependencies in the POM,

<dependency> <groupId>com.101tec</groupId> <artifactId> zkClient </artifactId> <version>0.10</version> </dependency>Copy the code

Zookeeper single machine configuration: specify a zk IP as a service registry < dubbo: registry address = “Zookeeper: / / 12.22.123.101:2181″ / > or < dubbo: registry Protocol =”zookeeper” address=”12.22.123.101:2181″ /> ZooKeeper specifies the zooKeeper service registry. 12.22.123.101:2181 is the ZK server address and service listening port number.

Zookeeper cluster configuration: specify multiple IP as a service registry < 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 divide multiple groups on the same ZK server, such as the following

` ` `

As shown in the code above, there are two groups on the same ZK server, that is, there are two tree directories with the roots of registry1 and registry2.

Three or more

For more information: gitbook.cn/gitchat/col…