dubbo結合zookeeper的使用

dubbo是分佈式管理框架,主要用於管理分佈式系統.zookeeper是分佈式協調的治理工具。

dubbo是什麼?

  • 一款分佈式服務框架
  • 高性能和透明化的RPC遠程服務調用方案
  • SOA服務治理方案

dubbo架構圖

dubbo結合zookeeper的使用

Provider:服務提供方

Consumer:服務消費者

Registry:註冊中心

Monitor:統計服務調用次數和調用時間的監控中心

調用流程:

0 . 啟動服務

1. 服務提供者啟動的時候, 向註冊中心提供自己的服務

2.服務消費者啟動的時候,向註冊中心訂閱自己的服務

3.註冊中心將服務提供者地址列表返回給服務消費者,如果有變更,註冊中心將基於長變更變更數據返回給消費者

4.服務消費者,基於軟負載均衡算法,選一臺提供者進行調用,如果調用失敗,再選擇另外一臺服務提供者進行調用.

5.服務消費者和服務提供者, 在內容中累計調用次數和調用時間, 定時每分鐘發送一次統計數據到監控臺中心.

zookeeper註冊中心

上述途中所或的Register就是註冊中心,dubbo官方推薦的註冊中心是zookeeper

服務提供方: 在註冊中心發佈服務

服務消費方: 在註冊中心訂閱服務

dubbo+zookeeper+SSM框架的配合使用

1.創建maven工程

dubbo結合zookeeper的使用

dubbo : dubbo父類工程, 主要是用來管理所有的jar版本

dubbo-api : dubbo工程所有的接口

dubbo-consumer : dubbo 消費者

dubbo-provider : dubbo提供者 , 所有公共接口的實現

dubbo-consumer-user : dubbo 消費用戶

2. 創建服務方

2.1 服務方接口


  1. package com.provider;
  2. public interface DemoService {
  3. String sayHello(String name);
  4. }

2.2服務方接口實現類


  1. package com.provider;
  2. import org.springframework.stereotype.Service;
  3. @Service(value="demoService")
  4. public class DemoServiceImpl implements DemoService{
  5. public String sayHello(String name) {
  6. return "Hello Dubbo,Hello " + name;
  7. }
  8. }

2.3 applicationContext.xml


  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xmlns:aop="http://www.springframework.org/schema/aop"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
  13. >

2.4 dubbo-provider.xml


  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans.xsd
  5. http://code.alibabatech.com/schema/dubbo
  6. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


分享到:


相關文章: