直連提供者
通常我們稱之爲點對點鏈接
作爲消費者 我們如果需要直連一個服務,可以在配置文件裏面這樣寫
服務端暴露服務
<!-- 省略部分 -->
<!--服務發佈的配置,需要暴露的服務接口-->
<!--interface 接口路徑-->
<!--timeout 服務的超時時間-->
<!--retries 服務的重試次數-->
<!--loadbalance 服務的負載均衡策略 random 隨機,roundrobin 輪詢 leastactive 最少活躍調用數,響應的越快的越容易收到請求,相同的隨機
consistenthash 一致性hash ,相同參數的請求總是發到同一提供者,當某一臺提供者掛時,原本發往該提供者的請求,基於虛擬節點,平攤到其它提供者,不會引起劇烈變動 -->
<!--actives-->
<!--ref-->
<dubbo:service
interface="com.jenkin.common.module1api.OrderService"
retries="2"
cluster="failover"
loadbalance="leastactive"
ref="orderService">
<!--可以指定方法級別的配置-->
<dubbo:method name="getOrderFailOver" retries="2" />
<dubbo:method name="getOrder" actives="0" />
</dubbo:service>
<!-- 省略部分 -->
消費端:
<dubbo:reference id="xxxService" interface="com.jenkin.common.module1api.OrderService" url="dubbo://localhost:20880" />
這樣連接就不會去使用註冊中心,一版不會在生產環境這樣使用
只訂閱
在使用dubbo開發的過程中,如果我們本地的應用需要調用一些其他的穩定服務,比如郵件,短信服務,但是呢,我們又不想發現在還沒開發好的服務註冊上去,所以我們本地就可以只用,不註冊
<dubbo:registry address="zookeeper://47.102.XXX.XXX:7000" register="false"/>
只註冊
同理,有了上面的只訂閱,那麼肯定就會有隻註冊,爲什麼會有隻註冊,因爲我們有時候如果有多個註冊中心,但是呢有一個註冊中心裏面的服務我們是不需要的,不過別人又會從這個註冊中心上面拉取我們的服務,所以這時候就需要只註冊了
<dubbo:registry address="zookeeper://47.102.XXX.1:7000" />
<dubbo:registry address="zookeeper://47.102.XXX.2:7000" subscribe="false" />
比如上面,我們需要提供服務到47.102.XXX.2 上面去,但是呢我們不需要從47.102.XXX.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>
多註冊中心
同一個服務註冊到不同的註冊中心
例如 helloService 同時註冊到了兩個註冊中心
<!-- 多註冊中心配置 -->
<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" />
不同服務不同註冊中心
例如:官網示例,HelloService註冊到chinaRegistry,DemoService註冊到intlRegistry
<!-- 多註冊中心配置 -->
<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" />
分開引用不同註冊中心的相同服務
比如:CRM 需同時調用中文站和國際站的 PC2 服務,PC2 在中文站和國際站均有部署,接口及版本號都一樣,但連的數據庫不一樣。
<!-- 多註冊中心配置 -->
<dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
<dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
<!-- 引用中文站服務 -->
<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" />
服務分組
當一個接口有多個實現類的時候,註冊的時候就可以對每個接口的實現進行分組
例如:
服務提供端 的配置:
這裏有一點需要注意,在使用了分組之後就不能再服務端指定方法級別的配置了,因爲dubbo會根據方法名把method標籤封裝成爲beanDefinition對象,如果重名,就會報錯。
服務消費端的配置:
xml文件配置好之後在代碼裏面直接引用就好
多版本
當一個接口實現,出現不兼容升級時,可以用版本號過渡,版本號不同的服務相互間不引用。
可以按照以下的步驟進行版本遷移:
- 在低壓力時間段,先升級一半提供者爲新版本
- 再將所有消費者升級爲新版本
- 然後將剩下的一半提供者升級爲新版本
服務端定義多個版本的服務
消費端定義需要使用的版本
使用模式和group的使用差不多
代碼調用
異步調用
dubbo 的異步調用
Dubbo 的異步調用是非阻塞的 NIO 調用,一個線程可同時併發調用多個遠程
服務,每個服務的調用都是非阻塞的,線程立即返回。就是對 java 中 Futrue
模式的擴展支持
- XML定義
<dubbo:method name="addOrderCost2Seconds" async="true" sent="true" return="true"/>
<dubbo:method name="getOrderCost2Seconds" async="true"/>
- 測試類
@Autowired
UserService userService;
/**
* 使用方式:
* 1、async 使用異步
* 2、sent 是否等待請求發送成功之後再返回,發送失敗直接拋異常
* 3、return 是否關心返回值
* <dubbo:method name="addOrderCost2Seconds" async="true" sent="true" return="true"/>
* 使用異步調用科明顯縮短接口的總耗費時長
* @return
* @throws ExecutionException
* @throws InterruptedException
*/
@GetMapping("/testAsyncCall")
public OrderDTO test() throws ExecutionException, InterruptedException {
userService.testAsyncCall();
return new OrderDTO();
}
上面測試對每個接口休眠了2秒,使用異步之後兩個接口的總執行時間爲2秒
事件通知
在調用之前、調用之後、出現異常時,
會觸發 oninvoke、onreturn、onthrow 三個事件,
可以配置當事件發生時,通知哪個類的哪個方法
- 配置代碼
<dubbo:method name="testEventNotice" oninvoke="eventService.oninvoke"
onreturn="eventService.onreturn"
onthrow="eventService.onthrow" />
- 測試用例
@Autowired
UserService userService;
/**
* 時間通知可以與異步結合使用
* 1、同步回調
* 僅僅使用回調
* 2、異步回調
* 加上aSync=‘true’
* 3、同步無回調
* 4、異步無回調
* 回調事件
* 使用方式
* <dubbo:method name="testEventNotice" oninvoke="eventService.oninvoke"
* onreturn="eventService.onreturn"
* onthrow="eventService.onthrow" />
* 1、onreturn
* 在方法返回的時候回調
* 2、oninvoke
* 在方法開始執行的是調用
* 3、onthrow
* 在方法出異常的時候調用
*
*
* @return
* @throws ExecutionException
* @throws InterruptedException
*/
@GetMapping("/testEventNotice")
public OrderDTO test() throws ExecutionException, InterruptedException {
userService.testEventNotice();
return new OrderDTO();
}
- 執行結果