網絡協議-dubbo協議

Dubbo支持dubbo、rmi、hessian、http、webservice、thrift、redis等多種協議,可是Dubbo官網是推薦咱們使用Dubbo協議的。html

下面咱們就針對Dubbo的每種協議詳解講解,以便咱們在實際應用中可以正確取捨。java

 

1.各協議的比較

 

協議名稱 實現描述 鏈接 使用場景
dubbo 傳輸:mina、netty、grizzy

序列化:dubbo、hessian二、java、json
    dubbo缺省採用單一長鏈接和NIO異步通信    1.傳入傳出參數數據包較小

2.消費者 比提供者多

3.常規遠程服務方法調用

4.不適合傳送大數據量的服務,好比文件、傳視頻
rmi 傳輸:java  rmi

序列化:java 標準序列化
    

鏈接個數:多鏈接

鏈接方式:短鏈接

傳輸協議:TCP/IP

傳輸方式:BIO

1.常規RPC調用

2.與原RMI客戶端互操做

3.可傳文件

4.不支持防火牆穿透
hessian
傳輸:Serverlet容器

序列化:hessian二進制序列化
   

    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸

   

1.提供者比消費者多

2.可傳文件

3.跨語言傳輸
http
傳輸:servlet容器

序列化:表單序列化
    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸
1.提供者多餘消費者

2.數據包混合
webservice
傳輸:HTTP

序列化:SOAP文件序列化
    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸

1.系統集成

2.跨語言調用
thrift
    與thrift RPC實現集成,並在基礎上修改了報文頭   

長鏈接、NIO異步傳輸   
 

 

二、協議的配置:<dubbo:protocal>(只需在服務提供方配置便可)

 

屬性 類型 是否必填 缺省值 描述
name string 必填 dubbo 協議名稱
port int 可選

dubbo協議缺省端口爲20880,web

rmi協議缺省端口爲1099,面試

http和hessian協議缺省端口爲80;redis

若是配置爲-1或者沒有配置port,則spring

會分配一個沒有被佔用的端口。apache

服務端口
threadpool string 可選 fixed 線程池類型,可選:fixed/cached
threads int 可選 100 服務h定大小)
iothreads int 可選 CPU個數+1 io線程池大小(固定)
accepts int 可選 0 服務提供方最大可接受鏈接數
serialization string 可選

dubbo協議缺省爲hessian2,json

rmi缺省協議爲java,api

http協議缺省爲json瀏覽器

可填序列化:dubbo|hessian2|

java|compactedjava|fastjson

dispatcher string 可選 dubbo協議缺省爲all

協議的消息派發方式,用於指定線程模型,

好比:dubbo協議的all, direct, message,

execution, connection等

參考:https://blog.csdn.net/fd2025/article/

details/79985542

queues int 可選 0

線程池隊列大小,當線程池滿時,

排隊等待執行的隊列大小,建議不要設置,

當線程程池時應當即失敗,重試其它服務

提供機器,而不是排隊,除非有特殊需求。

charset string 可選 UTF-8 序列化編碼
server server string

dubbo協議缺省爲netty,

http協議缺省爲servlet

hessian協議缺省爲jetty

協議的服務器端實現類型,

好比:dubbo協議的mina,netty等,

http協議的jetty,servlet等

 
1    <dubbo:protocol name="hessian" server="jetty"
2                     client="httpclient"
3                     port="20880"
4                     threadpool="fixed"
5                     threads="50"
6                      iothreads="5"
7                     accepts="1000"/>
 

協議對應的源碼:

 

dubbo 協議:com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol

RMI協議: com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol

Http協議:com.alibaba.dubbo.rpc.protocol.http.HttpProtocol.InternalHandler

Hessian協議 :com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol.HessianHandler

 

 

 

dubbo多鏈接配置:

Dubbo 協議缺省每服務每提供者每消費者使用單一長鏈接,若是數據量較大,可使用多個鏈接。

