1. 程式人生 > >dubbo/dubbox 和SpringMVC 使用

dubbo/dubbox 和SpringMVC 使用

我在學習的時候,打算是先學習dubbo 然後再學習dubbox,可是網上只說了 兩者的區別只是在於dubbox支援rest方式呼叫,其他的說的不明確。 我根據我自己的理解就是 阿里的dubbo 版本最高是 2.5.3 後面就不再更新了。而噹噹網接下來之後,就是再阿里的版本上更新了。最高版本是2.8.4。JAR包同名只是版本號不一樣而已。其他沒什麼了。

隨著網際網路的發展,網站應用的規模不斷擴大,常規的垂直應用架構已無法應對,分散式服務架構以及流動計算架構勢在必行,亟需一個治理系統確保架構有條不紊的演進。 這裡寫圖片描述 單一應用架構 當網站流量很小時,只需一個應用,將所有功能都部署在一起,以減少部署節點和成本。 此時,用於簡化增刪改查工作量的 資料訪問框架(ORM) 是關鍵。 垂直應用架構 當訪問量逐漸增大,單一應用增加機器帶來的加速度越來越小,將應用拆成互不相干的幾個應用,以提升效率。 此時,用於加速前端頁面開發的 Web框架(MVC) 是關鍵。 分散式服務架構 當垂直應用越來越多,應用之間互動不可避免,將核心業務抽取出來,作為獨立的服務,逐漸形成穩定的服務中心,使前端應用能更快速的響應多變的市場需求。 此時,用於提高業務複用及整合的 分散式服務框架(RPC) 是關鍵。 流動計算架構 當服務越來越多,容量的評估,小服務資源的浪費等問題逐漸顯現,此時需增加一個排程中心基於訪問壓力實時管理叢集容量,提高叢集利用率。 此時,用於提高機器利用率的 資源排程和治理中心(SOA) 是關鍵。

一直在埋頭碼程式碼。好久之前聽說dubbo這個東西好用。又聽說dubbox也很好用。現在終於有點時間來弄。 先說dubbo。網上找了幾篇 dubbo的使用。目前dubbo 都是和 zookeeper一起配合使用的。所以這裡我也使用了zookeeper。 其中 zookeeper是可以做成叢集模式的 1.什麼是zookeeper

  ZooKeeper是一個分散式的,開放原始碼的分散式應用程式協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要元件。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、名字服務、分散式同步、組服務等。 ZooKeeper的目標就是封裝好複雜易出錯的關鍵服務,將簡單易用的介面和效能高效、功能穩定的系統提供給使用者。 ZooKeeper包含一個簡單的原語集,[1] 提供Java和C的介面。 ZooKeeper程式碼版本中,提供了分散式獨享鎖、選舉、佇列的介面,程式碼在zookeeper-3.4.3\src\recipes。其中分佈鎖和佇列有Java和C兩個版本,選舉只有Java版本。 2.zookeeper的原理   ZooKeeper是以Fast Paxos演算法為基礎的,paxos演算法存在活鎖的 問題,即當有多個proposer交錯提交時,有可能互相排斥導致沒有一個proposer能提交成功,而Fast Paxos作了一些優化,通過選舉產生一個leader,只有leader才能提交propose,具體演算法可見Fast Paxos。因此,要想弄懂ZooKeeper首先得對Fast Paxos有所瞭解。[3]

ZooKeeper的基本運轉流程:   1、選舉Leader。   2、同步資料。   3、選舉Leader過程中演算法有很多,但要達到的選舉標準是一致的。   4、Leader要具有最高的zxid。   5、叢集中大多數的機器得到響應並follow選出的Leader 3.zookeeper的特點    在Zookeeper中,znode是一個跟Unix檔案系統路徑相似的節點,可以往這個節點儲存或獲取資料。如果在建立znode時Flag設定為 EPHEMERAL,那麼當建立這個znode的節點和Zookeeper失去連線後,這個znode將不再存在在Zookeeper 裡,Zookeeper使用Watcher察覺事件資訊。當客戶端接收到事件資訊,比如連線超時、節點資料改變、子節點改變,可以呼叫相應的行為來處理數 據。Zookeeper的Wiki頁面展示瞭如何使用Zookeeper來處理事件通知,佇列,優先佇列,鎖,共享鎖,可撤銷的共享鎖,兩階段提交。 那麼Zookeeper能作什麼事情呢,簡單的例子:假設我們有20個搜尋引擎的伺服器(每 個負責總索引中的一部分的搜尋任務)和一個總伺服器(負責向這20個搜尋引擎的伺服器發出搜尋請求併合並結果集),一個備用的總伺服器(負責當總伺服器宕 機時替換總伺服器),一個web的cgi(向總伺服器發出搜尋請求)。搜尋引擎的伺服器中的15個伺服器提供搜尋服務,5個伺服器正在生成索引。這20個 搜尋引擎的伺服器經常要讓正在提供搜尋服務的伺服器停止提供服務開始生成索引,或生成索引的伺服器已經把索引生成完成可以搜尋提供服務了。使用 Zookeeper可以保證總伺服器自動感知有多少提供搜尋引擎的伺服器並向這些伺服器發出搜尋請求,當總伺服器宕機時自動啟用備用的總伺服器。

1.zookeeper

我官網下載的是zookeeper是 3.4.9 版本,據說比較穩定 下載的是 zookeeper-3.4.9.tar.gz 解壓 tar -zxvf zookeeper-3.4.9.tar.gz 進入conf 目錄裡面有一個 zoo_sample.cfg 檔案。 複製一份 重新命名為zoo.cfg 必須這麼做。

