"Dubbo Combat" dubbo+zookeeper+spring integrated application-dubbo based on Zookeeper to realize distributed service (II.)

Source: Internet
Author: User
Tags zookeeper

Dubbo and Zookeeper, spring integrated use

Dubbo uses full spring configuration to transparently access applications. Without any API intrusion, it is possible to load Dubbo based on the spring schema extension by simply loading the Dubbo configuration with spring.

One: Single-machine mode installation Zookeeper

1, Download Zookeeper Center,: http://www.apache.org/dyn/closer.cgi/zookeeper/Download After the decompression can be. Enter the E:\zookeeper-3.3.6\zookeeper-3.3.6\bin.

Double-click Zkserver.cmd to start the brochure Center service.

Zkserver.sh "Linux" or Zkserver.cmd "Windows"

2, before you execute the startup script, there are several major configuration items that need to be configured, zookeeper configuration files under the Conf folder. This folder has Zoo_sample.cfg and log4j.properties, what you need to do is to rename zoo_sample.cfg to Zoo.cfg, because zookeeper will find this file as the default profile at startup.

Here's a concrete introduction. The meaning of each configuration item in this configuration file.

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Ticktime: This time is the time interval between zookeeper server or between client and server to maintain heartbeat, that is, each ticktime time sends a heartbeat.

DataDir: As the name implies is Zookeeper to save the data folder, by default, Zookeeper will write the data log file is also saved in this folder.

Datalogdir: As the name implies, zookeeper the folder where the log files are saved

ClientPort: This port is the port that the client connects to Zookeeper server, Zookeeper will listen to the port and accept the client's access request

After the configuration is ready. The zookeeper will listen to the 2181 port on this machine.

When these configuration items are configured, you will now be able to start Zookeeper, after starting to check whether Zookeeper is already in service, to be able to check with the Netstat–ano command to see if there is a ClientPort port number you configured in the Listening service.

II: Service Providers

Define the Service interface: (this interface needs to be packaged separately, shared between service providers and consumers)

Package Com.unj.dubbotest.provider;import Java.util.list;public Interface Demoservice {string SayHello (string name); Public List getusers ();}

implementing interfaces in the service provider: (Hidden implementations for service consumers)

Package Com.unj.dubbotest.provider.impl;import Java.util.arraylist;import Java.util.list;import Com.unj.dubbotest.provider.demoservice;public class Demoserviceimpl implements Demoservice {public String SayHello ( String name) {return "Hello" + Name;} Public List getusers () {List List = new ArrayList (); User U1 = New user (), U1.setname ("Hejingyuan"); u1.setage; U1.setsex ("F"); User U2 = New user (), U2.setname ("Xvshu"); U2.setage (+); U2.setsex ("M"); List.add (U1); List.add (U2); return list;}}

To declare an exposure service with spring configuration:


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: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 "/&G t;<!--provider app information for calculating dependencies--><dubbo:application name= "Xs_provider"/><!--using multicast Broadcast Center Exposure service address-- ><!--<dubbo:registry address= "multicast://"/>--><!--use Zookeeper Center to expose service address-- That is, zookeeper's ServerIP address and port number--><dubbo:registry address= "zookeeper://"/><!-- Exposing the service with the Dubbo protocol on port 20880--><dubbo:protocol name= "Dubbo" port= "20880"/><!--declares the service interface to be exposed--&GT;<dubbo:service interface= "Com.unj.dubbotest.provider.DemoService" ref= "Demoservice"/></beans>

load the spring configuration. Start the service (or build the project as a Web project.) Then , configure Spring startup in Web. Xml and throw it into Tomcat to provide the service):

Package Com.unj.dubbotest.provider.impl;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Provider {public static void main ( String[] args) throws Exception {Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (New String [] {"Applicationcontext.xml"}); Context.start (); System.in.read (); To ensure that the service has been open. To simulate} by plugging in the input stream

Third: Service Consumers

To reference a remote service through spring configuration:

<?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: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, used to calculate dependencies, not match criteria, do not--><dubbo:application name= "Hjy_consumer"/><!--use Zookeeper Center to expose service addresses --><!--<dubbo:registry address= "multicast://"/>--><dubbo:registry address= " zookeeper:// "/><!--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>

Call Service test:

Package Com.alibaba.dubbo.demo.pp;import Java.util.list;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.unj.dubbotest.provider.demoservice;public class Consumer {public static void main (string[] args) throws Exception { Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (new string[] {"Applicationcontext.xml"}) ; Context.start ();D emoservice demoservice = (demoservice) context.getbean ("Demoservice"); String Hello = Demoservice.sayhello ("Hejingyuan"); System.out.println (hello); List List = Demoservice.getusers (); if (list = null && list.size () > 0) {for (int i = 0; i < list.size (); i+ +) {System.out.println (List.get (i));}} System.in.read ();}}

Test results:

attached: Management page of Dubbo

need to download: dubbo-admin-2.5.3 War Package

: http://download.csdn.net/detail/u013286716/7041185

Actions such as the following:

1 . Replace the root folder content under Tomcat/webapps (that is, replace Tomcat's startup home page), unzip the downloaded war package into Webapps/root, and replace it directly

Note: JDK Do not use 1.8 . 1.6 is used in this experiment.

2 , start Tomcat , Interview ip:8080 you can use it or assume it's local. localhost:8080

Input Usernamepassword, in the E:\apache-tomcat-7.0.6-dubbo\webapps\ROOT\WEB-INF under the Dubbo.properties file can be found, such as:

3 , Interview

4 , start our service providers and consumers to see

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>

General description :

Zookeeper (note Center) deployed to 213 on the machine. Service providers and service consumers are executed on the 215. Of course, we can also be able to deploy service providers and service consumers to different two machines respectively.


Service providers and service consumers simply need to know that the centre is available, and that their dealings with them need to be through the third party of the Register Center. We are able to use it only if the service has been registered in the center of the register, enabling decoupling between service providers and service consumers.

"Dubbo Combat" dubbo+zookeeper+spring integrated application-dubbo based on Zookeeper implementation of distributed Services (ii)

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.