建立Dubbo-client
建立一個maven-archetype-quickstart工程
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>dubbo-client</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dubbo-client</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>first-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
App.java
public static void main( String[] args )
{
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("dubbo-client.xml");
ISumService sumService = (ISumService) context.getBean("sumService");
System.out.println("3+4="+sumService.sum(3,4));
}
dubbo-client.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">
<dubbo:application name="dubbo-client" owner="lyj"/>
<dubbo:registry address="zookeeper://192.168.67.128:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:reference id="sumService" interface="com.taotao.ISumService"/>
</beans>
server端
建立兩個子模塊 api和provider
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.taotao</groupId>
<artifactId>do-dubboserver</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>first-api</module>
<module>first-provider</module>
</modules>
<name>do-dubboserver</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
api 的接口
public interface ISumService {
Integer sum(Integer a,Integer b);
}
provider
public class SumService implements ISumService {
@Override
public Integer sum(Integer a, Integer b) {
return a+b;
}
}
public class App
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("dubbo-server.xml");
context.start();
System.in.read();
}
}
dubbo-server.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">
<dubbo:application name="dubbo-provider" owner="lyj"/>
<dubbo:registry address="zookeeper://192.168.67.128:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.taotao.ISumService" ref="sumService"/>
<bean name="sumService" class="com.taotao.service.SumService"/>
</beans>