一:簡介:前身是阿裡巴巴的一個開源的項目,後來停止維護,由噹噹網繼續維護,它致力於rpc遠程的調度方案.是一個服務框架 二:執行原理圖: 節點角色說明: · Provider: 暴露服務的服務提供方。 · Consumer: 調用遠程服務的服務消費方。 · Registry: 服務註冊與發現的註冊中 ...
一:簡介:前身是阿裡巴巴的一個開源的項目,後來停止維護,由噹噹網繼續維護,它致力於rpc遠程的調度方案.是一個服務框架
二:執行原理圖:
節點角色說明:
· Provider: 暴露服務的服務提供方。
· Consumer: 調用遠程服務的服務消費方。
· Registry: 服務註冊與發現的註冊中心。
· Monitor: 統計服務的調用次調和調用時間的監控中心。
· Container: 服務運行容器。
調用關係說明:
· 0. 服務容器負責啟動,載入,運行服務提供者。
· 1. 服務提供者在啟動時,向註冊中心註冊自己提供的服務。
· 2. 服務消費者在啟動時,向註冊中心訂閱自己所需的服務。
· 3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推
送變更數據給消費者。
· 4. 服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行調用,
如果調用失敗,再選另一臺調用。
· 5. 服務消費者和提供者,在記憶體中累計調用次數和調用時間,定時每分鐘發送一次統計
數據到監控中心。
三:使用
1.建議使用zookper註冊中心(zookper是Apacahe 的子項目)安裝到linux系統上
2.Dubbox本地jar包部署,maven中央倉庫沒有需要手動安裝
先將dubbo-2.8.4.jar包放到d:\setup, 然後輸入命令(jar包自行下載)
mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar |
3.入門demo
1.服務提供者,創建Maven工程(WAR)dubboxdemo-service ,在pom.xml中引入依賴
<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>
(2)在工程的webapps下創建WEB-INF文件夾,創建web.xml
<!-- 載入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>
(3)創建業務介面
創建包cn.itcast.dubbodemo.service,用於存放業務介面,創建介面
(4)創建業務實現類
創建包cn.itcast.dubbodemo.service.impl ,用於存放業務實現類。創建業務實現類:
註意:Service註解與原來不同,需要引入com.alibaba包下的
(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.132:2181"/><!--虛擬機上的ip--> <dubbo:annotation package="cn.itcast.dubboxdemo.service" /> </beans> |
註意:dubbo:annotation用於掃描@Service註解
2.服務消費者開發
創建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依賴 ,同“dubboxdemo-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:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> |
(3)拷貝業務介面
將“dubboxdemo-service”工程的cn.itcast.dubboxdemo.service 包以及下麵的介面拷貝至此工程。
(4)編寫Controller
(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 服務 --> <dubbo:application name="dubboxdemo-web" /> <dubbo:registry address="zookeeper://192.168.25.132:2181"/> <dubbo:annotation package="cn.itcast.dubboxdemo.controller" /> </beans> |
接下來測試運行
3.6管理中心的部署(百度一下看看怎麼使用安裝部署)