Dubbo+zookeeper Example

Source: Internet
Author: User

0. The principle Alibaba has several distributed frameworks, mainly: remote invocation (similar to this remote invocation of RMI) (Dubbo, HSF), JMS message Service (Napoli, notify), KV database (tair), etc. This framework/tool/product, when implemented, takes into account the disaster tolerance, scaling, and load balancing, so there is a configuration center (configserver) to solve these problems. Basic principle

In our system, there are often some cross-system calls, such as in a system to invoke a service B system, we may use RMI directly, B system issued an RMI interface service, and then a system to call this interface through RMI, in order to solve the disaster tolerance, expansion, load balancing problems, We may think of a lot of ways, Alibaba this method feels good. This article only says Dubbo, the principle is as follows:
    • Configserver
A real-time heartbeat detection is done between the configuration center and each server/client (because they are all established socket long connections), such as a few seconds of detection. Collect information about the services provided by each server, each client's information, and sort out a list of services such as:
ServiceName Serveraddresslist Clientaddresslist
UserService 192.168.0.1,192.168.0.2,192.168.0.3,192.168.0.4 172.16.0.1,172.16.0.2
Productservice 192.168.0.3,192.168.0.4,192.168.0.5,192.168.0.6 172.16.0.2,172.16.0.3
OrderService 192.168.0.10,192.168.0.12,192.168.0.5,192.168.0.6 172.16.0.3,172.16.0.4
When a server is unavailable, the affected service is updated with the corresponding serveraddresslist, which is to take this server from serveraddresslist(Removed from the address list) and pushes the serveraddresslistAll the clients inside the clientaddresslist to these affected services. such as: 192.168.0.3 hung up, then UserService and Productservice's serveraddresslistThe 192.168.0.3 is removed, and the new list is told to the corresponding client 172.16.0.1,172.16.0.2,172.16.0.3; When a client is hung up, the affected service is updated clientaddresslistConfigserver based on the list of services, you can provide a Web management interface to view the providers and users of managed services. When adding a new server, because it will actively contact Configserver, and Configserver will send this information to the client actively, so when a new server, only need to start the server, and then within a few seconds, The client will use the services it provides.
    • Client
The machine that invokes the service, each client starts, initiates the socket long connection with configserver, and sends the corresponding information such as its own IP to Configserver. When the client uses the service, it obtains the service provider information according to the service name to Configserver (so that Configserver knows which client is currently in use), and when the client obtains the service provider information, it establishes a connection with them. You can invoke the service directly later, when there are multiple service providers, the client will load balance according to certain rules, such as polling, random, weight, etc. Once the client uses a service that has a change in its corresponding service provider (the service provider has new, deleted cases), Configserver will push the latest list of service providers to client,client to reestablish the connection based on the latest list of service providers. The new provider establishes the connection, and the deleted provider discards the connection
    • Server
The machine that really provides the service, each server starts, the initiative establishes the Scoket long connection with the configserver, and sends own IP, provides the service name, the port and so on the information direct sends to Configserver, Configserver collects information about the services provided by each server.   Advantages: 1, as long as the client and server startup, Configserver is good, the service can be called, if the back Configserver hung, that only affect the configserver after the service provider has changed, The client is not aware of this change. 2,client each invocation of the service is not configserver, the client simply establishes a connection to it, gets a list of service providers from it 3, invokes the service-load balancer: When the client invokes the service, the service can be called alternately between multiple service providers according to the rules. 4, service provider-disaster recovery: A server hangs, the client is still able to invoke the service correctly, when the premise is that the service has at least 2 service providers, the client can quickly perceive the service provider changes, and respond accordingly. 5, service Provider-extension: It is easy to add a service provider, and the client will quickly perceive its presence and use it. 1. Development of software, materials
    • jdk1.7.0_79, install and configure the Java development environment.
    • zookeeper-3.4.5:http://download.csdn.net/detail/adam_zs/9470314
    • Tomcat 7.0 can be configured into eclipse or MyEclipse
    • dubbo-admin-2.5.3.war:http://download.csdn.net/detail/adam_zs/9470323
    • apache-maven-3.2.5 configuration into eclipse or myeclipse can be
    • Dubbo Official Document Http://dubbo.io/Home-zh.htm
    • Dubbo installation :https://github.com/alibaba/dubbo/releases pom.xml:http://files.cnblogs.com/files/belen/pom.xml

