一:簡介
Dubbox 是一個分佈式服務框架,其前身是阿里巴巴開源項目Dubbo ,被國內電商及互聯網項目中使用,後期阿里巴巴中止了該項目的維護,噹噹網便在Dubbo基礎上進行優化,並繼續維護,爲了與原有的Dubbo區分,故將其命名爲Dubbox。
Dubbox 致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。說白了就是個遠程服務調用的分佈式框架。
節點角色說明:
• Provider: 暴露服務的服務提供方。
• Consumer: 調用遠程服務的服務消費方。
• Registry: 服務註冊與發現的註冊中心。
• Monitor: 統計服務的調用次調和調用時間的監控中心。
• Container: 服務運行容器。
調用關係說明:
• 0. 服務容器負責啓動,加載,運行服務提供者。
• 1. 服務提供者在啓動時,向註冊中心註冊本身提供的服務。
• 2. 服務消費者在啓動時,向註冊中心訂閱本身所需的服務。
• 3. 註冊中心返回服務提供者地址列表給消費者,若是有變動,註冊中心將基於長鏈接推
送變動數據給消費者。
• 4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,
若是調用失敗,再選另外一臺調用。
• 5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計
數據到監控中心。java
一:簡介
Zookeeper 是 Apacahe Hadoop 的子項目,是一個樹型的目錄服務,適合做爲Dubbox 服務的註冊中心,註冊中心負責服務地址的註冊與查找,至關於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。
二: Zookeeper 在Linux系統的安裝linux
tar -zxvf zookeeper-3.4.6.tar.gzgit
mkdir datagithub
mv zoo_sample.cfg zoo.cfgweb
dataDir=/root/zookeeper-3.4.6/data算法
三:Zookeeper 服務啓動
進入bin目錄,啓動服務輸入命令spring
./zkServer.sh startapache
一:建立服務提供者的Maven工程
1:pom文件瀏覽器
<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>
2:web.xmlspring-mvc
<?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:Spring的配置文件
<?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"/> <!-- dubbox的包掃描 --> <dubbo:annotation package="com.zgz.demo.serviceImpl" /> </beans>
4:代碼及目錄結構
二:建立服務消費者的Maven工程
1:pom文件和上面的一致
2: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:SpringMvc配置文件
<?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"/> <!-- dubbox的包掃描 --> <dubbo:annotation package="com.zgz.demo.controller" /> </beans>
4:代碼及目錄結構
三:管理中心的部署
咱們在開發時,須要知道註冊中心都註冊了哪些服務,以便咱們開發和測試。咱們能夠經過部署一個管理中心來實現。其實管理中心就是一個web應用,部署到tomcat便可。
(1)編譯源碼,獲得war包在命令符下進入dubbo-admin目錄 ,輸入maven命令
mvn package -Dmaven.skip.test=true
(2)進入target文件夾,你會看到一個dubbo-admin-2.8.4.war , 在linux服務器上安裝tomcat, 將此war包上傳到linux服務器的tomcat的webapps下。啓動tomcat後自動解壓。
(3)若是你部署在zookeeper同一臺主機而且端口是默認的2181,則無需修改任何配置。若是不是在一臺主機上或端口被修改,須要修改WEB-INF下的dubbo.properties ,修改以下配置,修改後從新啓動tomcat
dubbo.registry.address=zookeeper://127.0.0.1:2181
四:管理中心的使用
打開瀏覽器,輸入http://192.168.25.128:8080/dubbo-admin ,登陸用戶名和密碼均爲root 進入首頁。
五:啓動demo
先啓動服務提供方的maven工程,在啓動服務消費方的maven工程。(啓動方法見下圖)注意zookeeper的服務要提早啓動,直接輸入服務消費方的地址,看到結果。