SOA的架構是將表現層和服務層分成不同的工程,這時候要實現商品列表的查詢需要兩個系統之間的進行通訊。
實現遠程通訊的方法:
1、webservice:效率不高基於soap協議,項目中不建議使用。
2、使用restful形式的服務:HTTP+json。很多項目中應用,如果服務太多,服務之間調用關係混亂,需要治療服務。
3、使用dubbo。使用rpc協議進行遠程調用,直接使用socket通訊。傳輸效率高,並且可以統計出系統之間的調用關係、調用次數。
開發網站中架構的舉例:
- 單一應用架構
- 當網站流量很小時,只需一個應用,將所有功能都部署在一起,以減少部署節點和成本。
- 此時,用於簡化增刪改查工作量的 數據訪問框架(ORM) 是關鍵。
- 垂直應用架構
- 當訪問量逐漸增大,單一應用增加機器帶來的加速度越來越小,將應用拆成互不相干的幾個應用,以提升效率。
- 此時,用於加速前端頁面開發的 Web框架(MVC) 是關鍵。
- 分佈式服務架構
- 當垂直應用越來越多,應用之間交互不可避免,將核心業務抽取出來,作爲獨立的服務,逐漸形成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。
- 此時,用於提高業務複用及整合的 分佈式服務框架(RPC) 是關鍵。
- 流動計算架構
- 當服務越來越多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增加一個調度中心基於訪問壓力實時管理集羣容量,提高集羣利用率。
- 此時,用於提高機器利用率的 資源調度和治理中心(SOA) 是關鍵。
1、Dubbo的架構
Dubbo就是資源調度和治理中心的管理工具。
節點角色說明:
- Provider: 暴露服務的服務提供方。
- Consumer: 調用遠程服務的服務消費方。
- Registry: 服務註冊與發現的註冊中心。
- Monitor: 統計服務的調用次調和調用時間的監控中心。
- Container: 服務運行容器。
調用關係說明:
- 0. 服務容器負責啓動,加載,運行服務提供者。
- 1. 服務提供者在啓動時,向註冊中心註冊自己提供的服務。
- 2. 服務消費者在啓動時,向註冊中心訂閱自己所需的服務。
- 3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連接推送變更數據給消費者。
- 4. 服務消費者,從提供者地址列表中,基於軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選另一臺調用。
- 5. 服務消費者和提供者,在內存中累計調用次數和調用時間,定時每分鐘發送一次統計數據到監控中心。
2、使用方法
Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基於Spring的Schema擴展進行加載。
單一工程中spring的配置
<bean id="xxxService" class="com.xxx.XxxServiceImpl" /> <bean id="xxxAction" class="com.xxx.XxxAction"> <property name="xxxService" ref="xxxService" /> </bean> |
遠程服務:
在本地服務的基礎上,只需做簡單配置,即可完成遠程化:
將上面的local.xml配置拆分成兩份,將服務定義部分放在服務提供方remote-provider.xml,將服務引用部分放在服務消費方remote-consumer.xml。
並在提供方增加暴露服務配置<dubbo:service>,在消費方增加引用服務配置<dubbo:reference>。
發佈服務:
<!-- 和本地服務一樣實現遠程服務 --> <bean id="xxxService" class="com.xxx.XxxServiceImpl" /> <!-- 增加暴露遠程服務配置 --> <dubbo:service interface="com.xxx.XxxService" ref="xxxService" /> |
調用服務:
<!-- 增加引用遠程服務配置 --> <dubbo:reference id="xxxService" interface="com.xxx.XxxService" /> <!-- 和本地服務一樣使用遠程服務 --> <bean id="xxxAction" class="com.xxx.XxxAction"> <property name="xxxService" ref="xxxService" /> </bean> |
3、zookeeper註冊中心
官方推薦使用zookeeper註冊中心。
註冊中心負責服務地址的註冊與查找,相當於目錄服務,服務提供者和消費者只在啓動時與註冊中心交互,註冊中心不轉發請求,壓力較小。使用dubbo-2.3.3以上版本,建議使用zookeeper註冊中心。
Zookeeper是Apacahe Hadoop的子項目,是一個樹型的目錄服務,支持變更推送,適合作爲Dubbo服務的註冊中心,工業強度較高,可用於生產環境,並推薦使用
Zookeeper:
- 可以作爲集羣的管理工具使用。
- 可以集中管理配置文件。
3.1 Zookeeper的安裝(詳細步驟https://blog.csdn.net/qq_18142501/article/details/80849879)
安裝環境:
Linux:centos6.4
Jdk:1.7以上版本
Zookeeper是java開發的可以運行在windows、linux環境。需要先安裝jdk。
安裝步驟:
第一步:安裝jdk
第二步:把zookeeper的壓縮包上傳到linux系統。
第三步:解壓縮壓縮包
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:進入zookeeper-3.4.6目錄,創建data文件夾。
第五步:把zoo_sample.cfg改名爲zoo.cfg
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
第六步:修改data屬性:dataDir=/root/zookeeper-3.4.6/data
第七步:啓動zookeeper
[root@localhost bin]# ./zkServer.sh start
關閉:[root@localhost bin]# ./zkServer.sh stop
查看狀態:[root@localhost bin]# ./zkServer.sh status
注意:需要關閉防火牆https://blog.csdn.net/qq_18142501/article/details/80359940。
實例
- SOA工程改造
- 拆分工程
1)將表現層工程獨立出來:
e3-manager-web
2)將原來的e3-manager改爲如下結構
e3-manager
|--e3-manager-dao
|--e3-manager-interface
|--e3-manager-pojo
|--e3-manager-service(打包方式改爲war)
服務層工程
第一步:把e3-manager的pom文件中刪除e3-manager-web模塊。
第二步:把e3-manager-web文件夾移動到e3-manager同一級目錄。
第三步:e3-manager-service的pom文件修改打包方式
<packaging>war</packaging>
第四步:在e3-manager-service工程中添加web.xml文件
第五步:把e3-manager-web的配置文件複製到e3-manager-service中。
刪除springmvc.xml
第六步:web.xml 中只配置spring容器。刪除前端控制器
第七步:發佈服務
- 在e3-manager-Service工程中添加dubbo依賴的jar包。
<!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> |
2、在spring的配置文件中添加dubbo的約束,然後使用dubbo:service發佈服務。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="cn.e3mall.service"></context:component-scan>
<!-- 使用dubbo發佈服務 --> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="e3-manager" /> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" />
</beans> |
表現層工程
改造e3-manager-web工程。
第一步:刪除mybatis、和spring的配置文件。只保留springmvc.xml
第二步:修改e3-manager-web的pom文件,
- 修改parent爲e3-parent
- 添加spring和springmvc的jar包的依賴
- 刪除e3-mangager-service的依賴
- 添加dubbo的依賴
<!-- dubbo相關 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> |
5、e3-mangager-web添加對e3-manager-Interface的依賴。
第三步:修改springmvc.xml,在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-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd">
<context:component-scan base-package="cn.e3mall.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
<!-- 引用dubbo服務 --> <dubbo:application name="e3-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/> <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
</beans> |
第四步:在e3-manager-web工程中添加tomcat插件配置。
<build> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <path>/</path> <port>8081</port> </configuration> </plugin> </plugins> </build> |