​​林炳文Evankaka

           Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

一、Dubbo简介

 

 

1.1、Dubbo是什么?

        Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架

其核心部分包含:

1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。

2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。

3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

 

 

1.2. Dubbo能做什么?

1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。 

2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

3. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

 

 

 

1.3. dubbo的架构

dubbo架构图如下所示:

 

Dubbo入门基础与实例讲解(转)_ide

节点角色说明:

Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

调用关系说明:
0 服务容器负责启动,加载,运行服务提供者。
1. 服务提供者在启动时,向注册中心注册自己提供的服务。
2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

1.4. dubbo使用方法

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐)

Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

 

下面以一个实例来做说明,看这个实例前,建议先看看这里 ​​Dubbo-Admin管理平台和Zookeeper注册中心的搭建​​

 

二、服务提供者

提供者的目录如下:工程下载地址:http://download.csdn.net/detail/evankaka/9054253

Dubbo入门基础与实例讲解(转)_ide_02

其中初始化工程的创建如下:

首先,选择创建

Dubbo入门基础与实例讲解(转)_xml_03

然后,next,记得红框打上,这里直接快速

Dubbo入门基础与实例讲解(转)_xml_04

最后,输入项目名等

Dubbo入门基础与实例讲解(转)_spring_05

好了,项目有了,接下来就是加代码了

1、service代码

接口层如下:ProviderService.java

 

1. package com.lin.service;  
2.
3. /**
4. * 功能概要:提供者service接口层
5. *
6. * @author linbingwen
7. * @since 2015年8月26日
8. */
9. public interface ProviderService {
10. public String sayHello(String name);
11.
12. }

实现层如下:ProviderServiceImpl.java

 

1. package com.lin.service;  
2. /**
3. * 功能概要:功能概要:提供者service实现层
4. *
5. * @author linbingwen
6. * @since 2015年8月26日
7. */
8. public class ProviderServiceImpl implements ProviderService {
9.
10. public String sayHello(String name) {
11. return "Hello:~~~~~~~~~~~~~~~~~~~~~~~~"+name+"你好,你好~~";
12. }
13.
14. }

2、Spring配置文件-applicationProvider.xml

1. <?xml version="1.0" encoding="UTF-8"?>    
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. "http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4. //www.springframework.org/schema/beans
5. //www.springframework.org/schema/beans/spring-beans.xsd
6. //code.alibabatech.com/schema/dubbo
7. //code.alibabatech.com/schema/dubbo/dubbo.xsd ">
8. <!-- 具体的实现bean -->
9. "providerService" class="com.lin.service.ProviderServiceImpl" />
10. <!-- 提供方应用信息,用于计算依赖关系 -->
11. "dubbo_provider" />
12. <!-- 使用multicast广播注册中心暴露服务地址
13. "multicast://localhost:1234" />-->
14. <!-- 使用zookeeper注册中心暴露服务地址 -->
15. "zookeeper://127.0.0.1:2181" />
16. 20880端口暴露服务 -->
17. "dubbo" port="29014" />
18. <!-- 声明需要暴露的服务接口 -->
19. interface="com.lin.service.ProviderService" ref="providerService" />
20. </beans>

 

3、pom.xml中依赖文件

 

