用最簡單的組播方式來編寫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了。