Java Cluster optimization--dubbo+zookeeper building highly available distributed cluster __java

Source: Internet
Author: User
Tags zookeeper java web server port
Not long ago, we discussed the cluster of Nginx+tomcat, which is already very flexible cluster technology, but when our system encountered a larger bottleneck, the full application of the single point server can not meet our needs, at this time, we have to consider another, we are familiar with the content is distributed, And now the popular Dubbo framework, we can not ignore, here, let's explore the use of this framework.


One, background


We used to call other people's interfaces remotely, and that's what we did:


The problems we encounter:

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

What did Dubbo do for us to solve these problems:




Load Balancing:


This is known as soft load balancing.



Now let's get in touch with this excellent framework:

Brief introduction

Architecture diagram:


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.

You need to install the JDK first, download from Oracle's Java Web site, and install it very simply, no longer detailed.

Stand-alone mode

A stand-alone installation is very simple, as long as you get the zookeeper package and extract to a directory such as: C:/ZOOKEEPER-3.4.5/, zookeeper startup script under the bin directory, Windows boot script is zkserver.cmd.

Before you execute the startup script, there are a few basic configuration items to configure, zookeeper configuration files in the Conf directory, this directory has zoo_sample.cfg and log4j.properties, you need to do is to Zoo_ SAMPLE.CFG is renamed Zoo.cfg because zookeeper will find this file as the default profile at startup. The following is a detailed description of the meaning of each configuration item in this configuration file.


 <span style= "FONT-SIZE:18PX;" ># the number of milliseconds of each tick ticktime=2000 # The number of ticks this initial # synchronization PHAs E can take initlimit=10 # The number of ticks that can pass between # Sending a request and getting a acknowledgement sy Nclimit=5 # The directory where the snapshot is stored. # do not use/tmp for storage,/tmp here is just # example sakes. Datadir=c://zookeeper-3.4.5//data Datalogdir=c://zookeeper-3.4.5//log # The port at which the clients'll connect client port=2181 # is sure to read the maintenance section of the ' # Administrator guide before turning on Autopurge. # # Http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # The number of snapshots to retain in da Tadir #autopurge. snapretaincount=3 # Purge Task interval in hours # Set to ' 0 ' to disable Auto Purge feature #autopurge. Pu Rgeinterval=1</span> 
Ticktime: This time is as zookeeper between the server or between the client and the server to maintain the heartbeat time interval, that is, each ticktime time will send a heartbeat. DataDir: As the name implies is zookeeper save the data directory, by default, zookeeper will write the data log file also saved in this directory. Datalogdir: As the name implies is zookeeper save log file directory ClientPort: This port is the client connection Zookeeper server port, zookeeper will listen to this port, accept the client's access request.

When these configuration items are configured, you can start zookeeper now, check if zookeeper is already in service after startup, and you can check if there is a ClientPort port number you have configured in the monitor service through the Netstat–ano command.


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

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

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

<span style= "FONT-SIZE:18PX;" >dubbo.registry.address=zookeeper://127.0.0.1:2181 Dubbo.admin.root.password=root dubbo.admin.guest.password= Guest</span>


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

Public interface Testregistryservice {public    string Hello (string name);}

(2) Test-maven-console in Pom.xml Add Dubbo and Zookeeper jar packages, reference Test-maven-api jar packages, the code is as follows:

<span style= "FONT-SIZE:18PX;" ><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></span>


(3) Test-maven-console implementation of specific services, the code is as follows:

@Service ("Testregistryservice") public class Testregistryserviceimpl implements Testregistryservice {public  string Hello (string name) {return    &quo
Related Article

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.