DUBBO+ZOOKEEPER+SPRINGMVC integration to realize micro-service project

Source: Internet
Author: User
Tags unpack zookeeper

The development of the Internet, the scale of the application of the website expands unceasingly, the conventional vertical application architecture has been unable to deal with, the distributed service architecture and the flow computation architecture is imperative, Dubbo is a distributed service framework, in this case born. Now the core business is extracted, as an independent service, so that front-end applications can be faster and more stable response.

First: Introduce Dubbo background


Before mass service, applications may simply expose and reference remote services via RMI or Hessian tools, and invoke them by configuring the URL address of the service to load balance through hardware such as F5.

(1) When the service is more and more, the service URL configuration management becomes very difficult, the F5 hardware load Balancer's single point pressure is also more and more big.

A service registry, a dynamic registration and discovery service, is required to make the location of the service transparent.

and through the consumer to obtain the service provider address list, realizes the soft load equalization and the failover, reduces to F5 hardware load balancer the dependence, also may reduce the partial cost.

(2) When further development, the dependencies of the service become more complex, and even unclear which application to start before which application, the architect can not fully describe the application of the 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 number of calls to the service more and more, the capacity of the service to expose the problem, the service needs a number of machine support. When to add the machine.

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

Secondly, to be able to dynamically adjust the weight, online, the weight of a machine has been increased, and in the process of increasing the record response time changes, until the response time to reach the threshold, record the amount of traffic at this time, and then multiply the number of this traffic to the total capacity of the machine.


Second: Dubbo's introduction

Dubbo is a distributed service framework that solves the problems that are faced above, and the Dubbo architecture is shown in the figure:

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 time reconciliation call times for statistical services.

Container: Service Run container.


Call Relationship Description:

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

1. The service provider registers its own services with the registry at the time of commencement.

2. When starting, the service consumer subscribes to the registry for the services it needs.

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

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

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



Dubbo provides a number of protocols, Dubbo Protocol, RMI Protocol, Hessian protocol, we look at the Dubbo source code, there are various protocol implementation, as shown in the figure:

We didn't use Dubbo before, most of them used Hessian to use the exposure and invocation of our service, using Hessianproxyfactory to invoke the remote interface.

The above is referred to the Dubbo official network introduction, Next we will introduce Springmvc, Dubbo, zookeeper integration use.


Third: Dubbo and Zookeeper, SPRINGMVC integrated use

The first step: Install Zookeeper on Linux

Zookeeper as the Dubbo Service Registry Center, Dubbo originally based on the Database Registry center, did not adopt the Zookeeper,zookeeper a distributed service framework, is the tree type directory service data storage, can do the cluster management data, Here can be very good as the Dubbo Service Registration Center, Dubbo can do with zookeeper cluster deployment, when the provider of abnormal downtime, such as power outages, zookeeper registry can automatically delete the provider information, when the provider restarts, can automatically restore registration data, as well as subscription requests. 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 in a folder under Linux, and then unpack:

#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 this file configured to listen to the client connection port and other information, zookeeper at startup will find zoo.cfg this file as the default configuration file, So we copy a file called Zoo.cfg, as shown in the figure:

Let's look at some of the configuration information in this file, as shown in the picture:

Description

ClientPort: Listens to the port on which the client is connected.

Ticktime: The base event unit, in milliseconds. It is used to control heartbeat and timeout, by default the minimum session timeout time is twice times ticktime.

We can configure the port of the profile, etc., or perform advanced configuration and cluster configuration such as: Maxclientcnxns: Limit the number of clients connected to zookeeper

(4) Start the Zookeeper service as shown in the picture:

Over here. Zookeeper Installation and configuration complete

The second step: Configure the Dubbo-admin Management page to facilitate our management page

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

#jar-XVF Dubbo-admin-2.4.1.war

(2) then to Webapps/root/web-inf, there is a dubbo.properties file, which points to the zookeeper, using the Zookeeper registry, as shown in the figure:

(3) Then start Tomcat Service, username and password: root, and access service, display landing page, Description Dubbo-admin deployment success, as shown in the figure:

Step three: The integration of SPRINGMVC and Dubbo, the MAVEN management project used here

First: We first develop service registration, is to provide services, the project structure as shown in the picture:

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

[Java] view plain copy public interface Testregistryservice {public string hello (string name); (2) test-maven-console add Dubbo and Zookeeper jar packages to the pom.xml, and reference Test-maven-api jar packages in the following code:

   [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.

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.