一、Dubbox簡介java
Dubbox 是一個分佈式服務架構,其前身是阿里巴巴開源項目 Dubbo,被國內電商及互聯網項目使用,後期阿里巴巴中止了該項目的維護,噹噹網便在 Dubbo 基礎上進行優化,並繼續維護,爲了與原有的 Dubbo 區分,故將其命名爲 Dubbox。linux
Dubbox 致力於提供高性能和透明話的 RPC 遠程服務調用方案,以及 SOA 服務治理方案。簡單的說,Dubbox 就是個服務框架,若是沒有分佈式的需全,實際上是不須要用的,只有在分佈式的時候,纔有 Dubbox 這樣的分佈式服務架構需求 ,而且本質上是個服務調用的東西,說白了就是個遠程服務調用的分佈式架構。git
節點角色說明:github
Provider:暴露服務的服務提供方。web
Consumer:調用遠程服務的服務消費方。算法
Registry:服務註冊與發現的註冊中心。spring
Monitor:統計服務的調用次數和調用時間的監控中心。apache
Container:服務運行容器。瀏覽器
調用關係說明:spring-mvc
0、服務容器負責啓動,加載,運行服務提供者。
一、服務提供者在啓動時,向註冊中心註冊本身提供的服務。
二、服務消費者在啓動時,向註冊中心訂閱本身所需的服務。
三、註冊中心返回服務提供者地址列表給消費者,若是有變動,註冊中心將基於長鏈接推送變動數據給消費者。
四、服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,若是調用失敗,再選另外一臺調用。
五、服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。
二、註冊中心 Zookeeper
一、Zookeeper 介紹
官方推薦使用 zookeeper 註冊中心。註冊中心負責服務地址的註冊於查找,至關於目錄服務,服務提供者和消費者只在啓動時於註冊中心交互,註冊中心不轉發請求,壓力較小。
zookeeper 是 Appache Hadoop 的子項目,是一個樹型的目錄服務,支持變動推送,適合做爲 Dubbox 服務的註冊中心,工業強度較高,可用於生產環境。
二、Zookeeper 在 Linux (虛擬機)系統的安裝
安裝步驟:
第一步:安裝 jdk (zookeeper 的服務須要依賴於 JVM 環境)。
第二步:把 zookeeper 的壓縮包上傳到 linux 系統 。
第三步:解壓縮壓縮包(版本視本身的狀況而定,文中版本僅爲事例)。
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:進入 zookeeper-3.4.6 目錄,建立 data 目錄。
mkdir data
第五步:進入 conf 目錄,把 zoo_sample.cfg 更名爲 zoo.cfg。
cd conf
mv zoo_sample.cfg zoo.cfg
第六步:打開 zoo.cfg 文件,修改 data 屬性:dataDir=/root/zookeeper-3.4.6/data (本身的 data 目錄放在哪,就把路徑寫在哪)
三、Zookeeper 服務啓動
進入 bin 目錄,啓動服務輸入命令:./zkServer.sh start
輸出如下內容表示啓動成功
關閉服務輸入命令:./zkServer.sh stop
輸出已下信息表示關閉成功
查看狀態輸入命令:./zkServer.sh status
若是服務爲啓動狀態,提示
若是服務爲關閉狀態,提示
三、Dubbox 本地 jar 包 部署與安裝
Dubbox 的 jar 包並無部署到 Maven 中央倉庫中,在 Maven 的中央倉庫中能夠查找到 Dubbo 的最終版本是 2.5.3,阿里解散了 Dubbo 團隊後由噹噹網繼續維護此項目,並更名爲 Dubbox,座標不變,版本變動了,可是沒有提交到中央倉庫。
所以須要手動將 Dubbox 的 jar 包安裝到本身的本地倉庫中。
先將 dubbo-2.8.4.jar 包放到 d:\setup (個人是這樣,視本身的狀況而定),而後輸入命令
mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar
四、配置離線約束(可選,這個步驟是爲了在 application.xml 文件中配置 dubbo 時有提示)
一、先進入eclipse 的 preferences 選項,而後找到 XML Catalog
二、點擊 Add ,選擇 File System,打開預先下好的約束文件 dubbo.xsd
將key 設置爲 :http://code.alibabatech.com/schema/dubbo/dubbo.xsd
五、入門小 demo
一、服務提供者開發
1)、建立 Maven 工程(war)dubboxdemo-service,在 pom.xml 文件中引入依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itcast.demo</groupId> <artifactId>dubboxdemo-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <spring.version>4.2.4.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>8081</port> <!-- 請求路徑 --> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
2)、在工程的 webapps 下建立 WEB-INF 文件夾,建立 web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 加載spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3)、建立包 com.itcast.dubbodemo.service,用於存放業務接口,建立接口
1 package com.itcast.demo.service; 2 3 public interface UserService { 4 5 public String getName(); 6 }
4)、建立業務實體類,建立包 com.itcast.dubbodemo.service.impl,用於存放業務實體類。建立業務實體類:
1 package com.itcast.demo.service.impl; 2 3 import com.alibaba.dubbo.config.annotation.Service; 4 import com.itcast.demo.service.UserService; 5 6 @Service 7 public class UserServiceImpl implements UserService { 8 9 @Override 10 public String getName() { 11 // TODO Auto-generated method stub 12 return "itcast"; 13 } 14 15 }
注意:此時的 @Service 導的包是:com.alibaba.dubbo.config.annotation.Service ,若是是導的是 Spring 的包,獲取的是本地的 Service,而咱們是要獲取遠程的 Servie 服務,所以要注意
5)、編寫配置文件
在 src/main/resources 下建立 applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="dubboxdemo-service"/> <dubbo:registry address="zookeeper://192.168.25.128:2181"/> <dubbo:annotation package="com.itcast.demo.service.impl"/> </beans>
注意:dubbo:registry 要填寫本身的 zookeeper 所在的 linux 系統上的 ip 地址,端口號統一爲 2181。
dubbo:annotation 用於掃描 @Service 註解
個人地址爲: 192.168.25.128,本身 ip 地址是什麼就寫什麼
6)、測試運行
tomcat7:run
二、服務消費者開發
1)、建立 Maven 工程(war)dubbodemo-web,在 pom.xml 引入依賴,同 dobbodemo-service 工程。區別就是把 tomcat 插件的運行端口號改成 8082(改爲什麼都 能夠,只要不和 dubbodemo-service 同樣就行)
2)、在 webapps 目錄下建立 WEB-INF 目錄,並建立 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定加載的配置文件 ,經過參數contextConfigLocation加載--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
3)、拷貝業務接口
將 "dubbodemo-service" 工程的 com.itcast.dubbodemo.service 包以及下面的接口拷貝至此工程
4)、編寫 Controller
1 package com.itcast.demo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.ResponseBody; 6 7 import com.alibaba.dubbo.config.annotation.Reference; 8 import com.itcast.demo.service.UserService; 9 10 @Controller 11 @RequestMapping("/user") 12 public class UserController { 13 @Reference 14 private UserService userService; 15 16 @RequestMapping("/showName") 17 @ResponseBody 18 public String showName() { 19 return userService.getName(); 20 } 21 }
注意:此時裝配 UserService 時不用註解 "@Autowired",該註解是裝配本地的 UserService ,而咱們要裝配的是遠程的 UserSrevice,所以這裏使用 @Referenc。
5)、編寫 spring 配置文件
在 src/main/resources 下建立 applicationContext-web.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven > <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <dubbo:application name="dubboxdemo-web"/> <dubbo:registry address="zookeeper://192.168.25.128:2181"/> <dubbo:annotation package="com.itcast.demo.controller"/> </beans>
6)、測試運行
tomcat7:run
最後在瀏覽器輸入:http://localhost:8082/user/showName.do 查看結果
注意:要把 dubbodemo-service 和 dubbodemo-web 的服務都啓動起來,並確保 zookeeper 的服務爲開啓狀態。