Dubbo and Zookeeper, SPRINGMVC integration and use (load balancing, fault tolerance)

Source: Internet
Author: User
Tags zookeeper

The development of the Internet, the scale of the application of the website, the conventional vertical application architecture is unable to deal with, the distributed service architecture and the mobile computing architecture is imperative, Dubbo is a distributed service framework, which is born in this case. Now the core business is extracted, as an independent service, so that the front-end application can be more rapid and stable response.

First: Introduction Dubbo background

Before a large-scale service, the application may simply expose and reference remote services through tools such as RMI or Hessian, call through the URL address of the configuration service, and load balance through hardware such as F5.

(1) With more and more services, service URL configuration management becomes very difficult, and the F5 hardware load Balancer's single point of pressure is getting bigger.

At this point, a service registry, dynamic registration and discovery services are required to make the location of the service transparent.

It also reduces the reliance on the F5 hardware load balancer by obtaining a list of service provider addresses in the consumer, which can reduce the partial cost.

(2) When further development, inter-service dependencies become a complex, even unclear which application to start before which application, the architect can not fully describe the application's architectural relationship.

At this point, you need to automatically draw a dependency graph between applications to help the architect clean up the relationship.

(3) Then, the service is more and more calls, the capacity of the service problem is exposed, how much machine support this service needs? When should I add a machine?

To solve these problems, the first step is to count the number of calls and response times for the service now, as a reference for capacity planning.

Secondly, to be able to dynamically adjust the weight, on-line, the weight of a machine has been increased, and in the process of increasing the time to record the change in response times, until the response time to reach the threshold, record the amount of traffic at this time, and then the amount of this traffic multiplied by the number of machines to push back total capacity.

Second: Introduction to Dubbo

Dubbo is a distributed service framework that solves the problems faced above, Dubbo architecture:


Node role Description:

Provider: The service provider that exposes the service.

Consumer: Invokes the service consumer of the remote service.

Registry: Registration Center for service Registration and discovery.

Monitor: The monitoring center of the call times of the statistics service and the call time.

Container: The service runs the container.

Call Relationship Description:

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

1. Upon launch, the service provider registers its services with the registry.

2. When the service consumer starts, it subscribes to the registration center for the services it needs.

3. The registry returns the service provider address list to the consumer, and if there is a change, the registry will push the change data to the consumer based on a long connection.

4. Service consumers, from the provider address list, based on the soft load equalization algorithm, select a provider to make the call, if the call fails, then choose another call.

5. Service consumers and providers, the cumulative number of calls in memory and call time, timed to send statistics every minute to the monitoring center.

Dubbo provides a lot of protocols, Dubbo Protocol, RMI Protocol, Hessian protocol, we look at Dubbo source code, there are various protocols implemented:


Before we used Dubbo, most used Hessian to use the exposure and invocation of our services, using Hessianproxyfactory to invoke the remote interface.


The above is a reference to the Dubbo official network introduction, Next we introduce SPRINGMVC, Dubbo, zookeeper integrated use.

Third: Dubbo and Zookeeper, SPRINGMVC integrated use

First step: Install Zookeeper on Linux

Zookeeper as the Dubbo Service Registration Center, Dubbo The original database-based registry, did not use Zookeeper,zookeeper a distributed service framework, is a tree-based directory service data storage, can achieve cluster management data, Here can be very good as Dubbo Service Registration Center, Dubbo can do with zookeeper cluster deployment, when the provider has a power outage, such as abnormal downtime, zookeeper registry can automatically delete the provider information, when the provider restarts, can automatically recover the registration data, as well as the subscription request. We first install the Zookeeper on Linux, we install the simplest single point, the cluster is more troublesome.

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

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

#tar ZXVF zookeeper-3.4.6.tar.gz

(3) Then there is a file under the corresponding zookeeper-3.4.6/conf zoo_sample.cfg this file is configured to listen to the port of the client connection and other information, Zookeeper at the start will find zoo.cfg this file as the default configuration file, So we copy a file called Zoo.cfg, which:

