dubbo學習
Dubbo是阿里巴巴內部的SOA服務化治理方案的核心框架,每天為2000+ 個服務提供3,000,000,000+ 次訪問量支持,并被廣泛應用于阿里巴巴集團的各成員站點。Dubbo自2011年開源后,已被許多非阿里系公司使用。
Dubbo是一個分布式服務框架,致力于提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
其核心部分包含:
-
遠程通訊: 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
-
集群容錯: 提供基于接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集群支持。
-
自動發現: 基于注冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。
Dubbo能做什么?
-
透明化的遠程方法調用,就像調用本地方法一樣調用遠程方法,只需簡單配置,沒有任何API侵入。
-
軟負載均衡及容錯機制,可在內網替代F5等硬件負載均衡器,降低成本,減少單點。
-
服務自動注冊與發現,不再需要寫死服務提供方地址,注冊中心基于接口名查詢服務提供者的IP地址,并且能夠平滑添加或刪除服務提供者。
Dubbo采用全Spring配置方式,透明化接入應用,對應用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴展進行加載。
實例:
服務提供方:HuoDong
定義服務接口: (該接口需單獨打包,在服務提供方和消費方共享)
Java代碼
public interface DemoService { public void sayHello(); }
在服務提供方實現接口:(對服務消費方隱藏實現)
Java代碼
public class DemoServiceImpl implements DemoService{ @Override public void sayHello() { System.out.println("hello zy!"); } }
用Spring配置聲明暴露服務:
applicationProvider.xml:
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="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代碼
<?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):
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的話,那么都要打包進去。