Dubbo特性


dubbo.properties

Dubbo 將自動加載 classpath 根目錄下的dubbo.properties,可以通過JVM啟動參數 -Ddubbo.properties.file=xxx.properties 改變缺省配置位置。

啟動時檢查

注意:是在消費端進行檢查服務,格式如下,如果當前服務沒有啟動,就會報錯

<dubbo:reference id="sampleService" check="true" interface="bhz.dubbo.sample.provider.SampleService" />

關閉所有服務的啟動時檢查 (沒有提供者時報錯)

<dubbo:consumer check="false" />

關閉注冊中心啟動時檢查 (注冊訂閱失敗時報錯)

<dubbo:registry check="false" />

通過 dubbo.properties

dubbo.reference.bhz.dubbo.sample.provider.SampleService.check=false
dubbo.reference.check=false
dubbo.consumer.check=false
dubbo.registry.check=false

配置的含義

  • dubbo.reference.check=false,強制改變所有 reference 的 check 值,就算配置中有聲明,也會被覆蓋。

  • dubbo.consumer.check=false,是設置 check 的缺省值,如果配置中有顯式的聲明,如:<dubbo:reference check="true"/>,不會受影響。

  • dubbo.registry.check=false,前面兩個都是指訂閱成功,但提供者列表是否為空是否報錯,如果注冊訂閱失敗時,也允許啟動,需使用此選項,將在后台定時重試。

集群容錯 默認Failover Cluster

在集群調用失敗時,Dubbo 提供了多種容錯方案,缺省為 failover 重試。

詳見 https://dubbo.gitbooks.io/dubbo-user-book/content/demos/fault-tolerent-strategy.html

集群模式配置

按照以下示例在服務提供方和消費方配置集群模式

<dubbo:service cluster="failsafe" />

<dubbo:reference cluster="failsafe" />

直連提供者

<dubbo:reference id="xxxService" interface="com.alibaba.xxx.XxxService" url="dubbo://localhost:20890" />
java -Dcom.alibaba.xxx.XxxService=dubbo://localhost:20890

只訂閱

<dubbo:registry address="10.20.153.10:9090" register="false" />

只注冊

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />
<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />

多協議

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/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>

多協議暴露服務

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/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>

多注冊中心

注冊
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注冊中心配置 -->
    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
    <!-- 向多個注冊中心注冊 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
</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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注冊中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 向中文站注冊中心注冊 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
    <!-- 向國際站注冊中心注冊 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="world"  />
    <!-- 多注冊中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    
    <!--用 id 區分-->
    <!-- 引用中文站服務 -->
    <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
    <!-- 引用國際站站服務 -->
    <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
</beans>

服務分組

當一個接口有多種實現時,可以用 group 區分。

服務
<dubbo:service group="feedback" interface="com.xxx.IndexService" />
<dubbo:service group="member" interface="com.xxx.IndexService" />

引用
<!--根據id進行引用-->
<dubbo:reference id="feedbackIndexService" group="feedback" interface="com.xxx.IndexService" />
<dubbo:reference id="memberIndexService" group="member" interface="com.xxx.IndewxService" />
<!--任意組-->
<dubbo:reference id="barService" interface="com.foo.BarService" group="*" />

多版本

就是加version=""字段

分組聚合

按組合並返回結果 1,比如菜單服務,接口一樣,但有多種實現,用group區分,現在消費方需從每種group中調用一次返回結果,合並結果返回,這樣就可以實現聚合菜單項。

<!--搜索所有分組-->
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />

<!--合並指定分組-->
<dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />


<!--指定方法合並結果,其它未指定的方法,將只調用一個 Group-->
<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="true" />
</dubbo:service>

<!--指定合並策略,缺省根據返回值類型自動匹配,如果同一類型有兩個合並器時,需指定合並器的名稱 2
-->
<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="mymerge" />
</dubbo:service>

參數驗證

https://dubbo.gitbooks.io/dubbo-user-book/content/demos/parameter-validation.html

結果緩存

結果緩存 1,用於加速熱門數據的訪問速度,Dubbo 提供聲明式緩存,以減少用戶加緩存的工作量

