1.dubbo
dubbo是什么
Dubbo是一個(gè)分布式服務(wù)框架,致力于提供高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案,以及SOA服務(wù)治理方案。簡單的說,dubbo就是個(gè)服務(wù)框架,如果沒有分布式的需求,其實(shí)是不需要用的,只有在分布式的時(shí)候,才有dubbo這樣的分布式服務(wù)框架的需求,并且本質(zhì)上是個(gè)服務(wù)調(diào)用的東西,說白了就是個(gè)遠(yuǎn)程服務(wù)調(diào)用的分布式框架(告別Web Service模式中的WSdl,以服務(wù)者與消費(fèi)者的方式在dubbo上注冊)
其核心部分包含:
1. 遠(yuǎn)程通訊: 提供對多種基于長連接的NIO框架抽象封裝,包括多種線程模型,序列化,以及“請求-響應(yīng)”模式的信息交換方式。
2. 集群容錯(cuò): 提供基于接口方法的透明遠(yuǎn)程過程調(diào)用,包括多協(xié)議支持,以及軟負(fù)載均衡,失敗容錯(cuò),地址路由,動態(tài)配置等集群支持。
3. 自動發(fā)現(xiàn): 基于注冊中心目錄服務(wù),使服務(wù)消費(fèi)方能動態(tài)的查找服務(wù)提供方,使地址透明,使服務(wù)提供方可以平滑增加或減少機(jī)器。
dubbo能做什么
1.透明化的遠(yuǎn)程方法調(diào)用,就像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程方法,只需簡單配置,沒有任何API侵入。
2.軟負(fù)載均衡及容錯(cuò)機(jī)制,可在內(nèi)網(wǎng)替代F5等硬件負(fù)載均衡器,降低成本,減少單點(diǎn)。
3. 服務(wù)自動注冊與發(fā)現(xiàn),不再需要寫死服務(wù)提供方地址,注冊中心基于接口名查詢服務(wù)提供者的IP地址,并且能夠平滑添加或刪除服務(wù)提供者。
Dubbo采用全spring配置方式,透明化接入應(yīng)用,對應(yīng)用沒有任何API侵入,只需用Spring加載Dubbo的配置即可,Dubbo基于Spring的Schema擴(kuò)展進(jìn)行加載。
以上摘自博客
dubbo架構(gòu)
dubbo是一款高性能的java RPC框架,dubbo工作分為4個(gè)角色,分別是服務(wù)提供者,服務(wù)消費(fèi)者,注冊中心,監(jiān)控中心

節(jié)點(diǎn)角色說明

調(diào)用關(guān)系說明
0. 服務(wù)容器負(fù)責(zé)啟動,加載,運(yùn)行服務(wù)提供者。
1. 服務(wù)提供者在啟動時(shí),向注冊中心注冊自己提供的服務(wù)。
2. 服務(wù)消費(fèi)者在啟動時(shí),向注冊中心訂閱自己所需的服務(wù)。
3. 注冊中心返回服務(wù)提供者地址列表給消費(fèi)者,如果有變更,注冊中心將基于長連接推送變更數(shù)據(jù)給消費(fèi)者。
4. 服務(wù)消費(fèi)者,從提供者地址列表中,基于軟負(fù)載均衡算法,選一臺提供者進(jìn)行調(diào)用,如果調(diào)用失敗,再選另一臺調(diào)用。
5. 服務(wù)消費(fèi)者和提供者,在內(nèi)存中累計(jì)調(diào)用次數(shù)和調(diào)用時(shí)間,定時(shí)每分鐘發(fā)送一次統(tǒng)計(jì)數(shù)據(jù)到監(jiān)控中心。
單一應(yīng)用架構(gòu)
單一應(yīng)用架構(gòu)適用于當(dāng)一個(gè)網(wǎng)站流量很小時(shí),只需要一個(gè)應(yīng)用,把所有的功能都放到一個(gè)應(yīng)用中,將整個(gè)應(yīng)用打包部署到服務(wù)器里邊去。此時(shí),用于簡化增刪改查工作量的數(shù)據(jù)訪問框架(ORM)是關(guān)鍵。

垂直應(yīng)用架構(gòu)
當(dāng)訪問量逐漸增大時(shí),將一個(gè)大的應(yīng)用拆分成互不相干的小應(yīng)用,每一個(gè)小應(yīng)用從頁面,到業(yè)務(wù)邏輯,再到數(shù)據(jù)庫都是完整的。如果某一個(gè)小應(yīng)用的訪問量較大,可以把這個(gè)小的應(yīng)用部署到多臺服務(wù)器上去。