1 <dubbo:service connections="1"/>
2 <dubbo:reference connections="1"/>
  • <dubbo:service connections="0"> 或 <dubbo:reference connections="0"> 表示該服務使用 JVM 共享長鏈接。缺省
  • <dubbo:service connections="1"> 或 <dubbo:reference connections="1"> 表示該服務使用獨立長鏈接。
  • <dubbo:service connections="2"> 或<dubbo:reference connections="2"> 表示該服務使用獨立兩條長鏈接。

爲防止被大量鏈接撐掛,可在服務提供方限制大接收鏈接數,以實現服務提供方自我保護。

<dubbo:protocol name="dubbo" accepts="1000" />

dubbo.properties 配置:

1 dubbo.service.protocol=dubbo

 

 

1.各協議的比較

 

協議名稱 實現描述 鏈接 使用場景
dubbo 傳輸:mina、netty、grizzy

序列化:dubbo、hessian二、java、json
    dubbo缺省採用單一長鏈接和NIO異步通信    1.傳入傳出參數數據包較小

2.消費者 比提供者多

3.常規遠程服務方法調用

4.不適合傳送大數據量的服務,好比文件、傳視頻
rmi 傳輸:java  rmi

序列化:java 標準序列化
    

鏈接個數:多鏈接

鏈接方式:短鏈接

傳輸協議:TCP/IP

傳輸方式:BIO

1.常規RPC調用

2.與原RMI客戶端互操做

3.可傳文件

4.不支持防火牆穿透
hessian
傳輸:Serverlet容器

序列化:hessian二進制序列化
   

    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸

   

1.提供者比消費者多

2.可傳文件

3.跨語言傳輸
http
傳輸:servlet容器

序列化:表單序列化
    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸
1.提供者多餘消費者

2.數據包混合
webservice
傳輸:HTTP

序列化:SOAP文件序列化
    鏈接個數:多鏈接
    鏈接方式:短鏈接
    傳輸協議:HTTP
    傳輸方式:同步傳輸

1.系統集成

2.跨語言調用
thrift
    與thrift RPC實現集成,並在基礎上修改了報文頭   

長鏈接、NIO異步傳輸   
 

 

二、協議的配置:<dubbo:protocal>(只需在服務提供方配置便可)

 

屬性 類型 是否必填 缺省值 描述
name string 必填 dubbo 協議名稱
port int 可選

dubbo協議缺省端口爲20880,

rmi協議缺省端口爲1099,

http和hessian協議缺省端口爲80;

若是配置爲-1或者沒有配置port,則

會分配一個沒有被佔用的端口。

服務端口
threadpool string 可選 fixed 線程池類型,可選:fixed/cached
threads int 可選 100 服務h定大小)
iothreads int 可選 CPU個數+1 io線程池大小(固定)
accepts int 可選 0 服務提供方最大可接受鏈接數
serialization string 可選

dubbo協議缺省爲hessian2,

rmi缺省協議爲java,

http協議缺省爲json

可填序列化:dubbo|hessian2|

java|compactedjava|fastjson

dispatcher string 可選 dubbo協議缺省爲all

協議的消息派發方式,用於指定線程模型,

好比:dubbo協議的all, direct, message,

execution, connection等

參考:https://blog.csdn.net/fd2025/article/

details/79985542

queues int 可選 0

線程池隊列大小,當線程池滿時,

排隊等待執行的隊列大小,建議不要設置,

當線程程池時應當即失敗,重試其它服務

提供機器,而不是排隊,除非有特殊需求。

charset string 可選 UTF-8 序列化編碼
server server string

dubbo協議缺省爲netty,

http協議缺省爲servlet

hessian協議缺省爲jetty

協議的服務器端實現類型,

好比:dubbo協議的mina,netty等,

http協議的jetty,servlet等

  1.  
    <dubbo:protocol name="hessian" server="jetty"
  2.  
    client="httpclient"
  3.  
    port="20880"
  4.  
    threadpool="fixed"
  5.  
    threads="50"
  6.  
    iothreads="5"
  7.  
    accepts="1000"/>

    

