1. 程式人生 > >dubbo的使用(二)

dubbo的使用(二)

節點說明:

  • Provider:暴露服務的服務提供方
  • Container:服務執行容器
  • Registry:服務註冊於發現的註冊中心
  • Consumer:呼叫註冊於發現的註冊中心
  • Monitor:統計服務的呼叫次數和呼叫的監控中心

關係說明:

  • 0:服務容器負責啟動,載入,執行服務提供者
  • 1:服務提供者在啟動的時候,向註冊中心註冊自己提供的服務
  • 2:服務消費者在啟動的時候,想註冊中心訂閱自己所需要的服務
  • 3:註冊中心返回服務提供者的地址列表給消費者,如果有變更,註冊中心將基於長連線推送變更的資料給消費者
  • 4:服務消費方,從提供者地址列表中,基於軟負載的均衡演算法,選一臺提供者進行呼叫,如果呼叫失敗,再選另一臺呼叫
  • 5:服務消費者和提供者,在呢哦村中累計呼叫次數和呼叫的時間,定時每分鐘傳送一個統計資料到監控中心

連通性:

註冊中心負責服務地址的註冊於查詢,相當於目錄服務,服務提供者和消費者只在啟動的時候於註冊中心互動,註冊中心不轉發請求,壓力較小。

監控中心負責統計各服務呼叫的次數,呼叫的時間等,統計先在記憶體彙總後每分鐘傳送一次到監控中心伺服器,並以報表展示

服務提供者想註冊中心註冊其提供的服務,並彙報呼叫時間到監控中心,此時間不包含網路開銷

服務消費方在註冊中心獲取服務提供者地址列表,並根據負載短髮直接呼叫服務提供者,同時彙報呼叫時間到監控中心,此時間包含網路開銷的時間

註冊中心,服務提供者,服務消費者三者之間均屬於長連線,監控中心除外

註冊中心通過長連線感知服務提供者的存在,服務提供者宕機,註冊中心將立即推送事件通知消費者

註冊中心和監控中心全部宕機,不影響已執行的提供者和消費者,消費者在本地快取了提供者列表

註冊中心和監控中心都是可選的,服務消費者可以直姐連線到服務提供者

健壯性:

  • 監控中心宕機後不影響使用,只是丟失部分取樣資料
  • 資料庫宕機後,註冊中心仍能通過快取提供服務列表查詢,但不能註冊新的服務
  • 註冊中心對等叢集,任意一臺宕機後,將自動切換到另一臺
  • 註冊中心全部宕機後,服務提供者和服務消費者仍能通過本地快取通訊
  • 服務提供者無狀態,任意一臺宕機後,不影響使用
  • 服務提供者全部宕機後,服務消費者應用將無法使用,並無限次重連等待服務提供者恢復(可以設定重試連線的次數)

伸縮性:

  • 註冊中心為對等叢集,可動態增加機器部署例項,所有客戶端將自動發現新的 註冊中心
  • 服務提供者無狀態,可動態增加機器部署的例項,註冊中心將推送新的服務資訊給消費者

當zookeeper宕機後,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:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
    <dubbo:application name="${dubbo.name}" />
	<dubbo:registry protocol="zookeeper" address="${dubbo.url}" />
	
	<dubbo:protocol name="dubbo" port="-1" />
	<!-- rest協議 -->
    <dubbo:protocol name="rest" server="servlet" port="${rest.port}" contextpath="${rest.contextpath}"/>
    <!-- 該標籤為<dubbo:service>和<dubbo:protocol>標籤的預設值設定
      retries               ==> 服務呼叫超時時自動重試的次數
      timeout               = => 服務超時時間(單位:毫秒)
    -->
    <dubbo:provider retries="0" timeout="20000" protocol="dubbo" version="${dubbo.version1}" />
   	<!-- 該標籤為<dubbo:reference>標籤的預設值設定
      retries               ==> 服務呼叫超時時自動重試的次數
      timeout               ==> 服務超時時間(單位:毫秒)
    -->
    <dubbo:consumer retries="0" timeout="10000" version="${dubbo.version}" />
	
	
	
	<dubbo:reference id="cbService" interface="com.strongit.nj.czyd.sim.service.inter.shipinfo.ICbService" />

</beans>

對外提供的服務

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	<dubbo:service interface="com.strongit.nj.wjcz.ais.service.inter.BindingTransactionalService" protocol="dubbo" ref="bindingTransactionalServiceImpl" timeout="10000" />
		
	<dubbo:service interface="com.strongit.nj.wjcz.ais.service.inter.BindingQueryService" protocol="dubbo" ref="bindingQueryServiceImpl" timeout="10000" />
	
	<dubbo:service interface="com.strongit.nj.wjcz.rest.ILocationInfoService" protocol="rest" ref="locationInfoServiceImpl" timeout="5000" />
</beans>

消費方: 消費不能自己提供服務,但是可以引入服務

流程:釋出到註冊中心的名稱 --->  註冊中心的地址 ----- > 引入服務的地址  ---- > 引入相關的介面

<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
	
	<!-- 提供方應用資訊,用於計算依賴關係 -->
    <dubbo:application name="${appname}" />
	<!-- 註冊中心地址 -->
    <dubbo:registry protocol="zookeeper" address="${dubbo.address}" timeout="300000"/>
	
    <!-- 該標籤為<dubbo:reference>標籤的預設值設定
      retries               ==> 服務呼叫超時時自動重試的次數
      timeout               ==> 服務超時時間(單位:毫秒)
    -->
    <dubbo:consumer retries="0" timeout="100000" version="${dubbo.version}" />
    
		
	<dubbo:reference id="organiseService" interface="com.strongit.nj.czyd.bim.service.inter.organise.IOrgService" />

	<dubbo:reference id="yhService" interface="com.strongit.nj.czyd.bim.service.inter.user.IYhService" />

	<dubbo:reference id="permissionService" interface="com.strongit.nj.czyd.bim.service.inter.permission.IPermissionService" />

	<dubbo:reference id="uploadFileService"
					 interface="com.strongit.nj.czyd.bim.service.inter.uploadFile.IUploadFileService"/>
</beans>
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
	
	<dubbo:reference id="cbService" interface="com.strongit.nj.czyd.sim.service.inter.shipinfo.ICbService" />
	
	<dubbo:reference id="bindingTransactionalService" interface="com.strongit.nj.wjcz.ais.service.inter.BindingTransactionalService" />
		
	<dubbo:reference id="bindingQueryService" interface="com.strongit.nj.wjcz.ais.service.inter.BindingQueryService" />

	<dubbo:reference id="qyfxService" interface="com.strongit.nj.wjcz.qyfx.service.inter.IQyfxService" />
</beans>