緩存類型
  • lru 基於最近最少使用原則刪除多余緩存,保持最熱的數據被緩存。
  • threadlocal 當前線程緩存,比如一個頁面渲染,用到很多 portal,每個 portal 都要去查用戶信息,通過線程緩存,可以減少這種多余訪問。
  • jcache 與 JSR107 集成,可以橋接各種緩存實現。


<dubbo:reference interface="com.foo.BarService" cache="lru" />

<dubbo:reference interface="com.foo.BarService">
    <dubbo:method name="findBar" cache="lru" />
</dubbo:reference>


泛化調用

回聲測試

// 遠程服務引用
MemberService memberService = ctx.getBean("memberService"); 

EchoService echoService = (EchoService) memberService; // 強制轉型為EchoService

// 回聲測試可用性
String status = echoService.$echo("OK"); 

assert(status.equals("OK"));

上下文信息

上下文中存放的是當前調用過程中所需的環境信息。所有配置信息都將轉換為 URL 的參數,參見 schema 配置參考手冊 中的對應URL參數一列。
RpcContext 是一個 ThreadLocal 的臨時狀態記錄器,當接收到 RPC 請求,或發起 RPC 請求時,RpcContext 的狀態都會變化。比如:A 調 B,B 再調 C,則 B 機器上,在 B 調 C 之前,RpcContext 記錄的是 A 調 B 的信息,在 B 調 C 之后,RpcContext 記錄的是 B 調 C 的信息。

服務消費方

// 遠程調用
xxxService.xxx();
// 本端是否為消費端,這里會返回true
boolean isConsumerSide = RpcContext.getContext().isConsumerSide();
// 獲取最后一次調用的提供方IP地址
String serverIP = RpcContext.getContext().getRemoteHost();
// 獲取當前服務配置信息,所有配置信息都將轉換為URL的參數
String application = RpcContext.getContext().getUrl().getParameter("application");
// 注意:每發起RPC調用,上下文狀態會變化
yyyService.yyy();

服務提供方

public class XxxServiceImpl implements XxxService {

    public void xxx() {
        // 本端是否為提供端,這里會返回true
        boolean isProviderSide = RpcContext.getContext().isProviderSide();
        // 獲取調用方IP地址
        String clientIP = RpcContext.getContext().getRemoteHost();
        // 獲取當前服務配置信息,所有配置信息都將轉換為URL的參數
        String application = RpcContext.getContext().getUrl().getParameter("application");
        // 注意:每發起RPC調用,上下文狀態會變化
        yyyService.yyy();
        // 此時本端變成消費端,這里會返回false
        boolean isProviderSide = RpcContext.getContext().isProviderSide();
    } 
}

隱式參數

可以通過 RpcContext 上的 setAttachment 和 getAttachment 在服務消費方和提供方之間進行參數的隱式傳遞。 1

在服務消費方端設置隱式參數
// setAttachment 設置的 KV 對,在完成下面一次遠程調用會被清空,即多次遠程調用要多次設置。

RpcContext.getContext().setAttachment("index", "1"); // 隱式傳參,后面的遠程調用都會隱式將這些參數發送到服務器端,類似cookie,用於框架集成,不建議常規業務使用

xxxService.xxx(); // 遠程調用

// ...