協議對應的源碼:

 

dubbo 協議:com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol

RMI協議: com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol

Http協議:com.alibaba.dubbo.rpc.protocol.http.HttpProtocol.InternalHandler

Hessian協議 :com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol.HessianHandler

 

 

 

dubbo多鏈接配置:

Dubbo 協議缺省每服務每提供者每消費者使用單一長鏈接,若是數據量較大,可使用多個鏈接。

  1.  
    <dubbo:serviceconnections="1"/>
  2.  
    <dubbo:referenceconnections="1"/>
  • <dubbo:service connections="0"> 或 <dubbo:reference connections="0"> 表示該服務使用 JVM 共享長鏈接。缺省
  • <dubbo:service connections="1"> 或 <dubbo:reference connections="1"> 表示該服務使用獨立長鏈接。
  • <dubbo:service connections="2"> 或<dubbo:reference connections="2"> 表示該服務使用獨立兩條長鏈接。

爲防止被大量鏈接撐掛,可在服務提供方限制大接收鏈接數,以實現服務提供方自我保護。

<dubbo:protocolname="dubbo"accepts="1000" />

dubbo.properties 配置:

dubbo.service.protocol=dubbo>


相關協議配置

1、9種協議
一、dubbo 協議 (默認)
一、dubbo 缺省協議 採用單一長鏈接和NIO異步通信,適合於小數據量大併發的服務調用,以及服務消費者機器數遠大於服務提供者機器數的狀況
二、不適合傳送大數據量的服務,好比傳文件,傳視頻等,除非請求量很低。

 

缺省協議,使用基於mina1.1.7+hessian3.2.1的tbremoting交互。

 

特性

 

  鏈接個數:單鏈接
  鏈接方式:長鏈接
  傳輸協議:TCP
  傳輸方式:NIO異步傳輸
  序列化:Hessian 二進制序列化
  適用範圍:傳入傳出參數數據包較小(建議小於100K),消費者比提供者個數多,單一消費者沒法壓滿提供者,儘可能不要用dubbo協議傳輸大文件或超大字符串。
適用場景:常規遠程服務方法調用
配置

 

  <!--配置協議: -->
  <dubbo:protocol name="dubbo" port="20880" />

 

  <!--設置默認協議: -->
  <dubbo:provider protocol="dubbo" />

 

  <!-- 設置服務協議: -->
  <dubbo:service protocol="dubbo" />

 

  <!-- 多端口 -->
  <dubbo:protocol id="dubbo1" name="dubbo" port="20880" />
  <dubbo:protocol id="dubbo2" name="dubbo" port="20881" />

 

  <!-- 配置協議選項: -->
  <dubbo:protocol name=「dubbo」 port=「9090」 server=「netty」 client=「netty」 codec=「dubbo」
  serialization=「hessian2」 charset=「UTF-8」 threadpool=「fixed」 threads=「100」
  queues=「0」 iothreads=「9」 buffer=「8192」 accepts=「1000」 payload=「8388608」 />

 


三、Dubbo協議缺省每服務每提供者每消費者使用單一長鏈接,若是數據量較大,可使用多個鏈接。

 

  <dubbo:protocol name="dubbo" connections="2" />

  <dubbo:service connections=」0」>或<dubbo:reference connections=」0」>表示該服務使用JVM共享長鏈接。(缺省)
  <dubbo:service connections=」1」>或<dubbo:reference connections=」1」>表示該服務使用獨立長鏈接。
  <dubbo:service connections=」2」>或<dubbo:reference connections=」2」>表示該服務使用獨立兩條長鏈接。

 

四、爲防止被大量鏈接撐掛,可在服務提供方限制大接收鏈接數,以實現服務提供方自我保護

 

  <dubbo:protocol name="dubbo" accepts="1000" />

常見問題

 

