Dubbo and Zookeeper, spring integrated use
Dubbo uses a full spring configuration, transparent access to the application, no API intrusion to the application, just load the Dubbo configuration with spring, and Dubbo the spring-based schema extension.
One: Single-machine mode installation Zookeeper
1, Download Zookeeper Registration center,: http://www.apache.org/dyn/closer.cgi/zookeeper/Download after decompression, enter the E:\zookeeper-3.3.6\zookeeper-3.3.6 \ bin,
Double-click Zkserver.cmd to start the registry service.
Zkserver.sh "Linux" or Zkserver.cmd "Windows"
2, before you execute the startup script, there are several basic configuration items need to be configured, zookeeper configuration file in the Conf directory, this directory has zoo_sample.cfg and log4j.properties, what you need to do is Zoo_ Sample.cfg renamed Zoo.cfg Because zookeeper will find this file as the default profile at startup. The meanings of each configuration item in this configuration file are described in detail below.
Ticktime: This time is the time interval between the zookeeper server or between the client and the server to maintain the heartbeat, that is, each ticktime time sends a heartbeat.
DataDir: As the name implies is Zookeeper to save the data directory, by default, Zookeeper will write the data log file is also stored in this directory.
Datalogdir: As the name implies zookeeper the directory where the log files are saved
ClientPort: This port is the port that the client connects to the Zookeeper server, Zookeeper listens to the port and accepts the client's access request.
When configured, zookeeper listens to the 2181 port of this machine
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 registry to expose service addresses-- ><!--<dubbo:registry address= "multicast://224.5.6.7:1234"/>--><!--use zookeeper registry to expose the service address-- That is, the server IP address and port number of the zookeeper--><dubbo:registry address= "zookeeper://192.168.24.213:2181"/><!-- Exposing the service with the Dubbo protocol on 20880 Ports--><dubbo:protocol name= "Dubbo" port= "20880"/><!--declares a service interface that needs to be exposed--><dubbo:service interface= "Com.unj.dubbotest.provider.DemoService" ref= "Demoservice"/></beans>
load the spring configuration, start the service (or build the project as Web project, and then in Web . XML in the configuration is good Spring the boot and then throw it to Tomcat can be served in the following):
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 is always open, use the blocking of the input stream to simulate}}
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 "/> as Provider <!--use zookeeper registry to expose the service address--><!--<dubbo:registry address= "multicast://224.5.6.7:1234"/>-->< Dubbo:registry address= "zookeeper://192.168.24.213:2181"/><!--generate a remote service proxy that can be used like a local bean-- <dubbo:reference id= "Demoservice" interface= "Com.unj.dubbotest.provider.DemoService"/></beans>
To invoke a 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 's War Package
: http://download.csdn.net/detail/u013286716/7041185
The operation is as follows:
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 , this experiment uses the 1.6
2 , start Tomcat , Access ip:8080 or if it's local, you can use it. localhost:8080
Enter the user name password, in the Dubbo.properties file under E:\apache-tomcat-7.0.6-dubbo\webapps\ROOT\WEB-INF can be seen, such as:
3 , Access http://192.168.24.213:38080/
4 , launch our service providers and consumers to see
Overall Description :
Zookeeper (registration Center) deployed to 213 Service providers and service consumers are 215 machine, and of course we can also deploy service providers and service consumers to two different machines, respectively.
Advantages:
Service providers and service consumers only need to know the registry, and they have to deal with the third party through the registry, as long as the registration center has registered services, we can use, to achieve the service provider and service consumers decoupling.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Dubbo Combat" Dubbo+zookeeper+spring Integration Application (ii)