用最简单的组播方式来编写dubbo小例子
分别新建两个maven webapp项目,一个是服务器端的,一个是客户端的。
服务器端这边项目的结构如下图:
先给pom添加依赖jar包
pom.xml内容如下:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>dubbo_server</groupId> <artifactId>dubbo_server</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>dubbo_server Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> <exclusion> <artifactId>mail</artifactId> <groupId>javax.mail</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> <type>pom</type> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> </dependencies> <build> <finalName>dubbo_server</finalName> </build> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
编写一个接口和一个实现类
package com.lhx.service; /** * Created by Lhx on 14-11-19. */ public interface SayHelloToClient { public String sayHello(String hello); }
实现类:
package com.lhx.service.impl; import com.lhx.service.SayHelloToClient; /** * Created by Lhx on 14-11-19. */ public class SayHelloToClientImpl implements SayHelloToClient{ public String sayHello(String hello){ System.out.println("我接收到了:" + hello); return hello + "你也好啊!!!" ; } }
配置文件applicationProvider.xml
在里面配置bean,内容如下:
<?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="hello-world" /> <!-- 注册地址 --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- 接口的位置 --> <dubbo:service interface="com.lhx.service.SayHelloToClient" ref="demoService" executes="10" /> <!-- 实现bean,客户端应用的bean就以这个id名称为主 --> <bean id="demoService" class="com.lhx.service.impl.SayHelloToClientImpl" /> </beans>
编写主函数,启动服务用:
package com.lhx.main; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Lhx on 14-11-19. */ public class MyMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("按任意键退出"); System.in.read(); } }
服务器端算是完成了,现在来编写客户端程序
客户端这边的目录结构如下:
pom.xml里面的内容和服务器端类似
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>dubbo_client</groupId> <artifactId>dubbo_client</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>dubbo_client Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> <exclusion> <artifactId>mail</artifactId> <groupId>javax.mail</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6.SEC03</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.github.adyliu</groupId> <artifactId>zkclient</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> <type>pom</type> </dependency> <dependency> <groupId>dubbo_server</groupId> <artifactId>dubbo_server</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>dubbo_client</finalName> </build> </project>
以下的这个有点不同,从服务器端那里的pom.xml里面寻找内容
<dependency> <groupId>dubbo_server</groupId> <artifactId>dubbo_server</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
编写客户端调用程序
package com.lhx.client; import com.lhx.service.SayHelloToClient; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Lhx on 14-11-19. */ public class MyClient { public void sayHello(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); //获取服务器那边的bean SayHelloToClient demoService = (SayHelloToClient) context.getBean("demoService"); System.out.println(demoService.sayHello("lisi")); } }
编写客户端主函数
package com.lhx.test; import com.lhx.client.MyClient; /** * Created by Lhx on 14-11-19. */ public class AppTest{ public static void main(String[] args) { MyClient myClient = new MyClient(); myClient.sayHello(); } }
最后配置applicationConsumer.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 --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <dubbo:consumer timeout="5000" /> <!-- which service to consume? --> <dubbo:reference id="demoService" interface="com.lhx.service.SayHelloToClient"/> </beans>
先启动服务器端的主函数
再启动客户端的主函数
服务器端这边收到了参数,知道要调用那个类中的方法。有如下显示:
服务器这边处理完了程序,客户端这边就收到了服务器端发来的处理结果。这有点像WebService了。