爲何要消費者比提供者個數多?
  因dubbo協議採用單一長鏈接,假設網絡爲千兆網卡(1024Mbit=128MByte),根據測試經驗數據每條鏈接最多隻能壓滿7MByte(不一樣的環境可能不同,供參考),理論上1個服務提供者須要20個服務消費者才能壓滿網卡

 

爲何不能傳大包?
  因dubbo協議採用單一長鏈接,若是每次請求的數據包大小爲500KByte,假設網絡爲千兆網卡(1024Mbit=128MByte),每條鏈接最大7MByte(不一樣的環境可能不同,供參考),單個服務提供者的TPS(每秒處理事務數)最大爲:128MByte / 500KByte = 262。單個消費者調用單個服務提供者的TPS(每秒處理事務數)最大爲:7MByte / 500KByte = 14。若是能接受,能夠考慮使用,不然網絡將成爲瓶頸。

 

爲何採用異步單一長鏈接?
  由於服務的現狀大都是服務提供者少,一般只有幾臺機器,而服務的消費者多,可能整個網站都在訪問該服務,好比Morgan的提供者只有6臺提供者,卻有上百臺消費者,天天有1.5億次調用,若是採用常規的hessian服務,服務提供者很容易就被壓跨,經過單一鏈接,保證單一消費者不會壓死提供者,長鏈接,減小鏈接握手驗證等,並使用異步IO,複用線程池,防止C10K問題。

 

接口增長方法,對客戶端無影響,若是該方法不是客戶端須要的,客戶端不須要從新部署;
輸入參數和結果集中增長屬性,對客戶端無影響,若是客戶端並不須要新屬性,不用從新
部署;

 

輸入參數和結果集屬性名變化,對客戶端序列化無影響,可是若是客戶端不從新部署,無論輸入仍是輸出,屬性名變化的屬性值是獲取不到的。

 

總結:服務器端 和 客戶端 對 領域對象 並不須要徹底一致,而是按照最大匹配原則。

 

若是不是集成Spring,單獨配置以下:

 

  dubbo.service.protocol=dubbo

二、rmi 協議
一、RMI協議採用JDK標準的java.rmi.*實現,採用阻塞式短鏈接和JDK標準序列化方式 。

 

注:
若是正在使用RMI提供服務給外部訪問(公司內網環境應該不會有攻擊風險),同時應用裏依賴了老的common-collections包(dubbo不會依賴這個包,請排查本身的應用有沒有使用)的狀況下,存在反序列化安全風險。
請檢查應用:
將commons-collections3 請升級到3.2.2版本:
https://commons.apache.org/proper/commons-collections/release_3_2_2.html

 

將commons-collections4 請升級到4.1版本:
https://commons.apache.org/proper/commons-collections/release_4_1.html

 

新版本的commons-collections解決了該問題 。

 

特性

 

  鏈接個數:多鏈接
  鏈接方式:短鏈接
  傳輸協議:TCP
  傳輸方式:同步傳輸
  序列化:Java標準二進制序列化
  適用範圍:傳入傳出參數數據包大小混合,消費者與提供者個數差很少,可傳文件。
  適用場景:常規遠程服務方法調用,與原生RMI服務互操做
接口
若是服務接口繼承了java.rmi.Remote接口,能夠和原生RMI互操做,即:
提供者用Dubbo的RMI協議暴露服務,消費者直接用標準RMI接口調用,或者提供方用標準RMI暴露服務,消費方用Dubbo的RMI協議調用。

 

若是服務接口沒有繼承java.rmi.Remote接口,缺省Dubbo將自動生成一個com.xxx.XxxService$Remote的接口,並繼承java.rmi.Remote接口,並以此接口暴露服務,

 

但若是設置了<dubbo:protocol name="rmi" codec="spring" />,將不生成$Remote接口,而使用Spring的RmiInvocationHandler接口暴露服務,和Spring兼容。

 