2. Key Steps Zookeeper Installation Deployment (Zookeeper is a distributed, open source distributed application Coordination Service that is an open source implementation of Google's chubby and an important component of Hadoop and HBase. instance, zookeeper will serve as the registry for the Dubbo service. Also responsible for cluster scheduling.

Why do you use zookeeper?

Zookeeper can provide configuration management, naming services, distributed algorithms, and cluster management functions. Refer to the following article for specific instructions:

Http://zhidao.baidu.com/question/919262980452730419.html?fr=iks&word=zookeeper+dubbo+%B9%D8%CF%B5&ie= Gbk

) Zookeeper Deployment

1, Dubbo relies on the Zookeeper, realizes the distributed configuration of the task and the interactive communication between each service, Zookeeper is stored in the TreeNode type, supports the cluster form deployment and guarantees the final data consistency, About ZK's information online is rich, related concepts are no longer repeated introduction, this article take zookeeper-3.4.6 as an example, please download http://zookeeper.apache.org from the official website.

2, create the Zookeeperlab folder directory, simulate the deployment of 3 Zookeeper server cluster, the directory structure is as follows.

3. Extract the Zookeeper-3.4.6.tar files downloaded from the official website and copy them to the zookeeper-3.4.6 folder of the three zkserver respectively.

4. Create the myID file under the data directory of three zkserver (note that there is no suffix), to identify the ID of each server, save the myID file of a single digit 1,server2 in the Server1\data\myid file save 2, Server3 's myID save 3.

5. Create a zkserver configuration file, create a zoo.cfg in the Zookeeper-3.4.6\conf folder directory, and rename it from the zoo_sample.cfg copy of the example. Because in the same machine simulation cluster deployment, the port number can not be duplicated, the configuration file has been explained in detail, the modified configuration is as follows, where the Server1 port number 2181,server2 port number 2182,server3 port number 2183.

# The number of milliseconds of each tickticktime=2000# the number of ticks, the initial # synchronization phase can T akeinitlimit=10# the number of ticks so can pass between # Sending a request and getting an acknowledgementsynclimit=5# The directory where the snapshot is stored.# does not use/tmp for storage,/tmp here is just # example Sakes.datadir=e:/zoo keeperlab/server1/datadatalogdir=e:/zookeeperlab/server1/logs# the port at which the clients would connectclientport= 2181# the maximum number of the client connections.# increase this if you need to handle more clients#maxclientcnxns=60## be s Ure to read the maintenance sections of the the # Administrator guide before turning on autopurge.## http://zookeeper.apache.or g/doc/current/zookeeperadmin.html#sc_maintenance## the number of snapshots to retain in datadir# autopurge.snapretaincount=3# Purge task interval in hours# Set to ' 0 ' to disable auto Purge Feature#autopurge.purgeinterva L=1server.1=127.0.0.1:2888:3888server.2=127.0.0.1:2889:3889server.3=127.0.0.1:2890:3890 

6, through the zookeeper-3.4.6\ The Bin folder zkserver.bat file starts Zkserver, because cluster deployment needs to elect leader and followers, so a zkserver will be prompted before all 3 warn are started, which is a normal behavior.

7, zookeeper after the successful start can be Zookeeper-3.4.6\bin folder Zkcli.bat Verify the connection is normal, such as creating a node "Create/testnode HelloWorld", view node "get/ Testnode "To connect to other Zkserver in the cluster, the node data should be consistent. For more instructions, use the Help command to view.

8, for the Linux environment deployment basically consistent, zoo.cfg configuration file in the data and Datalog folder path to the Linux format path, using the "./zkserver.sh start-foreground command to start Zkserver, note that the start start parameter cannot output exception information.

9, this zookeeper configuration is complete.

