1.阿裡巴巴開源項目。
2.Dubbo是一個分布式服務框架,致力於提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
ps: SOA(面相服務的體系結構) RPC(遠程過程調用協議)
3.遠程服務調用的分布式框架(告別Web Service模式中的WSdl,以服務者與消費者的方式在dubbo上注冊)。
其核心部分包含:
遠程通訊: 提供對多種基於長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應”模式的信息交換方式。
集群容錯: 提供基於接口方法的透明遠程過程調用,包括多協議支持,以及軟負載均衡,失敗容錯,地址路由,動態配置等集群支持。
自動發現: 基於注冊中心目錄服務,使服務消費方能動態的查找服務提供方,使地址透明,使服務提供方可以平滑增加或減少機器。
節點角色說明:
調用關系說明:
(1) 連通性:
(2) 健狀性:
(3) 伸縮性:
(4) 升級性:
zookeeper是hadoop的一個子項目是分布式系統的可靠協調者,他提供了配置維護,名字服務,分布式同步等服務。
下載鏡像站:http://apache.fayea.com/zookeeper/
具體安裝步驟自行谷歌百度,分分鐘搞起。
applicationContext:
<?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="xixi_provider" /> <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20889" /> <!-- 聲明需要暴露的服務接口 --> <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" /> <!-- 具體的實現bean --> <bean id="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" /> </beans>
DemoService:
package com.unj.dubbotest.provider; import java.util.List; public interface DemoService { String sayHello(UserBean name); public List getUsers(); }
UserBean:
package com.unj.dubbotest.provider; import java.io.Serializable; public class UserBean implements Serializable{ private static final long serialVersionUID = 8364702127381119446L; private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
DemoServiceImpl:
package com.unj.dubbotest.provider.impl; import java.util.ArrayList; import java.util.List; import com.unj.dubbotest.provider.DemoService; import com.unj.dubbotest.provider.UserBean; public class DemoServiceImpl implements DemoService { public String sayHello(UserBean user) { return "Hello " + user.getUserName(); } public List getUsers() { List list = new ArrayList(); User u1 = new User(); u1.setName("jack"); u1.setAge(20); u1.setSex("m"); User u2 = new User(); u2.setName("tom"); u2.setAge(21); u2.setSex("m"); User u3 = new User(); u3.setName("rose"); u3.setAge(19); u3.setSex("w"); list.add(u1); list.add(u2); list.add(u3); return list; } }
本地測試方法,模擬服務開啟:
package com.unj.dubbotest.provider.impl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); System.in.read(); // 為保證服務一直開著,利用輸入流的阻塞來模擬 } }
applicationContext
<?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="hehe_consumer" /> <!-- 使用zookeeper注冊中心暴露服務地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成遠程服務代理,可以像使用本地bean一樣使用demoService --> <dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" /> </beans>
DemoService
package com.unj.dubbotest.provider; import java.util.List; import com.unj.dubbotest.provider.UserBean; public interface DemoService { String sayHello(UserBean user); public List getUsers(); }
在消費者這邊需要維護和提供者一樣的一個javabean
UserBean
package com.unj.dubbotest.provider; import java.io.Serializable; public class UserBean implements Serializable{ private static final long serialVersionUID = 8364702127381119446L; private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Consumer
package com.alibaba.dubbo.demo.pp; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.unj.dubbotest.provider.DemoService; import com.unj.dubbotest.provider.UserBean; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); UserBean user = new UserBean(); user.setUserName("tom"); String hello = demoService.sayHello(user); System.out.println(hello); List list = demoService.getUsers(); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } System.in.read(); } }
本文資料來源:
阿裡巴巴dubbo《用戶指南》:http://dubbo.io/User+Guide-zh.htm