配置

 

  <!-- 定義 RMI 協議: -->
  <dubbo:protocol name="rmi" port="1099" />

 

  <!--設置默認協議: -->
  <dubbo:provider protocol="rmi" />

 

  <!-- 設置服務協議: -->
  <dubbo:service protocol="rmi" />

 

  <!--多端口: -->
  <dubbo:protocol id="rmi1" name="rmi" port="1099" />
  <dubbo:protocol id="rmi2" name="rmi" port="2099" />
  <dubbo:service protocol="rmi1" />

 

  <!--Spring 兼容性:-->
  <dubbo:protocol name="rmi" codec="spring" />

三、hessian 協議
Hessian 協議用於集成 Hessian 的服務,Hessian 底層採用 Http 通信,採用 Servlet 暴露服務,Dubbo 缺省內嵌 Jetty 做爲服務器實現。

 

Dubbo 的 Hessian 協議能夠和原生 Hessian 服務互操做,即:

 

提供者用 Dubbo 的 Hessian 協議暴露服務,消費者直接用標準 Hessian 接口調用
或者提供方用標準 Hessian 暴露服務,消費方用 Dubbo 的 Hessian 協議調用。
Hessian 是 Caucho 開源的一個 RPC 框架,其通信效率高於 WebService 和 Java 自帶的序列化。

 

特性

  鏈接個數:多鏈接
  鏈接方式:短鏈接
  傳輸協議:HTTP
  傳輸方式:同步傳輸
  序列化:Hessian二進制序列化
  適用範圍:傳入傳出參數數據包較大,提供者比消費者個數多,提供者壓力較大,可傳文件。
  適用場景:頁面傳輸,文件傳輸,或與原生hessian服務互操做

依賴:

  <dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.7</version>
  </dependency>

約束
  一、參數及返回值需實現Serializable接口
  二、參數及返回值不能自定義實現List, Map, Number, Date, Calendar等接口,只能用JDK自帶的實現,由於hessian會作特殊處理,自定義實現類中的屬性值都會丟失。

 

配置

 

  <!-- 定義 hessian 協議: -->
  <dubbo:protocol name="hessian" port="8080" server="jetty" />

 

  <!--設置默認協議: -->
  <dubbo:provider protocol="hessian" />

 

  <!-- 設置 service 協議: -->
  <dubbo:service protocol="hessian" />

 

  <!-- 多端口:-->
  <dubbo:protocol id="hessian1" name="hessian" port="8080" />
  <dubbo:protocol id="hessian2" name="hessian" port="8081" />

 

  <!--直連:-->
  <dubbo:reference id="helloService" interface="HelloWorld" url="hessian://10.20.153.10:8080/helloWorld" />

web.xml 配置:

 

  <servlet>
    <servlet-name>dubbo</servlet-name>
    <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dubbo</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

注意:若是使用servlet派發請求
協議的端口<dubbo:protocol port="8080" />必須與servlet容器的端口相同,
協議的上下文路徑<dubbo:protocol contextpath="foo" />必須與servlet應用的上下文路徑相同。

 

四、http 協議
基於http表單的遠程調用協議。參見:[HTTP協議使用說明]

 

特性

  鏈接個數:多鏈接
  鏈接方式:短鏈接
  傳輸協議:HTTP
  傳輸方式:同步傳輸
  序列化:表單序列化 ,即 json
  適用範圍:傳入傳出參數數據包大小混合,提供者比消費者個數多,可用瀏覽器查看,可用表單或URL傳入參數,暫不支持傳文件。
  適用場景:需同時給應用程序和瀏覽器JS使用的服務。
配置

 

  <!-- 配置協議:-->
  <dubbo:protocol name="http" port="8080" />

 

  <!-- 配置 Jetty Server (默認):-->
  <dubbo:protocol ... server="jetty" />

 

  <!-- 配置 Servlet Bridge Server (推薦使用): -->
  <dubbo:protocol ... server="servlet" />

配置 DispatcherServlet:

 

  <servlet>
    <servlet-name>dubbo</servlet-name>
    <servlet-class>org.apache.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dubbo</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

注意:若是使用 servlet 派發請求 :

 