在服務提供方端獲取隱式參數`


public class XxxServiceImpl implements XxxService {

    public void xxx() {
        // 獲取客戶端隱式傳入的參數,用於框架集成,不建議常規業務使用
        String index = RpcContext.getContext().getAttachment("index"); 
    }
}

異步調用

消費者 consumer.xml

<dubbo:reference id="fooService" interface="com.alibaba.foo.FooService">
      <dubbo:method name="findFoo" async="true" />
</dubbo:reference>
<dubbo:reference id="barService" interface="com.alibaba.bar.BarService">
      <dubbo:method name="findBar" async="true" />
</dubbo:reference>

調用代碼

// 此調用會立即返回null
fooService.findFoo(fooId);
// 拿到調用的Future引用,當結果返回后,會被通知和設置到此Future
Future<Foo> fooFuture = RpcContext.getContext().getFuture(); 

// 此調用會立即返回null
barService.findBar(barId);
// 拿到調用的Future引用,當結果返回后,會被通知和設置到此Future
Future<Bar> barFuture = RpcContext.getContext().getFuture(); 

// 此時findFoo和findBar的請求同時在執行,客戶端不需要啟動多線程來支持並行,而是借助NIO的非阻塞完成

// 如果foo已返回,直接拿到返回值,否則線程wait住,等待foo返回后,線程會被notify喚醒
Foo foo = fooFuture.get(); 
// 同理等待bar返回
Bar bar = barFuture.get(); 

// 如果foo需要5秒返回,bar需要6秒返回,實際只需等6秒,即可獲取到foo和bar,進行接下來的處理。

你也可以設置是否等待消息發出
<!--sent="true" 等待消息發出,消息發送失敗將拋出異常-->
<!--sent="false" 不等待消息發出,將消息放入 IO 隊列,即刻返回 -->

<dubbo:method name="findFoo" async="true" sent="true" />


如果你只是想異步,完全忽略返回值,可以配置 return="false",以減少 Future 對象的創建和管理成本:
<dubbo:method name="findFoo" async="true" return="false" />

事件通知

在調用之前、調用之后、出現異常時,會觸發 oninvoke、onreturn、onthrow 三個事件,可以配置當事件發生時,通知哪個類的哪個方法 1。

服務提供者與消費者共享服務接口
interface IDemoService {
    public Person get(int id);
}

服務提供者實現
class NormalDemoService implements IDemoService {
    public Person get(int id) {
        return new Person(id, "charles`son", 4);
    }
}
```java

###### 服務提供者配置

```xml

<dubbo:application name="rpc-callback-demo" />
<dubbo:registry address="http://10.20.160.198/wiki/display/dubbo/10.20.153.186" />
<bean id="demoService" class="com.alibaba.dubbo.callback.implicit.NormalDemoService" />
<dubbo:service interface="com.alibaba.dubbo.callback.implicit.IDemoService" ref="demoService" version="1.0.0" group="cn"/>

服務消費者 Callback 接口
interface Notify {
    public void onreturn(Person msg, Integer id);
    public void onthrow(Throwable ex, Integer id);
}
服務消費者 Callback 實現

class NotifyImpl implements Notify {
    public Map<Integer, Person>    ret    = new HashMap<Integer, Person>();
    public Map<Integer, Throwable> errors = new HashMap<Integer, Throwable>();

    public void onreturn(Person msg, Integer id) {
        System.out.println("onreturn:" + msg);
        ret.put(id, msg);
    }

    public void onthrow(Throwable ex, Integer id) {
        errors.put(id, ex);
    }
}

服務消費者 Callback 配置
<bean id ="demoCallback" class = "com.alibaba.dubbo.callback.implicit.NofifyImpl" />
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.callback.implicit.IDemoService" version="1.0.0" group="cn" >
      <dubbo:method name="get" async="true" onreturn = "demoCallback.onreturn" onthrow="demoCallback.onthrow" />
</dubbo:reference>

callback 與 async 功能正交分解,async=true 表示結果是否馬上返回,onreturn 表示是否需要回調。
兩者疊加存在以下幾種組合情況 2:
異步回調模式:async=true onreturn="xxx"
同步回調模式:async=false onreturn="xxx"
異步無回調 :async=true
同步無回調 :async=false

測試代碼
IDemoService demoService = (IDemoService) context.getBean("demoService");
NofifyImpl notify = (NofifyImpl) context.getBean("demoCallback");
int requestId = 2;
Person ret = demoService.get(requestId);
Assert.assertEquals(null, ret);
//for Test:只是用來說明callback正常被調用,業務具體實現自行決定.
for (int i = 0; i < 10; i++) {
    if (!notify.ret.containsKey(requestId)) {
        Thread.sleep(200);
    } else {
        break;
    }
}
Assert.assertEquals(requestId, notify.ret.get(requestId).getId());