[[email protected] conf]# cat  zoo.cfg 
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
## 資料資料夾,自定義,資料夾一定要存在
dataDir=/home/zk/data
## 日誌資料夾,原文中可能沒有,自定義 資料夾一定要存在
dataLogDir=/home/zk/log
# the port at which the clients will connect
## 開啟埠號
clientPort=2181

## if you have many server 多服務叢集模式 後面的3888埠是 提供服務埠
#server.1=192.168.23.200:2888:3888
#server.2=192.168.23.211:2888:3888
#server.3=192.168.23.212:2888:3888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

server.1=192.168.23.200:2888:3888 server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號伺服器;B 是這個伺服器的 ip 地址;C 表示的是這個伺服器與叢集中的 Leader 伺服器交換資訊的埠;D 表示的是萬一叢集中的 Leader 伺服器掛了,需要一個埠來重新進行選舉,選出一個新的 Leader,而這個埠就是用來執行選舉時伺服器相互通訊的埠。如果是偽叢集的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 例項通訊埠號不能一樣,所以要給它們分配不同的埠號。

配置好了之後就啟動。 cd /bin ./zkServer.sh start 啟動 然後檢視狀態

 ./zkServer.sh  status
 ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.9/bin/../conf/zoo.cfg
Mode: standalone
[root@localhost bin]# 
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到mode standalone 表示是單機模式 如果是叢集,檢視狀態可能是 follow (副機) 或者是 Leader(主機) 如果是叢集方式配置

[root@leon127 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader

[root@leon146 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower

[root@leon67 bin]# ./zkServer.sh  status
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

如果是叢集配置,還是需要在配置的資料檔案中dataDir=/home/zk/data 建立myid檔案 在dataDir目錄下建立一個myid檔案,然後分別在myid檔案中按照zoo.cfg檔案的server.A中A的數值,在不同機器上的該檔案中填寫相應的值。

2.springMVC配置

服務提供者 ,在spring配置檔案中 整合一個新的spring-dubbo 配置檔案

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://code.alibabatech.com/schema/dubbo
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
    >
    <!-- 提供方應用資訊,用於計算依賴關係 --> 
    <dubbo:application name="hehe_provider" />
    <!-- 使用zookeeper註冊中心暴露服務地址   埠是zookeeper 中配置的2181-->
    <dubbo:registry address="zookeeper://192.168.23.212:2181"/>
    <!-- 用dubbo協議在20880埠暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

     <!-- 服務者與消費者的預設配置 -->  
    <!-- 延遲到Spring初始化完成後,再暴露服務,服務呼叫超時設定為6秒,超時不重試   -->    
    <dubbo:provider delay="-1" timeout="6000" retries="0"/>  
    <dubbo:consumer timeout="6000" retries="0"/>  

    <!-- 宣告需要暴露的服務介面 -->
    <dubbo:service interface="com.smk.alilife.service.AliTestService" ref="aliTestService"  timeout="3000" protocol="dubbo" />
     <!-- 要引用的服務 -->  
  <!--  <dubbo:reference interface="cn.test.dubbo.registry.service.TestRegistryService" id="testRegistryService" /> -->
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

服務消費者 ,在spring配置檔案中 整合一個新的spring-dubbo 配置檔案

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
     http://code.alibabatech.com/schema/dubbo
     http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
    >

    <dubbo:annotation package="com.smk.ac.action.ext"/>

    <!-- 提供方應用資訊,用於計算依賴關係 --> 
    <dubbo:application name="hehe_provider" />
    <!-- 使用zookeeper註冊中心暴露服務地址   埠是zookeeper 中配置的2181-->
    <dubbo:registry address="zookeeper://192.168.23.212:2181"/>
    <!-- 用dubbo協議在20880埠暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

     <!-- 服務者與消費者的預設配置 -->  
    <!-- 延遲到Spring初始化完成後,再暴露服務,服務呼叫超時設定為6秒,超時不重試   -->    
    <dubbo:provider delay="-1" timeout="6000" retries="0"/>  
    <dubbo:consumer timeout="6000" retries="0"/>  

    <!-- 宣告需要暴露的服務介面 
    <dubbo:service interface="com.smk.alilife.service.AliTestService" ref="aliTestService"  timeout="3000" protocol="dubbo" />-->
     <!-- 要引用的服務 -->  
    <dubbo:reference interface="com.smk.alilife.service.AliTestService" id="aliTestService" /> 
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

看消費者使用的程式碼

package com.smk.ac.action.ext.vircard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cat.common.bean.EMsg;
import com.cat.common.bean.FMsgResponse;
import com.smk.ac.action.ext.base.BaseAction;
import com.smk.alilife.bean.ActAppChannel;
import com.smk.alilife.service.AliTestService;

@Controller
@Scope("prototype")
public class VirtualCardAction extends BaseAction{

    //像引用本地service一樣使用就可以
    @Autowired
    private AliTestService aliTestService;

   /**
    * 獲取卡列表
    * @return
    */
   @RequestMapping(value ="dubbo/test")
   @ResponseBody
   public FMsgResponse getVirCardList(){
      try{
          ActAppChannel s =  aliTestService.getBean("123123");
         return new FMsgResponse(EMsg.Success.code(), EMsg.Success.value(),s);
      }catch(Exception e){
         e.printStackTrace();
         return new FMsgResponse(EMsg.Fail.code(), EMsg.Fail.value());
      }
   }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

由此可以看出 十分方便。但是有一個就是service JAR包 需要單獨打包提供出去。消費者和服務都要引用用的。

服務提供者 提供service的實現類,所以需要該JAR包 消費者需要使用service 。所以也需要該JAR包

dubbox 的使用