協議的端口<dubbo:protocol port="8080" />必須與servlet容器的端口相同,
協議的上下文路徑<dubbo:protocol contextpath="foo" />必須與servlet應用的上下文路徑相同。


五、webservice 協議
基於 WebService 的遠程調用協議,基於 Apache CXF的 frontend-simple 和 transports-http 實現。

 

能夠和原生 WebService 服務互操做,即:

 

提供者用 Dubbo 的 WebService 協議暴露服務,消費者直接用標準 WebService 接口調用,
或者提供方用標準 WebService 暴露服務,消費方用 Dubbo 的 WebService 協議調用。

依賴

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-simple</artifactId>
  <version>2.6.1</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http</artifactId>
  <version>2.6.1</version>
</dependency>

特性

  鏈接個數:多鏈接
  鏈接方式:短鏈接
  傳輸協議:HTTP
  傳輸方式:同步傳輸
  序列化:SOAP文本序列化
  適用場景:系統集成,跨語言調用
一、基於CXF的 frontend-simple 和 transports-http 實現。
二、CXF是Apache開源的一個RPC框架:http://cxf.apache.org,由Xfire和Celtix合併而來 。

 

能夠和原生WebService服務互操做,即:
提供者用Dubbo的WebService協議暴露服務,消費者直接用標準WebService接口調用,或者提供方用標準WebService暴露服務,消費方用Dubbo的WebService協議調用。

 

約束
參數及返回值需實現Serializable接口
參數儘可能使用基本類型和POJO。

 

配置

 

  <!-- 配置協議: -->
  <dubbo:protocol name="webservice" port="8080" server="jetty" />

 

  <!-- 配置默認協議:-->
  <dubbo:provider protocol="webservice" />

 

  <!-- 配置服務協議:-->
  <dubbo:service protocol="webservice" />

 

  <!--多端口: -->
  <dubbo:protocol id="webservice1" name="webservice" port="8080" />
  <dubbo:protocol id="webservice2" name="webservice" port="8081" />

 

  <!-- 直連: -->
  <dubbo:reference id="helloService" interface="HelloWorld" url="webservice://10.20.153.10:8080/com.foo.HelloWorld" />

 

<!-- WSDL -->
http://10.20.153.10:8080/com.foo.HelloWorld?wsdl

 

<!-- Jetty Server: (默認) -->
<dubbo:protocol ... server="jetty" />

 

<!-- Servlet Bridge Server: (推薦) -->
<dubbo:protocol ... server="servlet" />


配置 DispatcherServlet:

 

  <servlet>
    <servlet-name>dubbo</servlet-name>
    <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dubbo</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

注意:若是使用servlet派發請求:
協議的端口<dubbo:protocol port=「8080」 />必須與servlet容器的端口相同,
協議的上下文路徑<dubbo:protocol contextpath=「foo」 />必須與servlet應用的上下文路徑相同。

 

六、thrift 協議
當前 dubbo 支持的 thrift 協議是對 thrift 原生協議 [2] 的擴展,在原生協議的基礎上添加了一些額外的頭信息,好比 service name,magic number 等。

 

使用 dubbo thrift 協議一樣須要使用 thrift 的 idl compiler 編譯生成相應的 java 代碼,後續版本中會在這方面作一些加強。

依賴

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.8.0</version>
</dependency>

配置

  <dubbo:protocol name="thrift" port="3030" />

常見問題
Thrift不支持null值,不能在協議中傳null

 

七、memcached 協議
基於 memcached實現的 RPC 協議。

 

註冊 memcached 服務的地址

 

  RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
  Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));
  registry.register(URL.valueOf("memcached://10.20.153.11/com.foo.BarService?category=providers&dynamic=false&application=foo&group=member&loadbalance=consistenthash"));

在客戶端引用

 

在客戶端使用 :

 

  <dubbo:reference id="cache"
    interface="http://10.20.160.198/wiki/display/dubbo/java.util.Map" group="member" />