1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3. <modelVersion>4.0.0</modelVersion>
4. <groupId>com.lin</groupId>
5. <artifactId>dubbo_provider</artifactId>
6. <version>0.0.1-SNAPSHOT</version>
7. <properties>
8. <spring.version>3.2.8.RELEASE</spring.version>
9. </properties>
10.
11. <dependencies>
12. <dependency>
13. <groupId>com.alibaba</groupId>
14. <artifactId>dubbo</artifactId>
15. <version>2.5.3</version>
16. <exclusions>
17. <exclusion>
18. <groupId>org.springframework</groupId>
19. <artifactId>spring</artifactId>
20. </exclusion>
21. </exclusions>
22. </dependency>
23. <dependency>
24. <groupId>com.github.sgroschupf</groupId>
25. <artifactId>zkclient</artifactId>
26. <version>0.1</version>
27. </dependency>
28. <!-- spring相关 -->
29. <dependency>
30. <groupId>org.springframework</groupId>
31. <artifactId>spring-core</artifactId>
32. <version>${spring.version}</version>
33. </dependency>
34. <dependency>
35. <groupId>org.springframework</groupId>
36. <artifactId>spring-beans</artifactId>
37. <version>${spring.version}</version>
38. </dependency>
39. <dependency>
40. <groupId>org.springframework</groupId>
41. <artifactId>spring-context</artifactId>
42. <version>${spring.version}</version>
43. </dependency>
44. <dependency>
45. <groupId>org.springframework</groupId>
46. <artifactId>spring-jdbc</artifactId>
47. <version>${spring.version}</version>
48. </dependency>
49. <dependency>
50. <groupId>org.springframework</groupId>
51. <artifactId>spring-web</artifactId>
52. <version>${spring.version}</version>
53. </dependency>
54. <dependency>
55. <groupId>org.springframework</groupId>
56. <artifactId>spring-webmvc</artifactId>
57. <version>${spring.version}</version>
58. </dependency>
59. <dependency>
60. <groupId>org.springframework</groupId>
61. <artifactId>spring-aop</artifactId>
62. <version>${spring.version}</version>
63. </dependency>
64. <dependency>
65. <groupId>org.springframework</groupId>
66. <artifactId>spring-tx</artifactId>
67. <version>${spring.version}</version>
68. </dependency>
69. <dependency>
70. <groupId>org.springframework</groupId>
71. <artifactId>spring-orm</artifactId>
72. <version>${spring.version}</version>
73. </dependency>
74. <dependency>
75. <groupId>org.springframework</groupId>
76. <artifactId>spring-context-support</artifactId>
77. <version>${spring.version}</version>
78. </dependency>
79. <dependency>
80. <groupId>org.springframework</groupId>
81. <artifactId>spring-test</artifactId>
82. <version>${spring.version}</version>
83. </dependency>
84. <dependency>
85. <groupId>org.springframework</groupId>
86. <artifactId>spring-jms</artifactId>
87. <version>${spring.version}</version>
88. </dependency>
89. </dependencies>
90. </project>


4、启动提供者服务

 

在src/test/java下添加

ProviderServiceTest.java内容如下:


1. package com.lin.service;  
2.
3. import java.io.IOException;
4.
5. import org.springframework.context.support.ClassPathXmlApplicationContext;
6.
7. /**
8. * 功能概要:
9. *
10. * @author linbingwen
11. * @since 2015年8月26日
12. */
13. public class ProviderServiceTest {
14.
15. /**
16. * @author linbingwen
17. * @since 2015年8月26日
18. * @param args
19. */
20. public static void main(String[] args) {
21. new ClassPathXmlApplicationContext(
22. new String[]{"applicationProvider.xml"});
23. context.start();
24. "提供者服务已注册成功");
25. "请按任意键取消提供者服务");
26. try {
27. //让此程序一直跑,表示一直提供服务
28. catch (IOException e) {
29. e.printStackTrace();
30. }
31. }
32.
33. }

运行结果:

因为我这里使用的是本地的zookeeper注册中心,所以一定要先打开它!!!一定要先打开它!!!一定要先打开它!!!打开D:\Java\Tool\zookeeper-3.4.6\bin下的zkServer.cmd,然后一直开着,不要关了!!!!!!!!!!!!!然后一直开着,不要关了!!!!!!!!!!!!!

Dubbo入门基础与实例讲解(转)_spring_06

接着运行上面的main方法,输出如下:

Dubbo入门基础与实例讲解(转)_ide_07

然后我们可以到注册中心去看看服务注册上去了没有。(注意,这里的zookeper注册中心在我本地电脑已搭建好,搭建过程看这里​​Dubbo-Admin管理平台和Zookeeper注册中心的搭建​​)

Dubbo入门基础与实例讲解(转)_spring_08

Dubbo入门基础与实例讲解(转)_xml_09

可以看到,提供者已注册成功。

三、消费者

1、还是一个maven项目,整个结构如下,工程下载地址:http://download.csdn.net/detail/evankaka/9054253

Dubbo入门基础与实例讲解(转)_ide_10

2、Spring配置文件applicationConsumer.xml

 

1. <?xml version="1.0" encoding="UTF-8"?>    
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
4. xsi:schemaLocation="http://www.springframework.org/schema/beans
5. ​​ http://www.springframework.org/schema/beans/spring-beans.xsd ​​
6. ​​ http://code.alibabatech.com/schema/dubbo ​​
7. >
8. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
9. <dubbo:application name="dubbo_consumer" />
10. <!-- 使用multicast广播注册中心暴露发现服务地址 -->
11. <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
12. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
13. <dubbo:reference id="providerService" interface="com.lin.service.ProviderService" />
14. </beans>

3、pom.xml文件如下

 

