Dubbo是阿里開源的一個分佈式服務框架,可是阿里內部用的倒是HSF(High-speed Service Framework)。下面看看怎麼使用吧。html
Dubbo是個RPC調用框架,本質上不須要依賴中間件就能夠完成點對點的通訊,可是實際生產環境中,在動態擴容和下線等狀況下,服務提供者和消費者的地址不多是一直固定的,因此須要一個固定的第三方讓雙方暴露服務和發現服務,這也就是註冊中心的存在乎義,Dubbo官方推薦ZooKeeper。下面在windows下單機部署ZooKeeper做爲演示。java
1.官網下載ZooKeeper,地址 http://zookeeper.apache.org/web
2.解壓後在conf路徑下將zoo_sample.cfg改爲zoo.cfg,默認數據文件會存放在C:\tmp\zookeeper,端口號是2181spring
3.在bin路徑下,雙擊zkServer.cmd,正常會出現下面apache
binding to port 0.0.0.0/0.0.0.0:2181
4.運行zkCli.cmd,正常會出現下面windows
WatchedEvent state:SyncConnected type:None path:null
Dubbo內部封裝了註冊,心跳,RPC調用等複雜邏輯,因此使用上很是簡單,只須要配置一下注冊中心地址和相關的服務提供便可完成。下面是完成服務提供和消費所須要改動的文件api
api模塊負責向其餘系統提供接口和模塊,做爲一個jar包分發出去,因此在這裏只須要定義一個facade接口,如瀏覽器
public interface TestFacade { String getDeptName(String id); }
api接口的實如今biz模塊中,而DeptServiceImpl文件中,須要將sal模塊中定義的bean導入mvc
public class TestFacadeImpl implements TestFacade { @Override public String getDeptName(String id) { return "部門名稱:"+id; } }
@Service public class DeptServiceImpl implements DeptService { @Autowired DeptDOMapper deptDOMapper; @Autowired TestClient testClient; @Override public List<DeptInfo> listDeptInfo(String name) { System.out.println(testClient.getDeptName("123")); List<DeptDO> deptDOList = deptDOMapper.listDept(name); List<DeptInfo> deptInfos = new ArrayList<>(); deptDOList.stream().forEach(x -> { DeptInfo info = new DeptInfo(); info.setName(x.getName()); info.setId(x.getId()); deptInfos.add(info); }); } }
sal負責調用其餘應用的服務,處理相應數據,而後再給biz模塊提供相應的服務,一般沒有複雜的邏輯都是直接透傳的,如app
public interface TestClient { String getDeptName(String id) ; }
@Service public class TestClientImpl implements TestClient { @Autowired TestFacade testFacade; @Override public String getDeptName(String id) { return testFacade.getDeptName(id); } }
另外還要在web模塊配置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="consumer-of-test-app" />--> <!-- 使用zookeeper註冊中心 --> <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/> <!-- 生成遠程服務代理,能夠和本地bean同樣使用 --> <dubbo:reference id="testFacade" check="false" interface="cn.com.test.springmvc.api.TestFacade" /> </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://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="test-app" /> <!-- 使用zookeeper註冊中心 --> <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="cn.com.test.springmvc.api.TestFacade" ref="testFacadeImpl" /> <!-- 和本地bean同樣實現服務 --> <bean id="testFacadeImpl" class="cn.com.test.springmvc.biz.apiimpl.TestFacadeImpl" /> </beans>
applicationContext.xml中加入引用資源
<import resource="provider.xml"></import> <import resource="consumer.xml"></import>
pom.xml加入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency>
這裏一切從簡就不拆成兩個應用了,一個應用充當服務者和消費者兩個角色,先運行zkServer.cmd,再運行應用,在瀏覽器輸入例如這樣的連接http://localhost:8080/dept/getList?name=1,會看控制檯看到輸出,如
部門名稱:123
調用成功。
Dubbo官方文檔 http://dubbo.apache.org/zh-cn/docs/user/preface/background.html