轉自: https://www.cnblogs.com/fri-yu/p/5981436.html
項目參考:
http://dubbo.io/User+Guide-zh.htm
https://my.oschina.net/superman158/blog/466637
項目使用 maven+idea 進行開發,以zookeeper為注冊中心,是以需要安裝并運作zookeeper
---------服務端開發
引入相關依賴:
<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):
public interface Provider {
String build(String name) throws Exception;
}
實作接口(DemoServiceImpl .java):
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”
<?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 inter
ref="demoService" />
</beans>
寫啟動類(Test1.java)
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"
inter />
</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
聯調成功。
demo 檔案連接配接: http://files.cnblogs.com/files/fri-yu/dubbo.zip