1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
2. "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3. 4.0.0</modelVersion>
4. <groupId>com.lin</groupId>
5. <artifactId>dubbo_consumer</artifactId>
6. 0.0.1-SNAPSHOT</version>
7.
8. <properties>
9. 3.2.8.RELEASE</spring.version>
10. </properties>
11.
12. <dependencies>
13. <!-- 添加provider的jar包 -->
14. <dependency>
15. <groupId>com.lin</groupId>
16. <artifactId>dubbo_provider</artifactId>
17. 0.0.1-SNAPSHOT</version>
18. </dependency>
19. <!-- 添加dubbo依赖 -->
20. <dependency>
21. <groupId>com.alibaba</groupId>
22. <artifactId>dubbo</artifactId>
23. 2.5.3</version>
24. <exclusions>
25. <exclusion>
26. <groupId>org.springframework</groupId>
27. <artifactId>spring</artifactId>
28. </exclusion>
29. </exclusions>
30. </dependency>
31. <!-- 添加zk客户端依赖 -->
32. <dependency>
33. <groupId>com.github.sgroschupf</groupId>
34. <artifactId>zkclient</artifactId>
35. 0.1</version>
36. </dependency>
37. <!-- spring相关 -->
38. <dependency>
39. <groupId>org.springframework</groupId>
40. <artifactId>spring-core</artifactId>
41. <version>${spring.version}</version>
42. </dependency>
43. <dependency>
44. <groupId>org.springframework</groupId>
45. <artifactId>spring-beans</artifactId>
46. <version>${spring.version}</version>
47. </dependency>
48. <dependency>
49. <groupId>org.springframework</groupId>
50. <artifactId>spring-context</artifactId>
51. <version>${spring.version}</version>
52. </dependency>
53. <dependency>
54. <groupId>org.springframework</groupId>
55. <artifactId>spring-jdbc</artifactId>
56. <version>${spring.version}</version>
57. </dependency>
58. <dependency>
59. <groupId>org.springframework</groupId>
60. <artifactId>spring-web</artifactId>
61. <version>${spring.version}</version>
62. </dependency>
63. <dependency>
64. <groupId>org.springframework</groupId>
65. <artifactId>spring-webmvc</artifactId>
66. <version>${spring.version}</version>
67. </dependency>
68. <dependency>
69. <groupId>org.springframework</groupId>
70. <artifactId>spring-aop</artifactId>
71. <version>${spring.version}</version>
72. </dependency>
73. <dependency>
74. <groupId>org.springframework</groupId>
75. <artifactId>spring-tx</artifactId>
76. <version>${spring.version}</version>
77. </dependency>
78. <dependency>
79. <groupId>org.springframework</groupId>
80. <artifactId>spring-orm</artifactId>
81. <version>${spring.version}</version>
82. </dependency>
83. <dependency>
84. <groupId>org.springframework</groupId>
85. <artifactId>spring-context-support</artifactId>
86. <version>${spring.version}</version>
87. </dependency>
88. <dependency>
89. <groupId>org.springframework</groupId>
90. <artifactId>spring-test</artifactId>
91. <version>${spring.version}</version>
92. </dependency>
93. <dependency>
94. <groupId>org.springframework</groupId>
95. <artifactId>spring-jms</artifactId>
96. <version>${spring.version}</version>
97. </dependency>
98. </dependencies>
99. </project>

和提供者的POM文件基本上是一样的,只不过加了对提供者的依赖。

4、消费者调用提供者

在src/test/java写一个ConsumerServiceTest.java

 

1. package com.lin.service;  
2.
3. import java.io.IOException;
4.
5. import org.springframework.context.support.ClassPathXmlApplicationContext;
6.
7. /**
8. * 功能概要:
9. *
10. * @author linbingwen
11. * @since 2015年8月26日
12. */
13. public class ConsumerServiceTest {
14.
15. /**
16. * @author linbingwen
17. * @since 2015年8月26日
18. * @param args
19. */
20. public static void main(String[] args) {
21. new ClassPathXmlApplicationContext(
22. new String[] { "applicationConsumer.xml" });
23.
24. context.start();
25. "providerService");
26.
27. "林炳文Evankaka"));
28. "Press any key to exit.");
29. try {
30. System.in.read();
31. catch (IOException e) {
32. e.printStackTrace();
33. }
34.
35. }
36.
37. }

然后启动:

 

Dubbo入门基础与实例讲解(转)_spring_11

再打开注册中心,发面有消费者在使用提供者,因为都是一个电脑,所以IP都一样。当然。其它电脑也可以访问这个提供者的!

Dubbo入门基础与实例讲解(转)_xml_12