http://www.cnblogs.com/luoxn28/p/6242546.html Dubbo服務(wù)的發(fā)展和作用:
dubbo的簡單環(huán)境搭建1,、安裝zookeeper zookeeper下載地址:http://www./dyn/closer.cgi/zookeeper/,,下載完成解壓即可運行。注意,,zookeeper目錄下的conf文件夾中并沒有zookeeper的配置文件,,不過有一個zoo_sample.cfg文件,將其命令為zoo.cfg即可使用默認(rèn)配置,。 點擊zkServer.cmd啟動zookeeper:
2,、安裝dubbo-admin.war 安裝dubbo-admin.war這一步是非必需的,不過使用dubbo-admin可以方便查看dubbo的運行狀態(tài)和數(shù)據(jù),,便于管理,。下載完成后放入到tomcat的webapps目錄下,啟動tomcat訪問dubbo即可,,登錄之后就可以看到如下頁面(登錄用戶名和密碼在dubbo.properties文件中查看):
3,、dubbo-server端配置 新建dubbo-server工程,下載所需的jar包,,編寫提供服務(wù)的接口和實現(xiàn)類,,然后通過zookeeper注冊中心暴露服務(wù)。 (3.1) 在maven文件中添加所需jar包的依賴: <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!--zkclient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- log relation --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- spring relation --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.4.RELEASE</version> </dependency> (3.2) 工程spring文件(applicationContext.xml)配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xmlns:dubbo="http://code./schema/dubbo" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc.xsd http://code./schema/dubbo http://code./schema/dubbo/dubbo.xsd"> <bean id="demoService" class="com.luoxn28.dubbo.impl.DemoServiceImpl"/> <!-- 提供方應(yīng)用消息 --> <dubbo:application name="demo-provider"/> <!-- 使用zookeeper注冊中心暴露服務(wù) --> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 對外服務(wù) --> <dubbo:service interface="com.luoxn28.dubbo.DemoService" ref="demoService"/> </beans> (3.3) 對外服務(wù)接口及其實現(xiàn)類: /** * DemoService */ public interface DemoService { String sayHello(String name); } /** * DemoServiceImpl */ public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hi " + name + ", I am dubbo service"; } } (3.4) 啟動類: /** * ServiceStart */ public class ServiceStart { public static void main(String[] args) { // 加載applicationContext.xml ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("start dubbo"); while (true) { Thread.yield(); } } } 整個dubbo-server工程結(jié)構(gòu)如下:
4,、dubbo-client端配置 新建dubbo-client工程,,下載所需的jar包,編寫獲取服務(wù)的接口,,然后通過zookeeper注冊中心暴露的服務(wù)來獲取,。 (4.1) 在maven文件中添加所需jar包的依賴: <!-- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!--zkclient--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- log relation --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- spring relation --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.4.RELEASE</version> </dependency> (4.2) 工程spring文件(applicationContext.xml)配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xmlns:dubbo="http://code./schema/dubbo" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context.xsd http://www./schema/mvc http://www./schema/mvc/spring-mvc.xsd http://code./schema/dubbo http://code./schema/dubbo/dubbo.xsd"> <!-- 提供方應(yīng)用消息 用于計算依賴關(guān)系 --> <dubbo:application name="demo-client"/> <!-- 使用zookeeper注冊中心暴露服務(wù)地址 --> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <!-- 生成遠(yuǎn)程服務(wù)代理,可以像使用本地bean一樣使用demoService --> <dubbo:reference id="demoService" interface="com.luoxn28.dubbo.DemoService" check="false"/> </beans> (4.3) 待獲取服務(wù)的接口: public interface DemoService { String sayHello(String name); } (4.4) 啟動類 /** * ClientStart */ public class ClientStart { public static void main(String[] args) { System.out.println("hello dubbo"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); DemoService service = (DemoService) context.getBean("demoService"); String response = service.sayHello("luoxn28"); System.out.println(response); } } 整個dubbo-server工程結(jié)構(gòu)如下:
運行ClientStart類之后,,輸出如下:
注意: 首先運行zookeeper,,然后啟動dubbo-server程序,,最后運行dubbo-client程序。dubbo-client中的服務(wù)接口路徑需要和dubbo-server的一致,,也就是說一個類在dubbo-server中的url和在dubbo-client中的url要一樣,,否則運行會出現(xiàn)Forbid consumer異常。
參考: |
|