Dubbo的使用及原理淺析.

Dubbo的使用及原理淺析.

Dubbo是什麼?

Dubbo是阿里巴巴SOA服務化治理方案的核心框架,每天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被廣泛應用於阿里巴巴集團的各成員站點。

Dubbo[]是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。


其核心部分包含:

  • 遠程通訊: 提供對多種基於長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。

  • 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。

  • 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查×××提供方,使地址透明,使服務提供方可以平滑增加或減少機器。

Dubbo能做什麼?

  • 透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何API侵入。

  • 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,降低成本,減少單點。

  • 服務自動註冊與發現,不再需要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,並且能夠平滑添加或刪除服務提供者。

Spring集成

Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基於Spring的Schema擴展進行加載。

上面簡單介紹了Dubbo的一些概念, 這裏再給一張圖來形象的形容下: 

我們用這張圖來形容 , 那麼映射到 項目中:
      當我們在多個Tomcat部署不同的系統時, 例如A系統(TomcatA)想調用B系統(TomcatB)中的服務, 這時Dubbo就有了用武之地. 首先我們需要B系統在註冊中心將自己的Url註冊進去, 然後註冊中心將Url返還給系統A, 那麼系統A就可以調用了. 當然了我這裏說的只是Dubbo的一種用法, 在這個項目中使用的也是Dubbo的遠程調用功能. (感覺真的和webservice有點像)

下面就步入正題, 看看Dubbo在項目中的使用實例:
1, 在linux下安裝Zookeeper
這個地方就不詳細概述Zookeeper的安裝了, 前面關於Linux的博文已經有講過在Linux下軟件的安裝了, 這裏安裝好後直接啓動 Zookeeper.


2, 使用需求
在這裏 當我們有一種需求, 我們需要在後臺(console)去對商品(product)做一些操做, 而這裏我們只能夠使用到公共的service方法, 那麼怎麼調用product中service的實現呢? 
項目結構:

公共service方法:
TestTbService.java:

複製代碼

1 package cn.itcast.core.service;2 3 import cn.itcast.core.bean.TestTb;4 5 public interface TestTbService {6     public void insertTestTb(TestTb testTb);7 }

複製代碼





product中的service實現類:
TestTbService.java:

複製代碼

 1 package cn.itcast.core.service; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 import org.springframework.transaction.annotation.Transactional; 6  7 import cn.itcast.core.bean.TestTb; 8 import cn.itcast.core.dao.TestTbDao; 9 10 @Service("testTbService")11 @Transactional12 public class TestTbServiceImpl implements TestTbService {13 14     @Autowired15     private TestTbDao testTbDao;16     17     //保存18     public void insertTestTb(TestTb testTb){19         testTbDao.insertTestTb(testTb);20     }21 }

複製代碼



在console中使用product中的service實現類:
CenterController.java:

複製代碼

 1 package cn.itcast.core.controller; 2  3 import java.util.Date; 4  5 import org.junit.runners.model.TestTimedOutException; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.ui.Model; 9 import org.springframework.web.bind.annotation.RequestMapping;10 11 import cn.itcast.core.bean.TestTb;12 import cn.itcast.core.service.TestTbService;13 14 @Controller15 public class CenterController {16     17     @Autowired18     private TestTbService testTbService;19     20     //測試21     @RequestMapping(value = "/test/index.do")22     public void index(Model model){23         24         TestTb testTb = new TestTb();25         testTb.setName("范冰冰");26         testTb.setBirthday(new Date());27         28         testTbService.insertTestTb(testTb);29         30     }31 }

複製代碼

 

如果這樣直接調用能夠行的通嗎?  當然是不行的, 在console裏面定義的只是service方法, 那麼這裏是怎麼直接調用到了product中的service實現類呢?

當然了, 這裏就需要一些配置文件了:
首先是需要在product中註冊服務:
dubbo-provider.xml:

複製代碼

 1 <beans xmlns="http://www.springframework.org/schema/beans" 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3     xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:aop="http://www.springframework.org/schema/aop"  5     xmlns:tx="http://www.springframework.org/schema/tx" 6     xmlns:task="http://www.springframework.org/schema/task" 7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
12         http://www.springframework.org/schema/context 
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
14         http://www.springframework.org/schema/aop 
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
16         http://www.springframework.org/schema/tx 
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd18         http://www.springframework.org/schema/task19            http://www.springframework.org/schema/task/spring-task-4.0.xsd20         http://code.alibabatech.com/schema/dubbo        
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">22         23         24         <!-- 整合Dubbo -->25         <!-- 第一步:Dubbo起名稱    計算用此名稱來區分   -->26         <dubbo:application name="babasport-service-product"/>27         <!-- 第二步:中介  註冊中心: zookeeper  redis ... -->28         <!-- <dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->29         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>30         <!-- 第三步:設置dubbo的端口號     192.168.40.88:20880/接口  -->31         <dubbo:protocol name="dubbo" port="20880"/>32         <!-- 第四步:設置服務提供方 提供的接口 -->33         <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>34         35 </beans>

複製代碼

接下來就是在console中使用了:
服務消費方:
dubbo-cusmer.xml:

複製代碼

 1 <beans xmlns="http://www.springframework.org/schema/beans" 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 3     xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:aop="http://www.springframework.org/schema/aop"  5     xmlns:tx="http://www.springframework.org/schema/tx" 6     xmlns:task="http://www.springframework.org/schema/task" 7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
12         http://www.springframework.org/schema/context 
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
14         http://www.springframework.org/schema/aop 
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
16         http://www.springframework.org/schema/tx 
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd18         http://www.springframework.org/schema/task19            http://www.springframework.org/schema/task/spring-task-4.0.xsd20         http://code.alibabatech.com/schema/dubbo        
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">22         23         <!-- 整合Dubbo -->24         <!-- 第一步:Dubbo起名稱    計算用此名稱來區分   -->25         <dubbo:application name="babasport-console"/>26         <!-- 第二步:中介  註冊中心    zookeeper  redis ... -->27         <!--<dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->28         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>29         <!-- 第三步:調用服務提供方 提供的接口 -->30         <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>31         32 </beans>

複製代碼



剩下的就是啓動服務了:
注意先啓動服務提供方, 然後再啓動服務消費方.

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章