Integration and use of Dubbo with Zookeeper and Spring MVC (load balancing, fault tolerance)

Keywords: Java Dubbo Zookeeper Spring Maven

With the development of Internet, the scale of website application is expanding, and the conventional vertical application architecture is unable to cope with it. Distributed service architecture and mobile computing architecture are imperative. Dubbo is a distributed service framework, which was born in this situation. Now the core business is extracted as an independent service, so that the front-end application can respond more quickly and steadily.


First: Introduce the background of Dubbo

   


Before large-scale service, applications may simply expose and refer to remote services through tools such as RMI or Hessian, invoke by configuring the URL address of services, and load balancing by hardware such as F5.

(1) When more and more services are available, service URL configuration management becomes very difficult, and the single-point pressure of F5 hardware load balancer is also increasing.

A service registry is needed to dynamically register and discover services to make the location of services transparent.

By acquiring the address list of service providers in the consumer side, the software load balancing and Failover can be realized, and the dependence on F5 hardware load balancer can be reduced, as well as part of the cost.

(2) With further development, the dependencies between services become so complicated that it is impossible for architects to fully describe the application's architecture relationship without knowing which application to start before which one.

At this point, dependency diagrams between applications need to be drawn automatically to help architects clean up relationships.

(3) Then, with the increasing number of service calls, the problem of service capacity is exposed. How much machine support does this service need? When should I add the machine?

In order to solve these problems, the first step is to count the daily service allocation and response time as a reference index for capacity planning.

Secondly, it is necessary to dynamically adjust the weight of a machine. On-line, the weight of a machine is always increased, and the change of response time is recorded in the process of increasing, until the response time reaches the threshold, the amount of visits at this time is recorded, and then the total capacity is deduced by multiplying the number of machines with the amount of visits.


Second: Introduction to Dubbo

Dubbo is a distributed service framework, which solves the above problems. The architecture of Dubbo is shown in the figure.


Node role description:

Provider: A service provider that exposes services.

Consumer: A service consumer who calls a remote service.

Registry: Registry for service registration and discovery.

Monitor: The Monitoring Center for calling times and calling times of statistical services.

Container Service Running Container.


Call relationship description:

0. The service container is responsible for starting, loading and running the service provider.

1. At startup, the service provider registers its services with the registry.

2. When service consumers start up, they subscribe to the registry for the services they need.

3. The registry returns a list of service provider addresses to consumers. If there is a change, the registry will push the change data to consumers based on long connections.

4. Service consumers, from the provider address list, based on soft load balancing algorithm Select one provider to make the call, and if the call fails, select another.

5. Service consumers and providers, accumulating call times and call times in memory, regularly send statistical data to the monitoring center every minute.



Dubbo provides many protocols, such as Dubbo protocol, RMI protocol and Hessian protocol. We look at the source code of Dubbo, and we have the implementation of various protocols, as shown in the figure.


When we didn't use Dubbo before, most of us used Hessian to expose and invoke our services, and Hessian ProxyFactory to invoke remote interfaces.


Above is a reference to the official Dubbo website. Next, we will introduce the integrated use of Spring MVC, Dubbo and Zookeeper.


Third: Dubbo is integrated with Zookeeper and Spring MVC

  

Step 1: Linux Install Zookeeper on

 

Zookeeper serves as a registry for Dubbo services, which were originally based on data base The registration center does not adopt Zookeeper, Zookeeper is a distributed service framework. It is the data storage of tree-type directory service and can manage data in cluster. It can be used as the registry of Dubbo service very well. Dubbo can deploy cluster with Zookeeper. When the providers have abnormal downtime such as power failure, Zookeeper registry can automatically delete providers'information. When the provider restarts, registration data and subscription requests can be automatically restored. Let's install Zookeeper on linux first. We install the simplest single point and cluster is more troublesome.

(1) Download Zookeeper-3.4.6.tar.gz address http://www.apache.org/dist/zookeeper/

(2) We put it in a folder under Linux and decompress it:

 

      #tar zxvf zookeeper-3.4.6.tar.gz

(3) Then, under the corresponding zookeeper-3.4.6/conf, there is a file zoo_sample.cfg which configures some information such as the port to listen to the client connection. Zookeeper will find zoo.cfg as the default configuration file when it starts, so we copy a file named zoo.cfg.

As shown in the figure:

    

Let's look at some configuration information in this file, as shown in the figure.

   

  

Explain:

clientPort: The port that listens for client connections.

TickTime: Basic event unit in milliseconds. It is used to control heartbeat and timeout, and by default the smallest session timeout is twice as long as tickTime.

We can configure the ports of configuration files or other advanced configurations and cluster configurations such as maxClientCnxns: limit the number of clients connected to ZooKeeper, etc.

(4) Start the Zookeeper service, as shown in the figure:

   

To complete the installation and configuration of Zookeeper

Step 2: Configure the management page of dubbo-admin to make it easier for us to manage the page.

(1) Download the dubbo-admin-2.4.1.war package and deploy the dubbo-admin-2.4.1 in Tomcat on Linux. First put dubbo-admin-2.4.1 under tomcat's webapps/ROOT, and then decompress:

        #jar -xvf dubbo-admin-2.4.1.war

(2) Then, under webapps/ROOT/WEB-INF, there is a dubbo.properties file pointing to Zookeeper, using the registration center of Zookeeper, as shown in the figure:

       

(3) Then start the tomcat service, username and password: root, and access the service, display the landing page, indicating the successful deployment of dubbo-admin, as shown in the figure:

     

Step 3: Integration of Spring MVC and Dubbo, the Maven management project used here

First: We first develop service registration, which is to provide services. The project structure is shown in the figure.

        

(1) The test-maven-api project adds a service interface with the following code:

