Dubbo is integrated and used with Zookeeper and SpringMVC (load balancing and fault tolerance) and dubbozookeeper

Source: Internet
Author: User

Dubbo is integrated and used with Zookeeper and SpringMVC (load balancing and fault tolerance) and dubbozookeeper

With the development of the Internet, the scale of website applications is constantly expanding, and conventional vertical application architectures cannot cope with this problem. distributed service architectures and mobile computing architectures are imperative. Dubbo is a distributed service framework, it was born in this situation. Currently, core services are extracted as independent services, allowing front-end applications to respond more quickly and stably.


1. Background of Dubbo




Before large-scale servitization, applications may simply expose and reference remote services using tools such as RMI or Hessian, call services by configuring the service URL address, and load balancing by using hardware such as F5.

(1) As there are more and more services, it is very difficult to manage Service URL configurations, and the single point of pressure on F5 hardware Server Load balancer is also increasing.

In this case, a service registration center is required to dynamically register and discover services so that the service location is transparent.

The service provider address list is obtained from the consumer to achieve soft load balancing and Failover, reducing the dependency on F5 hardware Load balancer and reducing part of the cost.

(2) As the dependency between services becomes more complex, architects cannot fully describe the architecture relationship of an application until the application is started.

In this case, you need to automatically draw dependency relationships between applications to help architects clean up relationships.

(3) then, as the number of calls to the service increases, the capacity of the Service is exposed. How many machines does the service need to support? When should I add machines?

To solve these problems, the first step is to collect statistics on the number of calls and response times of the service on a daily basis as a reference indicator for capacity planning.

Second, you need to dynamically adjust the weight. Online, the weight of a machine is continuously increased, and the change in response time is recorded during the process of increasing until the response time reaches the threshold value, record the traffic at this time, and multiply the traffic by the number of machines to reverse the total capacity.


2. Dubbo Introduction

Dubbo is a distributed service framework that solves the problems mentioned above. Dubbo architecture:


Node role description:

Provider: exposes the service Provider of the service.

Consumer: the Consumer who calls the remote service.

Registry: The Registration Center for service registration and discovery.

Monitor: the monitoring center that counts the call times and call times of a service.

Container: a service running Container.


Call relationship description:

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

1. When the service provider starts, it registers its own services with the Registration Center.

2. When a service consumer starts, the service consumer subscribes to the Registry to subscribe to the desired service.

3. The Registration Center returns the service provider address list to the consumer. If there is any change, the Registration Center will push the change data to the Consumer based on the persistent connection.

4. The service consumer selects one provider for calling Based on the soft load balancing algorithm from the provider address list. If the call fails, selects another call.

5. service consumers and providers, accumulate the number of calls and call time in the memory, and regularly send statistics once per minute to the monitoring center.



Dubbo provides many protocols, including Dubbo protocol, RMI protocol, and Hessian protocol. We can view the source code of Dubbo and implement various protocols ,:


Before Dubbo was used, Hessian was used to expose and call our services, and HessianProxyFactory was used to call remote interfaces.


The above is a reference to Dubbo official network introduction, next we will introduce SpringMVC, Dubbo, Zookeeper integrated use.


Third, Dubbo is integrated with Zookeeper and SpringMVC.

Step 1: Install Zookeeper on Linux

Zookeeper is the Registration Center of Dubbo services. Dubbo was originally a database-based registration center and did not use Zookeeper. Zookeeper, a distributed service framework, is a tree-type directory service data storage, the data can be managed by clusters. Dubbo can be used as the Registration Center for Dubbo services. Dubbo can deploy clusters with Zookeeper. When the provider suffers an abnormal shutdown, such as power failure, the Zookeeper registration center can automatically delete provider information. When the provider restarts, It can automatically restore registration data and subscribe to requests. We first install Zookeeper on linux. We have the simplest single point of installation, which is troublesome for clusters.

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

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

# Tar zxvf zookeeper-3.4.6.tar.gz

(3) then there is a file zoo_sample.cfg in the corresponding zookeeper-3.4.6/conf file, which configures some information such as the listening client connection port, Zookeeper will find zoo at startup. cfg is used as the default configuration file, so we copy a file named zoo. cfg file ,:

Let's take a look at some configuration information in this file ,:

Note:

ClientPort: the port that listens to the client connection.

TickTime: basic event unit, in milliseconds. It is used to control the heartbeat and timeout. By default, the Minimum Session Timeout is twice the tickTime.

We can perform advanced configuration and cluster configuration on the port of the configuration file, for example, maxClientCnxns: Limit the number of clients connected to ZooKeeper.

(4) Start the Zookeeper service ,:

Zookeeper installation and configuration are complete.

Step 2: configure the dubbo-admin Management page to facilitate our management page

