框架 day88 濤濤商城專案(補)-soa架構及服務中介軟體Dubbo
1.1.1. 基於soa的架構
SOA:Service Oriented Architecture面向服務的架構。也就是把工程拆分成服務層、表現層兩個工程。服務層中包含業務邏輯,只需要對外提供服務即可。表現層只需要處理和頁面的互動,業務邏輯都是呼叫服務層的服務來實現。
淘淘商城系統架構
後臺工程搭建
Maven的常見打包方式:jar、war、pom
Pom工程一般都是父工程,管理jar包的版本、maven外掛的版本、統一的依賴管理。聚合工程。
Taotao-parent:父工程,打包方式pom,管理jar包的版本號。
| 專案中所有工程都應該繼承父工程。
|--Taotao-common:通用的工具類通用的pojo。打包方式jar
|--Taotao-manager:服務層工程。聚合工程。Pom工程
|--taotao-manager-dao:打包方式jar
|--taotao-manager-pojo:打包方式jar
|--taotao-manager-interface:打包方式jar
|--taotao-manager-service:打包方式:war
|--taotao-manager-web:表現層工程。打包方式war
1.1.2. 系統間通訊(dubbo)
分析
由於淘淘商城是基於soa的架構,表現層和服務層是不同的工程。所以要實現商品列表查詢需要兩個系統之間進行通訊。
如何實現遠端通訊?
1、Webservice:效率不高基於soap協議。專案中不推薦使用。
2、使用restful形式的服務:http+json。很多專案中應用。如果服務太多,服務之間呼叫關係混亂,需要治療服務。
3、使用dubbo。使用rpc協議進行遠端呼叫,直接使用socket通訊。傳輸效率高,並且可以統計出系統之間的呼叫關係、呼叫次數。
什麼是dubbo
隨著網際網路的發展,網站應用的規模不斷擴大,常規的垂直應用架構已無法應對,分散式服務架構以及流動計算架構勢在必行,亟需一個治理系統確保架構有條不紊的演進。
- 單一應用架構
- 當網站流量很小時,只需一個應用,將所有功能都部署在一起,以減少部署節點和成本。
- 此時,用於簡化增刪改查工作量的 資料訪問框架(ORM) 是關鍵。
- 垂直應用架構
- 當訪問量逐漸增大,單一應用增加機器帶來的加速度越來越小,將應用拆成互不相干的幾個應用,以提升效率。
- 此時,用於加速前端頁面開發的 Web框架(MVC) 是關鍵。
- 分散式服務架構
- 當垂直應用越來越多,應用之間互動不可避免,將核心業務抽取出來,作為獨立的服務,逐漸形成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。
- 此時,用於提高業務複用及整合的 分散式服務框架(RPC) 是關鍵。
- 流動計算架構
- 當服務越來越多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增加一個排程中心基於訪問壓力實時管理叢集容量,提高叢集利用率。
- 此時,用於提高機器利用率的 資源排程和治理中心(SOA) 是關鍵。
Dubbo就是資源排程和治理中心的管理工具。
Dubbo的架構
節點角色說明:
Provider: 暴露服務的服務提供方。
Consumer: 呼叫遠端服務的服務消費方。
Registry: 服務註冊與發現的註冊中心。
Monitor: 統計服務的呼叫次調和呼叫時間的監控中心。
Container: 服務執行容器。
呼叫關係說明:
0. 服務容器負責啟動,載入,執行服務提供者。
1. 服務提供者在啟動時,向註冊中心註冊自己提供的服務。
2. 服務消費者在啟動時,向註冊中心訂閱自己所需的服務。
3. 註冊中心返回服務提供者地址列表給消費者,如果有變更,註冊中心將基於長連線推送變更資料給消費者。
4. 服務消費者,從提供者地址列表中,基於軟負載均衡演算法,選一臺提供者進行呼叫,如果呼叫失敗,再選另一臺呼叫。
5. 服務消費者和提供者,在記憶體中累計呼叫次數和呼叫時間,定時每分鐘傳送一次統計資料到監控中心。
使用方法
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> |
註冊中心
註冊中心負責服務地址的註冊與查詢,相當於目錄服務,服務提供者和消費者只在啟動時與註冊中心互動,註冊中心不轉發請求,壓力較小。使用dubbo-2.3.3以上版本,建議使用zookeeper註冊中心。
Zookeeper是Apacahe Hadoop的子專案,是一個樹型的目錄服務,支援變更推送,適合作為Dubbo服務的註冊中心,工業強度較高,可用於生產環境,並推薦使用
Zookeeper的安裝:
第一步:安裝jdk
第二步:解壓縮zookeeper壓縮包
第三步:將conf資料夾下zoo_sample.cfg複製一份,改名為zoo.cfg
第四步:修改配置dataDir屬性,指定一個真實目錄
第五步:
啟動zookeeper:bin/zkServer.sh start
關閉zookeeper:bin/zkServer.sh stop
檢視zookeeper狀態:bin/zkServer.sh status
注意要關閉linux的防火牆。
1.1.3. 整合測試
需求
根據商品id查詢商品資訊,並將商品資訊使用json資料返回。
分析
請求的url:/item/{itemId}
引數:商品id,從請求的url中獲得
返回值:TbItem物件,逆向工程生成的pojo(響應json資料)
Dao層
根據商品id查詢商品資訊,單表查詢可以使用逆向工程生成的程式碼。
Service層
1、在taotao-manager-interface工程中建立一個ItemService介面
2、在taotao-manager-Service工程中建立一個itemSeviceImpl的實現類。
@Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Override public TbItem getItemById(long itemId) { TbItem tbItem = itemMapper.selectByPrimaryKey(itemId); return tbItem; } } |
釋出服務
1、在taotao-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="com.taotao.service"></context:component-scan> <!-- 使用dubbo釋出服務 --> <!-- 提供方應用資訊,用於計算依賴關係 --> <dubbo:application name="taotao-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="com.taotao.service.ItemService" ref="itemServiceImpl" /> </beans> |
引用服務
1、在taotao-manager-web工程中新增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、在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="com.taotao.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="taotao-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="com.taotao.service.ItemService" id="itemService" /> </beans> |
Controller
@Controller public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/item/{itemId}") @ResponseBody public TbItem getItemById(@PathVariable Long itemId) { //根據商品id查詢商品資訊 TbItem tbItem = itemService.getItemById(itemId); return tbItem; } } |
Dubbo監控中心
需要安裝tomcat,然後部署監控中心即可。
1、部署監控中心:
[[email protected] ~]# cpdubbo-admin-2.5.4.war apache-tomcat-7.0.47/webapps/dubbo-admin.war
2、啟動tomcat
3、訪問http://192.168.25.167:8080/dubbo-admin/
使用者名稱:root
密碼:root
如果監控中心和註冊中心在同一臺伺服器上,可以不需要任何配置。
如果不在同一臺伺服器,需要修改配置檔案:
/root/apache-tomcat-7.0.47/webapps/dubbo-admin/WEB-INF/dubbo.properties
Dubbo直連
dubbo-consumer.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo 3步 --> <!-- 服務消費方 名稱 --> <dubbo:application name="taotao-manager-web" /> <!-- 註冊中心 註冊地址 連線zookeeper <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/> --> <dubbo:registry address="N/A" /> <!-- 呼叫暴露介面 <dubbo:reference interface="com.itheima.core.service.buyer.SessionService" id="sessionService" url="127.0.0.1:20880" /> --> <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" url="127.0.0.1:20880" /> <!-- 使用debug跑 所以連線超時 我這裡使用10分鐘 啟動時不校驗服務提供方 --> <dubbo:consumer timeout="600000" check="false" /> </beans> |
dubbo-provider.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- dubbo 4步 --> <!-- 服務提供方 名稱 唯一標示 --> <dubbo:application name="taotao-manager-service"/> <!-- 註冊中心 註冊地址 連線zookeeper <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/> --> <dubbo:registry address="N/A"/> <!-- dubbo 主機 埠 --> <dubbo:protocol host="127.0.0.1" port="20880"/> <!-- 暴露介面 <dubbo:service interface="com.itheima.core.service.TestTbService" ref="testTbService"/> --> <dubbo:service interface="com.taotao.service.ItemService" ref="itemService"/> </beans> |
升級版逆向工程
mybatis:動態sql語句
逆向工程 缺點:
1.不能查詢動態sql語句(所有的欄位都查)
2.不支援分頁
3.pojo類沒有實現序列化介面
4.沒註釋
5.toString方法
6.可以改名為dao
7.pojo類查詢(query)
升級版逆向工程 以上都解決了