[java] view plain copy
    public interface TestRegistryService {  
       public String hello(String name);  
    }  

 

(2) test-maven-console adds the jar packages of Dubbo and Zookeeper in pom.xml and refers to the jar packages of test-maven-api. The code is as follows:

  

[java] view plain copy
 <dependency>  
    <groupId>cn.test</groupId>  
    <artifactId>test-maven-api</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
</dependency>  
  
   <dependency>  
         <groupId>com.alibaba</groupId>  
         <artifactId>dubbo</artifactId>  
         <version>2.5.3</version>  
     </dependency>  
       
      <dependency>  
         <groupId>org.apache.zookeeper</groupId>  
<artifactId>zookeeper</artifactId>  
<version>3.4.6</version>  
     </dependency>  
  
   <dependency>  
     <groupId>com.github.sgroschupf</groupId>  
<artifactId>zkclient</artifactId>  
<version>0.1</version>  
   </dependency>  

 

(3)test-maven-console implements specific services, the code is as follows:

[java] view plain copy
@Service("testRegistryService")  
public class TestRegistryServiceImpl implements TestRegistryService {  
public String hello(String name) {    
    return "hello"+name;  
}  
}

 

 


(4) Our services and implementations are completed. At this time, we need to expose the services. The code is as follows:
  

[java] view plain copy
    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        <span style="color:#cc0000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span>  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
        <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
        default-lazy-init="false" >  
       <!-- Provider applies name information, which corresponds to a name, we dubbo The management page is clearer which application is exposed -->  
       <dubbo:application name="dubbo_provider"></dubbo:application>  
       <!-- Use zookeeper Registry Exposure Service Address -->    
       <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>  
      <!-- Service interfaces to be exposed -->    
      <dubbo:service interface="cn.test.dubbo.registry.service.TestRegistryService" ref="testRegistryService" />        
    </beans>  

 

  

Explain:

Description of some attributes of the dubbo:registry tag:

1) Whether the register registers the service with this registry, if set to false, it will only subscribe and not register.

2) Whether an error is reported when the check registry does not exist.

3) Whether subscribe subscribes to this registry or not, if set to false, it will only register and not subscribe.

4) timeout registry request timeout time (milliseconds).

5) address can be configured in Zookeeper cluster, addresses can be separated by commas, etc.

Some attributes of the dubbo:service tag are described:

1) Path of interface service interface

2) ref refers to the ID of the Bean of the corresponding implementation class

(3) Registry registers with a designated registry and uses the ID attribute of <dubbo:registry> in multiple registries. Multiple registry IDs are separated by commas. If you do not want to register the service with any registry, you can set the value to N/A.

(4) register defaults to true, and whether the services of the protocol are registered in the registry.


(5) Start the project, and then we display the exposed services on the Dubbo management page, but show that there are no consumers, because we have not implemented consumer services, as shown in the figure:

 

Second: We are developing service consumers, that is, invoking services. We are building a new consumer project structure as shown in the figure.

      

(1) The pom.xml of test-maven-server-console introduces the jar packages of Dubbo and Zookeeper, and the jar packages of test-maven-api, because the jar packages of test-maven-api are introduced, we call them in the project as if they were called locally. The code is as follows:

      

[java] view plain copy
<dependency>  
    <groupId>cn.test</groupId>  
    <artifactId>test-maven-api</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
</dependency>  
  
   <dependency>  
         <groupId>com.alibaba</groupId>  
         <artifactId>dubbo</artifactId>  
         <version>2.5.3</version>  
     </dependency>  
       
      <dependency>  
         <groupId>org.apache.zookeeper</groupId>  
<artifactId>zookeeper</artifactId>  
<version>3.4.6</version>  
     </dependency>  
  
   <dependency>  
     <groupId>com.github.sgroschupf</groupId>  
<artifactId>zkclient</artifactId>  
<version>0.1</version>  
   </dependency>  

 

(2) The concrete implementation of the test-maven-server-console project is as follows:

     

[java] view plain copy
    @Controller  
    public class IndexController {  
          
        @Autowired  
        private TestRegistryService testRegistryService;  
          
        @RequestMapping("/hello")  
        public String index(Model model){  
             String name=testRegistryService.hello("zz");  
             System.out.println("xx=="+name);  
            return "";  
        }  
      
    }  

 


(3) The address we want to refer to is the following code: ___________
   

[java] view plain copy
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    <span style="background-color: rgb(255, 255, 255);"><span style="color:#990000;">xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"</span></span>  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd  
    <span style="color:#990000;">http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span>  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"  
    default-lazy-init="false" >  
  
   <dubbo:application name="dubbo_consumer"></dubbo:application>  
   <!-- Use zookeeper Registry Exposure Service Address -->    
   <dubbo:registry address="zookeeper://192.168.74.129:2181" check="false"></dubbo:registry>   
     <!-- Services to be referenced -->    
   <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService"></dubbo:reference>  
</beans> 

 

Explain:

Description of some attributes of dubbo:reference:

1) Service interface invoked by interface

2) check the existence of the provider at startup, true error, false neglect

3) Registry obtains a list of services from a designated registry registry, which is used in multiple registries with an ID attribute of <dubbo:registry> separated by commas.

4) Load balance load balancing strategy, optional values: random,roundrobin,leastactive, respectively: random, round robin, least active call

   

(4) Project launch, Dubbo management page, you can see consumers, as shown in the figure:

 

(5) Then access the consumer project, and the Controller layer can invoke the specific implementation of the service just like calling the local one, as shown in the figure:

 



Dubbo offers a variety of fault-tolerant solutions, including load balancing, as shown in the figure:

 

 

Posted by jamiefoxx on Mon, 25 Mar 2019 00:06:30 -0700