分布式服務(wù)架構(gòu)
當(dāng)垂直應(yīng)用越來越多,應(yīng)用之間的交互不可避免,應(yīng)用的界面也需要迎合多變的市場,所以提高業(yè)務(wù)復(fù)用及整合的分布式服務(wù)框架(RPC)是關(guān)鍵。分布式服務(wù)架構(gòu)將核心業(yè)務(wù)單獨(dú)抽取出來,作為獨(dú)立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心。比如一個(gè)用戶的應(yīng)用抽取成了用戶的web界面和用戶的核心業(yè)務(wù),其他的應(yīng)用同樣。這樣界面可以不斷的美化而不需要牽扯到核心業(yè)務(wù)。

2.dubbo實(shí)例
(1)創(chuàng)建一個(gè)maven空工程,作為項(xiàng)目的父工程
不選擇archetype,后面的各個(gè)模塊都是不選擇


配置pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo</groupId>
<artifactId>dubbo_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<motan.version>0.3.0</motan.version>
<dubbo.version>2.5.3</dubbo.version>
<dubbox.version>2.8.4</dubbox.version>
<spring.version>4.3.6.RELEASE</spring.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- spring相關(guān) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
<modules>
<module>dubbo_api</module>
<module>dubbo_consumer</module>
<module>dubbo_provider</module>
<module>dubbo_api</module>
<module>dubbo_provider</module>
<module>dubbo_consumer</module>
</modules>
</project>
(2)在dubbo_demo下創(chuàng)建新的模塊

創(chuàng)建dubbo_api模塊,直接Next,不選擇archetype



以創(chuàng)建dubbo_api模塊的同樣的方法創(chuàng)建dubbo_consumer模塊,dubbo_provider模塊
下面是三個(gè)模塊的pom文件
dubbo_api的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_api</artifactId>
<packaging>jar</packaging>
</project>
dubbo_provider的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_provider</artifactId>
<dependencies>
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
dubbo_consumer的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo_demo</artifactId>
<groupId>com.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo_consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.dubbo</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
(3)最后的項(xiàng)目結(jié)構(gòu)如下圖所示:

(4)搭建好基本的框架之后,就是一些接口、實(shí)現(xiàn)類和一些需要創(chuàng)建的配置文件
首先是dubbo_api,定義服務(wù)接口

package com.api.service;
/**
* 定義服務(wù)接口
*/
public interface DemoService {
String sayHello(String name);
}
然后在dubbo_provider創(chuàng)建中一個(gè)實(shí)現(xiàn)類DemoServiceImpl實(shí)現(xiàn)DemoService接口(因?yàn)樵趐om.xml文件中,將dubbo_api作為依賴,所以能引入DemoService接口)

類DemoServiceImpl
package com.provider.service;
import com.api.service.DemoService;
//實(shí)現(xiàn)DemoService接口(由于在pom.xml文件中,將dubbo_api作為依賴,故能引入上述接口)
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello "+name;
}
}
在dubbo_provider模塊中的resources需要創(chuàng)建兩個(gè)pom.xml文件,分別是dubbo-provider.xml和springmvc.xml配置文件
dubbo-provider.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">
<!-- 提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系 -->
<dubbo:application name="dubbo_provider" />
<!-- 使用zookeeper注冊中心暴露服務(wù)地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service interface="com.api.service.DemoService" ref="demoService" />
<!-- 接口實(shí)現(xiàn)類-->
<bean id="demoService" class="com.provider.service.DemoServiceImpl"/>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"
default-autowire="byName">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com" />
<import resource="classpath:dubbo-provider.xml" />
</beans>
在dubbo_provider模塊下寫一個(gè)測試類,ProviderTest

package com.provider.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
context.start();
System.out.println("Dubbo provider start...");
try {
System.in.read(); // 按任意鍵退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
在dubbo_consumer模塊中寫測試類ConsumerTest和配置文件springmvc.xml和dubbo-consumer.xml

dubbo-consumer.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 ">
<!-- 消費(fèi)方應(yīng)用名,用于計(jì)算依賴關(guān)系,不是匹配條件,不要與提供方一樣 -->
<dubbo:application name="dubbo_consumer" />
<!-- 使用multicast廣播注冊中心暴露發(fā)現(xiàn)服務(wù)地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 生成遠(yuǎn)程服務(wù)代理,可以和本地bean一樣使用demoService -->
<dubbo:reference id="demoService" interface="com.api.service.DemoService" />
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd"
default-autowire="byName">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com" />
<import resource="classpath:/dubbo-consumer.xml" />
</beans>
測試類ConsumerTest
package com.consumer;
import com.api.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ConsumerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println(demoService.sayHello("MrLf"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(5)整個(gè)dubbo的實(shí)例都已經(jīng)完成了。開始測試,啟動Zookeeper:zkServer.cmd。然后運(yùn)行服務(wù)生產(chǎn)者測試類ProviderTest,控制臺出現(xiàn)以下結(jié)果。

然后啟動服務(wù)消費(fèi)者的測試類ConsumerTest,控制臺出現(xiàn)以下結(jié)果

進(jìn)入Dubbo Admin查看運(yùn)行情況