或者點對點直連:

 

  <dubbo:reference id="cache"
    interface="http://10.20.160.198/wiki/display/dubbo/java.util.Map"
    url="memcached://10.20.153.10:11211" />

自定義接口:

 

  <dubbo:reference id="cache"
    interface="com.foo.CacheService" url="memcached://10.20.153.10:11211" />

方法名建議和memcached的標準方法名相同,即:get(key), set(key, value), delete(key)。

 

若是方法名和memcached的標準方法名不相同,則須要配置映射關係:(其中」p:xxx」爲spring的標準p標籤)

 

  <dubbo:reference id="cache" interface="com.foo.CacheService"
    url="memcached://10.20.153.10:11211" p:set="putFoo"
    p:get="getFoo" p:delete="removeFoo" />

八、redis 協議
基於 Redis實現的 RPC 協議。

 

註冊 redis 服務的地址

 

能夠經過腳本或監控中心手工填寫表單註冊redis服務的地址:

 

  RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
  Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));
  registry.register(URL.valueOf("redis://10.20.153.11/com.foo.BarService?category=providers&dynamic=false&application=foo&group=member&loadbalance=consistenthash"));

在客戶端引用

在客戶端使用:

 

  <dubbo:reference id="store"
    interface="http://10.20.160.198/wiki/display/dubbo/java.util.Map" group="member" />

或者,點對點直連:

 

  <dubbo:reference id="store" interface="http://10.20.160.198/wiki/display/dubbo/java.util.Map"
    url="redis://10.20.153.10:6379" />

也可使用自定義接口:

 

  <dubbo:reference id="store" interface="com.foo.StoreService" url="redis://10.20.153.10:6379" />

方法名建議和redis的標準方法名相同,即:get(key), set(key, value), delete(key)。

 

若是方法名和redis的標準方法名不相同,則須要配置映射關係:(其中」p:xxx」爲spring的標準p標籤)

 

  <dubbo:reference id="cache" interface="com.foo.CacheService"
    url="memcached://10.20.153.10:11211"
    p:set="putFoo" p:get="getFoo" p:delete="removeFoo" />

九、rest ( 就是 RestFull)
基於標準的Java REST API——JAX-RS 2.0(Java API for RESTful Web Services的簡寫)實現的REST調用支持

 

參考: http://dubbo.apache.org/zh-cn/docs/user/references/protocol/rest.html

 

2、多協議配置
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://dubbo.apache.org/schema/dubbo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo   http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

  <dubbo:application name="world" />
  <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />

  <!-- 多協議配置 -->
  <dubbo:protocol name="dubbo" port="20880" />
  <dubbo:protocol name="rmi" port="1099" />

  <!-- 使用dubbo協議暴露服務 -->
  <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />

  <!-- 使用rmi協議暴露服務 -->
  <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" />

</beans>


多協議暴露服務
須要與 http 客戶端互操做

<?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://dubbo.apache.org/schema/dubbo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

  <dubbo:application name="world" />
  <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />

  <!-- 多協議配置 -->
  <dubbo:protocol name="dubbo" port="20880" />
  <dubbo:protocol name="hessian" port="8080" />

  <!-- 使用多個協議暴露服務 -->
  <dubbo:service id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0"   protocol="dubbo,hessian" />

</beans>

3、面試題:
一、dubbo 默認使用什麼序列化框架,你知道的還有哪些?
dubbo 協議 默認使用 Hessian2 序列化。
rmi協議 默認爲 java ,
http 協議 默認爲 爲 json 。

 

此外補充,
hessian 協議,默認是 hessian 序列化協議;
webservice 協議,默認是 soap 文本序列化 。

 

dubbo doc 有說明: http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

 

 

 

 


如上圖所示,在 serialization 這一行中,若是使用的是dubbo協議,則默認 序列化 框架是 hessian2,
rmi協議,則默認爲 java ;http 協議,則默認爲 爲json 。

 

二、dubbo推薦用什麼協議?默認使用dubbo協議。

相關文章
相關標籤/搜索