(1) download the dubbo-admin-2.4.1.war package, tomcat deployment in Linux, first put the dubbo-admin-2.4.1 in tomcat webapps/ROOT, and then unzip:

# Jar-xvf dubbo-admin-2.4.1.war

(2) then to webapps/ROOT/WEB-INF, there is a dubbo. properties file, which points to Zookeeper, using the Zookeeper registration center ,:

(3) Start the tomcat service with the username and password: root, and access the service. The logon page is displayed, indicating that dubbo-admin has been deployed successfully ,:

 Step 3: SpringMVC and Dubbo integration. Maven management projects used here

First:We first develop the service registration, which is to provide the service, project structure:

(1) A service interface is added to the test-maven-api project. The Code is as follows:

public interface TestRegistryService {   public String hello(String name);}
(2) Add the jar packages of Dubbo and Zookeeper to pom. xml on the test-maven-console and reference the jar packages of test-maven-api. The Code is as follows:

      <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:

  @Service("testRegistryService")public class TestRegistryServiceImpl implements TestRegistryService {public String hello(String name) {return "hello"+name;}}

(4) If our service is well implemented, we need to expose the service. The Code is as follows:
<? 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.sp Ringframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://code.alibabatech.com/schema/dubbo <span style = "color: #990000;"> http://code.alibabatech.com/schema /Dubbo. xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "default-lazy-init =" false "> <! -- Provider application name information, which is equivalent to a name. The dubbo Management page clearly shows which application is exposed --> <dubbo: application name = "dubbo_provider"> </dubbo: application> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 127.0.0.1: 2181 "check =" false "subscribe =" false "register ="> </dubbo: registry> <! -- Service interface to be exposed --> <dubbo: service interface = "cn. test. dubbo. registry. service. TestRegistryService" ref = "testRegistryService"/> </beans>

Note:

Dubbo: descriptions of some attributes of the registry label:

1) Whether register registers the service with this registration center. If it is set to false, it only subscribes and does not register.

2) check whether an error is returned when the registration center does not exist.

3) Whether subscribe subscribes to the service from this registration center. If it is set to false, it only registers and does not subscribe.

4) timeout Registration Center request timeout (MS ).

5) address can be configured in the Zookeeper cluster, and multiple addresses can be separated by commas.

Dubbo: service tag attributes:

1) interface service interface path

2) ref references the Bean ID of the corresponding implementation class

3) registry registers with the specified registry and is used in multiple registries. The value is the id attribute of <dubbo: registry>. multiple registries are separated by commas, if you do not want to register the service to any registry, you can set the value to N/.

4) The default value of register is true, indicating whether the service of this protocol is registered with the registration center.


(5) start the project, and then we will display the exposed services on the Dubbo Management page, but there are no consumers, because we have not yet implemented the consumer service ,:

Second, we are developing a service consumer, that is, calling the service. We are creating a new consumer project structure:

(1) test-maven-server-console pom. xml introduces the jar packages of Dubbo and Zookeeper and the jar packages of test-maven-api. Because the jar package of test-maven-api is introduced, we call the package in the project as if it were called locally. The Code is as follows:

      <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 specific implementation of the test-maven-server-console project is as follows:

@Controllerpublic class IndexController {@Autowiredprivate TestRegistryService testRegistryService;@RequestMapping("/hello")public String index(Model model){     String name=testRegistryService.hello("zz");     System.out.println("xx=="+name);return "";}}

(3) the address to be referenced. The Code is as follows:
<? 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> xmlns: context =" http://www.springfra Mework.org/schema/context "xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://code.alib <span style = "color: #990000;"> Abatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context </span> http://www.springframework.org/schema/context/spring-context-3.1.xsd "default-lazy-init =" false "> <dubbo: application name =" dubbo_consumer "> </dubbo: application> <! -- Use the zookeeper registration center to expose the service address --> <dubbo: registry address = "zookeeper: // 192.168.74.129: 2181" check = "false"> </dubbo: registry> <! -- The service to be referenced --> <dubbo: reference interface = "cn. test. dubbo. registry. service. testRegistryService "id =" testRegistryService "> </dubbo: reference> </beans>

Note:

Dubbo: descriptions of some attributes of reference:

1) Service interface called by the interface

2) check whether the provider exists at startup. If the value is true, an error is returned. If the value is false, the provider is ignored.

3) registry obtains the service list from the specified registry registration center. It is used in multiple registration centers and the value is the id attribute of <dubbo: registry>. Multiple registration center IDs are separated by commas (,).

4) loadbalance load balancing policy. Optional values: random, roundrobin, and leastactive, indicating random, round robin, and least active calls.

(4) when the project is started, the Dubbo Management page shows the consumer ,:

 

(5) then access the consumer project. The Controller layer can call the specific implementation of the service just as locally ,:



Dubbo provides a variety of Fault Tolerance solutions, including load balancing ,:

 

 


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.