Dubbo服務的發展和做用:html
註冊中心的選擇git
dubbo支持多種類型的註冊中心:github
一、安裝zookeeperweb
zookeeper下載地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,下載完成解壓便可運行。注意,zookeeper目錄下的conf文件夾中並無zookeeper的配置文件,不過有一個zoo_sample.cfg文件,將其命令爲zoo.cfg便可使用默認配置。spring
點擊zkServer.cmd啓動zookeeper:express
二、安裝dubbo-admin.warapache
安裝dubbo-admin.war這一步是非必需的,不過使用dubbo-admin能夠方便查看dubbo的運行狀態和數據,便於管理。下載完成後放入到tomcat的webapps目錄下,啓動tomcat訪問dubbo便可,登陸以後就能夠看到以下頁面(登陸用戶名和密碼在dubbo.properties文件中查看):windows
三、dubbo-server端配置spring-mvc
新建dubbo-server工程,下載所需的jar包,編寫提供服務的接口和實現類,而後經過zookeeper註冊中心暴露服務。tomcat
(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.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean id="demoService" class="com.luoxn28.dubbo.impl.DemoServiceImpl"/> <!-- 提供方應用消息 --> <dubbo:application name="demo-provider"/> <!-- 使用zookeeper註冊中心暴露服務 --> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 對外服務 --> <dubbo:service interface="com.luoxn28.dubbo.DemoService" ref="demoService"/> </beans>
(3.3) 對外服務接口及其實現類:
/** * 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工程結構以下:
四、dubbo-client端配置
新建dubbo-client工程,下載所需的jar包,編寫獲取服務的接口,而後經過zookeeper註冊中心暴露的服務來獲取。
(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.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用消息 用於計算依賴關係 --> <dubbo:application name="demo-client"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.1.100:2181" /> <!-- 生成遠程服務代理,能夠像使用本地bean同樣使用demoService --> <dubbo:reference id="demoService" interface="com.luoxn28.dubbo.DemoService" check="false"/> </beans>
(4.3) 待獲取服務的接口:
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工程結構以下:
運行ClientStart類以後,輸出以下:
注意:
首先運行zookeeper,而後啓動dubbo-server程序,最後運行dubbo-client程序。dubbo-client中的服務接口路徑須要和dubbo-server的一致,也就是說一個類在dubbo-server中的url和在dubbo-client中的url要同樣,不然運行會出現Forbid consumer異常。
參考: