項目參考:java
http://dubbo.io/User+Guide-zh.htmspring
https://my.oschina.net/superman158/blog/466637app
項目使用 maven+idea 進行開發,以zookeeper爲註冊中心,因此須要安裝並運行zookeepermaven
---------服務端開發ide
引入相關依賴:ui
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.9</version> </dependency> </dependencies>
聲明Provider接口(Provider.java):idea
public interface Provider { String build(String name) throws Exception; }
實現接口(DemoServiceImpl .java):spa
public class DemoServiceImpl implements Provider { public String build(String name) throws Exception { System.out.println(" got a argument: " + name); return "message from provider: " + name; } }
編輯spring的配置文件,隨便命名,這裏命名爲「applicationContext.xml」.net
<?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 "> <!-- 具體的實現bean --> <bean id="demoService" class="com.dubbotest.impl.DemoServiceImpl" /> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="anyname_provider" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.dubbotest.Provider" ref="demoService" /> </beans>
寫啓動類(Test1.java)code
public class Test1 { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}); context.start(); System.out.println(" app run "); System.in.read(); // 按任意鍵退出 } }
啓動本機的zookeeper服務,而後執行Test1.main()
Console中成功打出:app run 即成功。
---------客戶端開發
項目結構、文件,整個照抄。
刪除「DemoServiceImpl .java」。
修改applicationContext.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="consumer_app" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:consumer timeout="5000" /> <dubbo:reference id="demoService" interface="com.dubbotest.Provider" /> </beans>
修改啓動類,重命名爲Test2.java(Test2.java)
public class Test2 { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); context.start(); Provider demoService = (Provider) context.getBean("demoService"); // 獲取bean // service // invocation // proxy String message = ""; try { message = demoService.build("2016-10-20"); System.out.println(" the message from server is:" + message); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
執行Test2.main()
服務端的console打印出: got a argument: 2016-10-20
客戶端的console打印出: the message from server is:message from provider: 2016-10-20
聯調成功。