Dubbo 是阿里巴巴公司開源的一個高性能優秀的服務框架,使得應用可通過高性能的 RPC 實現服務的輸出和輸入功能,可以和
Spring框架無縫集成。
主要核心部件:
- Remoting: 網絡通信框架,實現了 sync-over-async 和 request-response 消息機制.
- RPC: (Remote Procedure Call Protocol遠程過程調用協議),一個遠程過程調用的抽象,支持負載均衡、容災和集群功能
- Registry: 服務目錄框架用於服務的注冊和服務事件發布和訂閱
Dubbo工作原理
- Provider
- 暴露服務方稱之為“服務提供者”。
- Consumer
- 調用遠程服務方稱之為“服務消費者”。
- Registry
- 服務注冊與發現的中心目錄服務稱之為“服務注冊中心”。
- Monitor
- 統計服務的調用次調和調用時間的日志服務稱之為“服務監控中心”。
(1) 連通性:
- 注冊中心負責服務地址的注冊與查找,相當於目錄服務,服務提供者和消費者只在啟動時與注冊中心交互,注冊中心不轉發請求,壓力較小
- 監控中心負責統計各服務調用次數,調用時間等,統計先在內存匯總后每分鍾一次發送到監控中心服務器,並以報表展示
- 服務提供者向注冊中心注冊其提供的服務,並匯報調用時間到監控中心,此時間不包含網絡開銷
- 服務消費者向注冊中心獲取服務提供者地址列表,並根據負載算法直接調用提供者,同時匯報調用時間到監控中心,此時間包含網絡開銷
- 注冊中心,服務提供者,服務消費者三者之間均為長連接,監控中心除外
- 注冊中心通過長連接感知服務提供者的存在,服務提供者宕機,注冊中心將立即推送事件通知消費者
- 注冊中心和監控中心全部宕機,不影響已運行的提供者和消費者,消費者在本地緩存了提供者列表
- 注冊中心和監控中心都是可選的,服務消費者可以直連服務提供者
(2) 健狀性:
- 監控中心宕掉不影響使用,只是丟失部分采樣數據
- 數據庫宕掉后,注冊中心仍能通過緩存提供服務列表查詢,但不能注冊新服務
- 注冊中心對等集群,任意一台宕掉后,將自動切換到另一台
- 注冊中心全部宕掉后,服務提供者和服務消費者仍能通過本地緩存通訊
- 服務提供者無狀態,任意一台宕掉后,不影響使用
- 服務提供者全部宕掉后,服務消費者應用將無法使用,並無限次重連等待服務提供者恢復
(3) 伸縮性:
- 注冊中心為對等集群,可動態增加機器部署實例,所有客戶端將自動發現新的注冊中心
- 服務提供者無狀態,可動態增加機器部署實例,注冊中心將推送新的服務提供者信息給消費者
例子
服務端
定義一個Service Interface:(
HelloService.java)
package com.alibaba.hello.api; public interface HelloService{ String sayHello(String name); }
接口的實現類:(
HelloServiceImpl.java)
package com.alibaba.hello.impl; import com.alibaba.hello.api.HelloService; public class HelloServiceImpl implements HelloService{ public String sayHello(String name){ return "Hello " + name; } }
Spring配置:(
provider.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans ......> <!-- Application name --> <dubbo:application name="hello-world-app" /> <!-- registry address, used for service to register itself --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- expose this service through dubbo protocol, through port 2 0 8 8 0 --> <dubbo:protocol name="dubbo" port="2 0 8 8 0" /> <!-- which service interface do we expose? --> <dubbo:service interface="com.alibaba.hello.api.HelloService" ref="helloService" /> <!-- designate implementation --> <bean id="helloService" class="com.alibaba.hello.impl.HelloServiceImpl" /> </beans>
測試代碼:(
Provider.java)
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); //啟動成功,監聽端口為2 0 8 8 0 System.in.read(); // 按任意鍵退出 } }
客戶端
Spring配置文件:(
consumer.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=......> <!-- consumer application name --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- registry address, used for consumer to discover services --> <dubbo:registry address="multicast://224.5.6.7:1234" /> <!-- which service to consume? --> <dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" /> </beans>
客戶端測試代碼:(
Consumer.java)
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.hello.api.HelloService; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); HelloService helloService = (HelloService)context.getBean("helloService"); // get service invocation proxy String hello = helloService.sayHello("world"); // do invoke! System.out.println( hello ); // cool, how are you~ } }
轉自:
http://www.oschina.net/p/dubbo
http://baike.baidu.com/view/6901431.htm#2