Create a new MAVEN project
Create a service interface
Package Com.pcx.dubbo_facade;public interface Demoservice { string SayHello (string name);}
Run the clean install package Dubbo-facade
Next create the Dubbo-provider project
In the Pom.xml, refer to the jar package of the service interface just now
<span style= "White-space:pre" ></span><dependency><groupId>com.pcx</groupId> <span style= "White-space:pre" ></span><artifactId>dubbo-facade</artifactId> <version >0.0.1-SNAPSHOT</version></dependency>
Writing service Implementation classes
Package Com.pcx.dubbo_prodiver;import Org.springframework.stereotype.service;import Com.pcx.dubbo_facade. Demoservice; @Service ("Demoservice") public class Demoserviceimpl implements Demoservice {public string SayHello (string Name) {return "Hello" + Name;}}
Write two spring profiles under the resource directory
Dubbo-provider.xml
<?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 "><!--define app name--><dubbo:application name=" Dubbo-demo-provider "/><!--ZK Registry Address-- <dubbo:registry protocol= "Zookeeper" address= "192.168.1.10:2181"/><!--exposed service with Dubbo protocol on 21000 Ports-->< Dubbo:protocol name= "Dubbo" port= "21000"/><!--Configuration Service Interface--><dubbo:service interface= "Com.pcx.dubbo_facade . Demoservice "ref=" Demoservice "/></beans>
Spring-context.xml
<?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:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http: Www.springframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX Http://www.springframework.org/schema/tx/spring-tx-3.2.xsd Http://www.springframework.org/schema/cont Ext http://www.springframework.org/schema/context/spring-context-3.2.xsd "default-autowire=" ByName "Default-laz Y-init= "false" ><!--Configure Bean--><conte in a commented mannerXt:annotation-config/><!--Configure the path of the package to be scanned--><context:component-scan base-package= "COM.PCX"/>< Import resource= "Dubbo-provider.xml"/></beans>
Writing test classes under Src/test/java path start the Dubbo service
Package Com.pcx.dubbo_prodiver;import Org.apache.commons.logging.log;import Org.apache.commons.logging.LogFactory ; Import Org.springframework.context.support.classpathxmlapplicationcontext;public class Dubboprovider {private Static final Log log = Logfactory.getlog (dubboprovider.class);p ublic static void Main (string[] args) {try {Classpathxmlap Plicationcontext context = new Classpathxmlapplicationcontext ("Classpath:spring-context.xml"); Context.start ();} catch (Exception e) {log.error ("= = Dubboprovider Context start error:", e);} Synchronized (Dubboprovider.class) {while (true) {try {DubboProvider.class.wait ()} catch (Interruptedexception e) { Log.error ("= = Synchronized Error:", e);}}}}
Running this test class, we can see our exposed service in the Dubbo console
Create a new project named Dobbo-consumer
A jar package that relies on our service interface under Pom.xml
<span style= "White-space:pre" ></span><dependency><groupId>com.pcx</groupId> <artifactId>dubbo-facade</artifactId> <version>0.0.1-snapshot</version></dependency >
Two new spring profiles added under the src/main/resource/directory
Dubbo-consumer.xml
<?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--><dubbo:application name=" Dubbo-demo-consumer "/><!--fill in the address of the ZK registry-- <dubbo:registry protocol= "Zookeeper" address= "192.168.1.10:2181"/><!--Reference Service provides the path of the interface--><dubbo: Reference interface= "Com.pcx.dubbo_facade. Demoservice "id=" Demoservice "check=" false "/></beans>
Spring-context.xml
<?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:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http: Www.springframework.org/schema/beans/spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema/aop/spring-aop-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX Http://www.springframework.org/schema/tx/spring-tx-3.2.xsd Http://www.springframework.org/schema/cont Ext http://www.springframework.org/schema/context/spring-context-3.2.xsd "><import resource=" dubbo-consum Er.xml "/></beans>
Write the test class that invokes the service in Src/test/java
Package Com.pcx.dubbo_consumer;import Org.apache.commons.logging.log;import Org.apache.commons.logging.LogFactory ; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.pcx.dubbo_facade. Demoservice;public class Consumer { private static final log log = Logfactory.getlog (Consumer.class); public static void Main (string[] args) throws Interruptedexception { Classpathxmlapplicationcontext context = new Cla Sspathxmlapplicationcontext ("Classpath:spring-context.xml"); Context.start (); Demoservice Demoservice = (demoservice) context.getbean ("Demoservice"); String Hello = Demoservice.sayhello ("World"); Log.info ("print" +hello); Thread.Sleep (100000); }}
Run the test class
Call succeeded
You can see the information in the Dubbo console at this time
Ali Dubbo Framework Use series: creation and use of service providers and consumers