Let's look at some of the configuration information in this file:

Description

ClientPort: Listens for the port of the client connection.

Ticktime: The base event unit, in milliseconds. It is used to control the heartbeat and timeout, by default the minimum session time-out is twice times 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, etc.

(4) Start the Zookeeper service,:

To this side. Zookeeper Installation and configuration complete

Step Two: Configure the Dubbo-admin Management page to facilitate our management of the page

(1) Download the Dubbo-admin-2.4.1.war package, in the Linux Tomcat deployment, first put dubbo-admin-2.4.1 under Tomcat Webapps/root, and then extract:

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

(3) then start the Tomcat service, username and password: root, and access the service, display the landing page, indicating the success of the Dubbo-admin deployment:

Step Three: Integration of SPRINGMVC and Dubbo, the MAVEN management project used here

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

(1) The TEST-MAVEN-API project joins a service interface with the following code:

Public interface Testregistryservice {public   string Hello (string name);}
(2) Test-maven-console in Pom.xml to add Dubbo and zookeeper jar package, reference Test-maven-api jar package, 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 implementation of the specific services, the code is as follows:

  @Service ("Testregistryservice") public class Testregistryserviceimpl implements Testregistryservice {public String Hello (String name) {return "Hello" +name;}}

(4) We serve and implement well, at this time 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.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<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 app name information, this is equivalent to a name, we Dubbo Management page is more clear which app is exposed--<dubbo: Application Name= "Dubbo_provider" ></dubbo:application> <!--using zookeeper registry to expose the service address--<dubbo:regi  Stry address= "zookeeper://127.0.0.1:2181" check= "false" Subscribe= "false" register= "" ></dubbo:registry> <!--to expose the service interface--<dubbo:service interface= "Cn.test.dubbo.registry.service.TestRegistryService" ref= " Testregistryservice "/> </beans>

Description

Dubbo:registry label Some of the properties of the description:

1) Register is registered to this registry service, if set to False, will only subscribe, not registered.

2) If the Check Registration center does not exist, whether to error.

3) Subscribe whether to subscribe to this registry service, if set to False, will only register, do not subscribe.

4) Timeout Registration center request time-out (ms).

5) address can be zookeeper cluster configuration, multiple addresses can be separated by commas and so on.

Some property descriptions of the Dubbo:service tags:

1) path to the interface service interface

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

3) Registry registered with the designated registry, used at multiple registries, with a value of <dubbo:registry> ID attribute, multiple registry IDs separated by commas, and if you do not want to register the service with any registry, set the value to N/a

4) Register By default True, whether the service of the Protocol is registered to the registry.

(5) Start the project and then we show on the Dubbo Management page that the service has been exposed, but the display is not yet consumer, as we have not yet implemented consumer services:

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

(1) Test-maven-server-console Pom.xml introduces Dubbo and zookeeper jar packages, Test-maven-api jar packages, because the Test-maven-api jar package is introduced, We call 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, the code 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 we are referring to, 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=" "><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.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<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> <!--using zookeeper registry 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 Interfa Ce= "Cn.test.dubbo.registry.service.TestRegistryService" id= "Testregistryservice" ></dubbo:reference> </beans>

Description

Descriptions of some of the properties of Dubbo:reference:

1) interface invocation of the service interface

2) Check startup checks if provider is present, true error, false ignore

3) Registry Register for a list of services from a designated registry, use at multiple registries, value <dubbo:registry> ID attribute, multiple registry IDs separated by commas

4) LoadBalance Load Balancing strategy, selectable values: random,roundrobin,leastactive, respectively: Random, round robin, least active call

(4) Project launch, Dubbo Management page, can see the consumer,:

(5) then accessing the consumer project, the controller layer can invoke the specific implementation of the service as if it were local:

Dubbo offers a variety of fault tolerant scenarios, including load balancing:






Original link: http://blog.csdn.net/congcong68/article/details/41113239

Dubbo and Zookeeper, SPRINGMVC integration and use (load balancing, fault tolerance)

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.