Before the use of EJB to do distributed systems, the previous period of time with people chatting, found or dubbo+zk used more, so, play with their own.
First install a ZK as a service registry, then, to build a maven project, POM inside the following configuration:
<dependencies><!--Dubbo--><dependency><groupid>com.alibaba</groupid>< Artifactid>dubbo</artifactid><version>2.0.13</version></dependency><dependency ><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId>< version>3.3.6</version><exclusions><exclusion><groupid>log4j</groupid>< Artifactid>log4j</artifactid></exclusion></exclusions></dependency><dependency ><groupid>log4j</groupid><artifactid>log4j</artifactid><version>1.2.16</ Version></dependency></dependencies>
After that, join the service provider's configuration file:
<?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 ><!--provider app information for calculating dependencies--><dubbo:application name= "Hello-world-app"/><!--Configuration Zookeeper Registry-- <dubbo:registry protocol= "Zookeeper" address= "127.0.0.1:2181"/><!--exposed service Dubbo with--><DUBBO protocol on 20880 ports: Protocol Name= "Dubbo" port= "20880"/><!--declares the service interface to be exposed--><dubbo:service interface= " Com.alibaba.dubbo.demo.DemoService "ref=" Demoservice "/><!--and local beans implement service--><bean id=" Demoservice " class= "Com.alibaba.dubbo.demo.DemoServiceImpl"/></beans>
Interfaces and classes for the service provider:
Package com.alibaba.dubbo.demo;/** * Defines the service interface: This interface needs to be packaged separately, shared between service providers and consumers * @author Liuhuichao * */public interface Demoservice {string SayHello (string name);}
/** * Implement Interface in service provider: Hide implementation for service consumer * @author Liuhuichao * */public class Demoserviceimpl implements Demoservice {public String SA Yhello (String name) {return "Hello," +name;}}
Then the service consumer:
<?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= like the provider" Consumer-of-helloworld-app "/><!--Configuration Zookeeper Registry--><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.alibaba.dubbo.demo.DemoService"/></beans>
Consumer Category:
Package Com.alibaba.dubbo.demo;import Org.springframework.context.support.classpathxmlapplicationcontext;public Class Consumer {public static void main (string[] args) {Classpathxmlapplicationcontext context=new Classpathxmlapplicationcontext ("Consumer.xml"); Context.start ();//Get Beandemoservice demoservice= (DemoService) Context.getbean ("Demoservice"); String Hello=demoservice.sayhello ("World"); System.out.println (hello);//Display the result of the call}}
When testing, run the service provider, register the service with the registry, and then run the service consumer to make the call.
The original rational thing will follow up after.
Simple example of getting started with Dubbo