新啓動的項目中可能會使用到dubbo,由於以前並無接觸過,因此先小試一下java
示例運行環境準備:OS X 10.10.5 + java version "1.8.0_40"git
zookeeper 和 dubbo-admin運行在虛擬機上,環境爲Centos7.0 + java version "1.8.0_73"github
項目使用gradlle管理web
其中dubbo-demo未項目,裏面新建了兩個module,api爲服務提供方,console爲服務消費方。注意,須要在console module中添加api 的module依賴spring
api中代碼以下:apache
DemoServicejson
package com.dh.demo.service; public interface DemoService { public String hello(String name); }
DemoServiceImplapi
package com.dh.demo.service.impl; import com.dh.demo.service.DemoService; public class DemoServiceImpl implements DemoService { public String hello(String name) { return "hello " + name; } }
服務端spring配置文件dubbo-provider.xml文件內容以下mvc
<?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-demo-api"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://vm:2181"/> <bean id="demoService" class="com.dh.demo.service.impl.DemoServiceImpl"/> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.dh.demo.service.DemoService" ref="demoService"/> </beans>
啓動api的測試代碼以下:app
package com.dh.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Provider { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/api/src/resources/dubbo-provider.xml"); context.start(); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
服務提供者的配置到此結束。
下面是消費者的配置和代碼
spring配置文件dubbo-consumer.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-demo-console"></dubbo:application> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://vm:2181"/> <!-- 要引用的服務 --> <dubbo:reference interface="com.dh.demo.service.DemoService" id="demoService"></dubbo:reference> </beans>
啓動消費者測試的代碼以下
Consumer:
package com.dh.test; import com.dh.demo.service.DemoService; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/xxx/dubbo-demo/console/src/resources/dubbo-consumer.xml"); context.start(); DemoService testRegistryService = (DemoService)context.getBean("demoService"); // 獲取遠程服務代理 String hello = testRegistryService.hello("world"); System.out.println(hello); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
先運行提供者測試代碼,再運行消費者代碼。能夠看到下面輸出
至此測試成功
若是運行了dubbo-admin的話能夠進去後能夠查看和管理服務
這是提供者:
消費者:
有些童鞋可能不知道怎麼運行dubbo-demo
看dubbo的github主頁就行
若是再jdk8的環境下運行可能在啓動的時候會遇到以下問題
Bean property 'URIType' is not writable or has 。。。。。後面省略
解決方式參考:https://github.com/alibaba/dubbo/issues/50 中 stirp 的方式便可
注:dubbo-demo的build.gradle內容以下
group 'com.dh' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.5 repositories { maven { url "http://repo.maven.apache.org/maven2" } } dependencies { compile(group: 'com.alibaba', name: 'dubbo', version: '2.5.3') { exclude(module: 'spring') } compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.6' compile group: 'com.google.zxing', name: 'core', version: '3.1.0' compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1' compile group: 'io.netty', name: 'netty', version: '4.0.0.Alpha8' compile group: 'javax.servlet', name: 'servlet-api', version: '2.5' compile group: 'org.springframework', name: 'spring-context-support', version: '4.1.3.RELEASE' compile group: 'org.springframework', name: 'spring-web', version: '4.1.3.RELEASE' compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.3.RELEASE' compile group: 'com.alibaba', name: 'fastjson', version: '1.2.7' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.5' compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.6.3' compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.6.3' compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.2' compile group: 'org.jdom', name: 'jdom', version: '1.1.3' compile group: 'com.github.sgroschupf', name: 'zkclient', version: '0.1' }