Dubbo Governance Platform Deployment (

The contents of the above look less intuitive. It would be great if there was a console to manage and show. I have to say Dubbo is very intimate.

Download

Website:

Http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-admin/2.4.1/dubbo-admin-2.4.1.war

However, the address has not been downloaded recently.

http://pan.baidu.com/share/link?shareid=2205137761&uk=3442370350&fid=707816148751698 can be downloaded here.

installation

Copy the war package to the Tomcat/webapps directory and start Tomcat. In the browser, enter:

http://localhost:8080/dubbo/

Dubbo-admin-2.5.3.war extract the following files

Delete all files under D:\ProgramFiles_java\Apache software foundation\tomcat 7.0\webapps\root path, copy and unzip the files to this path, the effect is as follows

If need to modify login Dubbo governance platform password, enter D:\ProgramFiles_java\Apache software foundation\tomcat 7.0\webapps\root\web-inf Path, Open Dubbo.properties

Default two users, user name password is root/root guest/guest

Launch Tomcat, browser input address: http://localhost:8080/into the Dubbo governance platform

Description Dubbo Governance Platform deployment complete

Service provider packaged to service consumer reference

3. Source code
<!--zookeeper--<dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> &LT;SCOPE&GT;PR ovided</scope> </dependency> <dependency> <groupid>org.apache.zoo Keeper</groupid> <artifactId>zookeeper</artifactId> <version>3.4.6</versi on> <exclusions> <exclusion> <artifactid>jmxtools</ar                Tifactid> <groupId>com.sun.jdmk</groupId> </exclusion> <exclusion> <artifactId>jmxri</artifactId> &LT;GROUPID&GT;COM.S un.jmx</groupid> </exclusion> <exclusion> <artifacti d>jms</artifactid&Gt <groupId>javax.jms</groupId> </exclusion> </exclusions> </depe Ndency>

<!--dubbo-->
    <dependency>            <groupId>com.alibaba</groupId>            <artifactid>dubbo</artifactid >            <version>2.4.9</version>            <scope>compile</scope>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupid>org.springframework </groupId>                </exclusion>            </exclusions>        </dependency>

Provider Applicationcontext.xml:<beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance " xmlns:dubbo="Http://code.alibabatech.com/schema/dubbo "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd " > <!--specific implementation bean--<bean id= "demoservice" class= "Com.unj.dubbotest.provider.impl.DemoServiceImpl"/ > <!--provider application information for computing dependencies--<dubbo:application name= "xixi_provider"/> <!--use multicast broadcast registry to expose the service address <dubbo:registry address= "multicast://224.5.6.7:1234"/> -<!--Use the Zookeeper registry to expose the service address --<dubbo:registry protocol= "zookeeper" address= "127.0.0.1:2181"/> <!--exposed at 20880 ports with the Dubbo protocol --<dubbo:protocol name= "Dubbo" port= "2090"/> <!--declaring a service interface that needs to be exposed--<dubbo:service interface= "com.unj.dubbotest.provider.DemoService" ref= " Demoservice "/> </beans>

Service Consumer Code dubbo-consumer:http://download.csdn.net/detail/adam_zs/9470354

Consumer Applicationcontext.xml:<beans xmlns= " Span class= "Html-attribute-value" >http://www.springframework.org/schema/beans "  xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance " xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo "xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/ Dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd  " <!--consumer app name for calculating dependencies, not matching criteria, not the same as the provider--<dubbo:application name= "hi_consumer"/> <!--Use the Zookeeper registry to expose the service address --<!--<dubbo:registry address= "multicast://224.5.6.7:1234"/> -<dubbo:registry protocol= "zookeeper" address= "127.0.0.1:2181"/> <!--generate a remote service proxy that can be used like a local bean demoservice--<dubbo:reference id= "demoservice" interface= " Com.unj.dubbotest.provider.DemoService "/> </beans>after the invocation method is injected into spring, the corresponding service interface is obtained through ApplicationContext and the service method is invoked. 4. Boot order
    1. Start Zookeeper
    2. Start Tomcat, start over you can enter address http://localhost:8080/can see Dubbo governance platform
    3. Start service provider Provider.java
    4. Start Consumer Consumer.java
5. Successful Start

Dubbo+zookeeper Example

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.