並發控制

限制 com.foo.BarService 的每個方法,服務器端並發執行(或占用線程池線程數)不能超過 10 個:

<dubbo:service interface="com.foo.BarService" executes="10" />

限制 com.foo.BarService 的 sayHello 方法,服務器端並發執行(或占用線程池線程數)不能超過 10 個:

<dubbo:service interface="com.foo.BarService">
    <dubbo:method name="sayHello" executes="10" />
</dubbo:service>

限制 com.foo.BarService 的每個方法,每客戶端並發執行(或占用連接的請求數)不能超過 10 個:

<dubbo:service interface="com.foo.BarService" actives="10" />

<dubbo:reference interface="com.foo.BarService" actives="10" />

限制 com.foo.BarService 的 sayHello 方法,每客戶端並發執行(或占用連接的請求數)不能超過 10 個:

<dubbo:service interface="com.foo.BarService">
    <dubbo:method name="sayHello" actives="10" />
</dubbo:service>

<dubbo:reference interface="com.foo.BarService">
    <dubbo:method name="sayHello" actives="10" />
</dubbo:service>
如果 dubbo:servicedubbo:reference 都配了actives,dubbo:reference 優先,參見:配置的覆蓋策略。

令牌驗證

通過令牌驗證在注冊中心控制權限,以決定要不要下發令牌給消費者,可以防止消費者繞過注冊中心訪問提供者,另外通過注冊中心可靈活改變授權方式,而不需修改或升級提供者

image

可以全局設置開啟令牌驗證
<!--隨機token令牌,使用UUID生成-->
<dubbo:provider interface="com.foo.BarService" token="true" />

<!--固定token令牌,相當於密碼-->
<dubbo:provider interface="com.foo.BarService" token="123456" />

<!--隨機token令牌,使用UUID生成-->
<dubbo:service interface="com.foo.BarService" token="true" />

<!--固定token令牌,相當於密碼-->
<dubbo:service interface="com.foo.BarService" token="123456" />

<!--隨機token令牌,使用UUID生成-->
<dubbo:protocol name="dubbo" token="true" />

<!--固定token令牌,相當於密碼-->
<dubbo:protocol name="dubbo" token="123456" />

優雅停機

設置優雅停機超時時間 缺省超時時間是 10 秒,如果超時則強制關閉。
dubbo.service.shutdown.wait=15000

日志適配

命令行

java -Ddubbo.application.logger=log4j

在 dubbo.properties 中指定

dubbo.application.logger=log4j

在 dubbo.xml 中配置

<dubbo:application logger="log4j" />

訪問日志

將訪問日志輸出到當前應用的log4j日志:

<dubbo:protocol accesslog="true" />

將訪問日志輸出到指定文件:

<dubbo:protocol accesslog="http://10.20.160.198/wiki/display/dubbo/foo/bar.log" />

服務容器

Spring Container
  • 自動加載 META-INF/spring 目錄下的所有 Spring 配置。
  • 配置 spring 配置加載位置:
dubbo.spring.config=classpath*:META-INF/spring/*.xml

Jetty Container

啟動一個內嵌 Jetty,用於匯報狀態。
配置:

dubbo.jetty.port=8080:配置 jetty 啟動端口
dubbo.jetty.directory=/foo/bar:配置可通過 jetty 直接訪問的目錄,用於存放靜態文件
dubbo.jetty.page=log,status,system:配置顯示的頁面,缺省加載所有頁面
Log4j Container

自動配置 log4j 的配置,在多進程啟動時,自動給日志文件按進程分目錄。
配置:

dubbo.log4j.file=/foo/bar.log:配置日志文件路徑
dubbo.log4j.level=WARN:配置日志級別
dubbo.log4j.subdirectory=20880:配置日志子目錄,用於多進程啟動,避免沖突
容器啟動
  • java com.alibaba.dubbo.container.Main
  • java com.alibaba.dubbo.container.Main spring jetty log4j
  • java com.alibaba.dubbo.container.Main -Ddubbo.container=spring,jetty,log4j
  • dubbo.container=spring,jetty,log4j


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM