Dubbo是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點:javascript
那麼,Dubbo是什麼?
Dubbo是一個分佈式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。java
其核心部分包含:web
- 遠程通信: 提供對多種基於長鏈接的NIO框架抽象封裝,包括多種線程模型,序列化,以及「請求-響應」模式的信息交換方式。
- 集羣容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集羣支持。
- 自動發現: 基於註冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方能夠平滑增長或減小機器。
Dubbo能作什麼?
- 透明化的遠程方法調用,就像調用本地方法同樣調用遠程方法,只需簡單配置,沒有任何API侵入。
- 軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,下降成本,減小單點。
- 服務自動註冊與發現,再也不須要寫死服務提供方地址,註冊中心基於接口名查詢服務提供者的IP地址,而且可以平滑添加或刪除服務提供者。
Dubbo採用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置便可,Dubbo基於Spring的Schema擴展進行加載。spring
實例:服務器
服務提供方:HuoDongapp
定義服務接口: (該接口需單獨打包,在服務提供方和消費方共享)負載均衡
- public interface DemoService {
- public void sayHello();
- }
在服務提供方實現接口:(對服務消費方隱藏實現)框架
- public class DemoServiceImpl implements DemoService{
- @Override
- public void sayHello() {
- System.out.println("hello zy!");
- }
- }
用Spring配置聲明暴露服務:分佈式
applicationProvider.xml:ide
- <?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/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <dubbo:application name="hello-world-app" />
- <dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" />
- <dubbo:protocol name="dubbo" port="20880" />
- <dubbo:service interface="org.huodong.service.DemoService"
- ref="demoService" /> <!-- 和本地bean同樣實現服務 -->
- <bean id="demoService" class="org.huodong.service.DemoServiceImpl" />
- </beans>
ps:這裏是把服務器方和提供方都註冊到了zookeeper上統一管理。上面的IP爲安裝了zookeeper的服務器地址。若是不想用zookeeper管理的話,能夠改成
<dubbo:registry address="multicast://224.5.6.7:1234" />
服務消費者方 Fashion:
applicationConsumer.xml:
- <?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/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd
- ">
- <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 -->
- <dubbo:application name="consumer-of-helloworld-app" /> <!-- 使用multicast廣播註冊中心暴露發現服務地址 -->
- <dubbo:registry protocol="zookeeper" address="172.17.0.119:2181,172.17.0.120:2181,172.17.0.121:2181,172.17.0.122:2181,172.17.0.123:2181" /> <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
- <dubbo:reference id="demoService" interface="org.huodong.service.DemoService" />
- </beans>
調用遠程服務(ChatAction.java):
- public class ChatAction implements Controller {
- private DemoService demoService;
- public ModelAndView handleRequest(HttpServletRequest req,
- HttpServletResponse res) throws Exception {
- demoService.sayHello(); //調用提供方的方法
- return null;
- }
- public DemoService getDemoService() {
- return demoService;
- }
- public void setDemoService(DemoService demoService) {
- this.demoService = demoService;
- }
- }
注:須要的jar包,消費方和服務方都須要
而後把服務方的DemoService打成一個jar包,這裏只定義了一個方法,若是定義了好多個service或者service的實現中須要用到輔助類或javabean的話,那麼都要打包進去。
ps:在公司的zookeeper中經過頁面http://172.17.0.121:8080/governance/services能夠找到全部註冊的服務,而後能夠在這裏設置每一個服務所使用的提供者(IP地址等信息),一個服務能夠設置好多提供者(好比一個是服務器的,一個是本地的),可是隻能一個是啓用狀態,其他都是禁用狀態。