As for why to use MAVEN multiple modules to build the project, and what Dubbo is doing is not much to say, direct start.
First create a Maven project as the root module named Mydubbo and delete the SRC directory (not required)
Except we need to rely on spring,zookeeper.
Mydubbo->pom.xml
<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "HTT" P://www.w3.org/2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apach
E.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>mydubbo</artifactId> <packaging>pom</packaging> <VERSION>1.0-SNAPSH ot</version> <properties> <spring.version>3.2.4.RELEASE</spring.version> </prop erties> <modules> <module>myProvider</module> <module>myconsumer</module > <module>myService</module> </modules> <dependencies> <DEPENDENCY&G
T <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version& Gt;2.5.3</version> <exclusions> <exclusion> <groupid>org.spri Ngframework</groupid> <artifactId>spring</artifactId> </exclusion
> </exclusions> </dependency> <!--Dubbo Registration Center--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactid>zookeeper</artifactid>
; <version>3.4.6</version> </dependency> <!--zookeeper client--> <dependency&
Gt
<groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupid>org. Springframework</groupid> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupid>or G.springframework</groupid> <artifactId>spring-context</artifactId> <version& gt;${spring.version}</version> </dependency> </dependencies> </project>
Then create three MAVEN modules for MyService, MyProvider, Myconsumer, respectively.
MyService: Function Interface Module
Myprovider:dubbo Provider
Myconsumer:dubbo Consumer
Because consumers and providers have a consistent functional interface, they all rely on MyService modules (MyService project packing for Jars)
Myprovider->pom.xml
<?xml version= "1.0" encoding= "UTF-8"?> <project "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http ://maven.apache.org/xsd/maven-4.0.0.xsd ">
<parent>
<artifactid>mydubbo</artifactid >
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent >
<modelVersion>4.0.0</modelVersion>
<artifactId>myProvider</artifactId>
<dependencies>
<dependency>
<groupId>com</groupId>
<artifactid >myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Myconsumer->pom.xml
<?xml version= "1.0" encoding= "UTF-8"?> <project "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http ://maven.apache.org/xsd/maven-4.0.0.xsd ">
<parent>
<artifactid>mydubbo</artifactid >
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent >
<modelVersion>4.0.0</modelVersion>
<artifactId>myConsumer</artifactId>
<dependencies>
<dependency>
<groupId>com</groupId>
<artifactid >myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Myservice->pom.xml
<?xml version= "1.0" encoding= "UTF-8"?> <project "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http ://maven.apache.org/xsd/maven-4.0.0.xsd ">
<parent>
<artifactid>mydubbo</artifactid >
<groupId>com</groupId>
<version>1.0-SNAPSHOT</version>
</parent >
<modelVersion>4.0.0</modelVersion>
<name>myService</name>
< artifactid>myservice</artifactid>
<packaging>jar</packaging>
</project>
Establish a interface in MyService, named HelloService, as follows
Helloservice.java
Package com.service;
/**
* Created by Yuyufeng on 2016/11/16.
*
/public interface HelloService {
string Speakhello (string name);
}
Now we need to install myservice this project to produce a jar that can be relied upon for use by other references.
MyProvider depends on the jar that MyService produces.
Now, start writing the code for the Dubbo provider MyProvider
First, you need to provide a HelloService implementation
Helloserviceimpl.java
Package Com.service.impl;
Import Com.service.HelloService;
/**
* Created by Yuyufeng on 2016/11/16.
*
/public class Helloserviceimpl implements HelloService {public
string Speakhello (string name) {
Return "Hello:" + name;
}
Then use the spring configuration declaration to expose the service, creating a provider.xml as follows
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" Xsi:sch emalocation= "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 application information used to compute dependencies--> <dubbo:application name=" Hello-provider "/> <!--use multicast Broadcast Registration center exposes service address--> <dubbo:registry address= "zookeeper://127.0.0.1:2181"/> <!--exposes services on 20880 ports with Dubbo protocol > <dubbo:protocol name= "Dubbo" port= "20880"/> <!--declares the service interface to be exposed--> <dubbo:service INTERFAC E= "Com.service.HelloService" ref= "HelloService"/> <!--and local bean implementation Services--> <bean id= "HelloService" CLA Ss= "Com.service.impl.HelloServiceImpl" /> </beans>
The registration center used for this Dubbo is zookeeper, so a zookeeper service needs to be started locally.
Finally, we can start the provider service, and the startup class is as follows:
Package Com.service.impl;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import java.io.IOException;
/**
* Created by Yuyufeng on 2016/11/16.
*
/public class providerserver{public
static void Main (string[] args) throws IOException {
Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Provider.xml");
Context.start ();
Press any key to exit
System.in.read ();
}
We can see that our service provider has been registered on the Zookeeper Registration center
Then, write a consumer through the Dubbo to tune the following remote services
The consumer references the remote service spring configuration file through the spring configuration as follows:
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 application name, used to compute dependencies, not matching criteria, not-->
<dubbo:application name=" Hello-consumer " />
<!--use multicast broadcast registry to expose Discovery service address-->
<dubbo:registry address=" zookeeper://127.0.0.1:2181 "/>
<!--generate a remote service proxy that can be used like a local bean demoservice-->
<dubbo:reference Id= "HelloService" interface= "Com.service.HelloService"/>
</beans>
Consumer Startup class
Consumerclient.java
Import Com.service.HelloService;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Yuyufeng on 2016/11/16.
*
/public class Consumerclient {public
static void Main (string[] args) {
classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Consumer.xml");
HelloService HelloService = (helloservice) context.getbean ("HelloService");
String result = Helloservice.speakhello ("Yyf");
SYSTEM.OUT.PRINTLN (result);
}
Run the change class, the result is as follows:
As you can see, the remote service invocation succeeded.