Dubbo学习总结(1)——Dubbo入门基础与实例讲解

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架构图如下所示:

节点角色说明:
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

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

首先,选择创建

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

最后,输入项目名等

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

1、service代码

接口层如下:ProviderService.java

[java]  view plain copy

  1. package com.lin.service;

  2. /**

  3. * 功能概要:提供者service接口层

  4. *

  5. * @author linbingwen

  6. * @since  2015年8月26日

  7. */

  8. public interface ProviderService {

  9. public String sayHello(String name);

  10. }

实现层如下:ProviderServiceImpl.java

[java]  view plain copy

  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. public String sayHello(String name) {

  10. return "Hello:~~~~~~~~~~~~~~~~~~~~~~~~"+name+"你好,你好~~";

  11. }

  12. }

2、Spring配置文件-applicationProvider.xml

[java]  view plain copy

  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. http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
  8. <!-- 具体的实现bean -->
  9. <bean id="providerService" class="com.lin.service.ProviderServiceImpl" />
  10. <!-- 提供方应用信息,用于计算依赖关系 -->
  11. <dubbo:application name="dubbo_provider" />
  12. <!-- 使用multicast广播注册中心暴露服务地址
  13. <dubbo:registry address="multicast://localhost:1234" />-->
  14. <!-- 使用zookeeper注册中心暴露服务地址 -->
  15. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  16. <!-- 用dubbo协议在20880端口暴露服务 -->
  17. <dubbo:protocol name="dubbo" port="29014" />
  18. <!-- 声明需要暴露的服务接口 -->
  19. <dubbo:service interface="com.lin.service.ProviderService" ref="providerService" />
  20. </beans>

3、pom.xml中依赖文件

[html]  view plain copy

  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. <dependencies>

  11. <dependency>

  12. <groupId>com.alibaba</groupId>

  13. <artifactId>dubbo</artifactId>

  14. <version>2.5.3</version>

  15. <exclusions>

  16. <exclusion>

  17. <groupId>org.springframework</groupId>

  18. <artifactId>spring</artifactId>

  19. </exclusion>

  20. </exclusions>

  21. </dependency>

  22. <dependency>

  23. <groupId>com.github.sgroschupf</groupId>

  24. <artifactId>zkclient</artifactId>

  25. <version>0.1</version>

  26. </dependency>

  27. <!-- spring相关 -->
  28. <dependency>

  29. <groupId>org.springframework</groupId>

  30. <artifactId>spring-core</artifactId>

  31. <version>${spring.version}</version>

  32. </dependency>

  33. <dependency>

  34. <groupId>org.springframework</groupId>

  35. <artifactId>spring-beans</artifactId>

  36. <version>${spring.version}</version>

  37. </dependency>

  38. <dependency>

  39. <groupId>org.springframework</groupId>

  40. <artifactId>spring-context</artifactId>

  41. <version>${spring.version}</version>

  42. </dependency>

  43. <dependency>

  44. <groupId>org.springframework</groupId>

  45. <artifactId>spring-jdbc</artifactId>

  46. <version>${spring.version}</version>

  47. </dependency>

  48. <dependency>

  49. <groupId>org.springframework</groupId>

  50. <artifactId>spring-web</artifactId>

  51. <version>${spring.version}</version>

  52. </dependency>

  53. <dependency>

  54. <groupId>org.springframework</groupId>

  55. <artifactId>spring-webmvc</artifactId>

  56. <version>${spring.version}</version>

  57. </dependency>

  58. <dependency>

  59. <groupId>org.springframework</groupId>

  60. <artifactId>spring-aop</artifactId>

  61. <version>${spring.version}</version>

  62. </dependency>

  63. <dependency>

  64. <groupId>org.springframework</groupId>

  65. <artifactId>spring-tx</artifactId>

  66. <version>${spring.version}</version>

  67. </dependency>

  68. <dependency>

  69. <groupId>org.springframework</groupId>

  70. <artifactId>spring-orm</artifactId>

  71. <version>${spring.version}</version>

  72. </dependency>

  73. <dependency>

  74. <groupId>org.springframework</groupId>

  75. <artifactId>spring-context-support</artifactId>

  76. <version>${spring.version}</version>

  77. </dependency>

  78. <dependency>

  79. <groupId>org.springframework</groupId>

  80. <artifactId>spring-test</artifactId>

  81. <version>${spring.version}</version>

  82. </dependency>

  83. <dependency>

  84. <groupId>org.springframework</groupId>

  85. <artifactId>spring-jms</artifactId>

  86. <version>${spring.version}</version>

  87. </dependency>

  88. </dependencies>

  89. </project>

4、启动提供者服务

在src/test/java下添加

ProviderServiceTest.java内容如下:

[java]  view plain copy

  1. package com.lin.service;

  2. import java.io.IOException;

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. /**

  5. * 功能概要:

  6. *

  7. * @author linbingwen

  8. * @since  2015年8月26日

  9. */

  10. public class ProviderServiceTest {

  11. /**

  12. * @author linbingwen

  13. * @since  2015年8月26日

  14. * @param args

  15. */

  16. public static void main(String[] args) {

  17. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

  18. new String[]{"applicationProvider.xml"});

  19. context.start();

  20. System.out.println("提供者服务已注册成功");

  21. System.out.println("请按任意键取消提供者服务");

  22. try {

  23. System.in.read();//让此程序一直跑,表示一直提供服务

  24. } catch (IOException e) {

  25. e.printStackTrace();

  26. }

  27. }

  28. }

运行结果:

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

**
**

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

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

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

三、消费者

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

2、Spring配置文件applicationConsumer.xml

[html]  view plain copy

  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. http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
  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文件如下

[java]  view plain copy

  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_consumer</artifactId>

  6. <version>0.0.1-SNAPSHOT</version>

  7. <properties>
  8. <spring.version>3.2.8.RELEASE</spring.version>

  9. </properties>
  10. <dependencies>
  11. <!-- 添加provider的jar包 -->
  12. <dependency>
  13. <groupId>com.lin</groupId>

  14. <artifactId>dubbo_provider</artifactId>

  15. <version>0.0.1-SNAPSHOT</version>

  16. </dependency>
  17. <!-- 添加dubbo依赖 -->
  18. <dependency>
  19. <groupId>com.alibaba</groupId>

  20. <artifactId>dubbo</artifactId>

  21. <version>2.5.3</version>

  22. <exclusions>
  23. <exclusion>
  24. <groupId>org.springframework</groupId>

  25. <artifactId>spring</artifactId>

  26. </exclusion>
  27. </exclusions>
  28. </dependency>
  29. <!-- 添加zk客户端依赖 -->
  30. <dependency>
  31. <groupId>com.github.sgroschupf</groupId>

  32. <artifactId>zkclient</artifactId>

  33. <version>0.1</version>

  34. </dependency>
  35. <!-- spring相关 -->
  36. <dependency>
  37. <groupId>org.springframework</groupId>

  38. <artifactId>spring-core</artifactId>

  39. <version>${spring.version}</version>

  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework</groupId>

  43. <artifactId>spring-beans</artifactId>

  44. <version>${spring.version}</version>

  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>

  48. <artifactId>spring-context</artifactId>

  49. <version>${spring.version}</version>

  50. </dependency>
  51. <dependency>
  52. <groupId>org.springframework</groupId>

  53. <artifactId>spring-jdbc</artifactId>

  54. <version>${spring.version}</version>

  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework</groupId>

  58. <artifactId>spring-web</artifactId>

  59. <version>${spring.version}</version>

  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>

  63. <artifactId>spring-webmvc</artifactId>

  64. <version>${spring.version}</version>

  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>

  68. <artifactId>spring-aop</artifactId>

  69. <version>${spring.version}</version>

  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>

  73. <artifactId>spring-tx</artifactId>

  74. <version>${spring.version}</version>

  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>

  78. <artifactId>spring-orm</artifactId>

  79. <version>${spring.version}</version>

  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework</groupId>

  83. <artifactId>spring-context-support</artifactId>

  84. <version>${spring.version}</version>

  85. </dependency>
  86. <dependency>
  87. <groupId>org.springframework</groupId>

  88. <artifactId>spring-test</artifactId>

  89. <version>${spring.version}</version>

  90. </dependency>
  91. <dependency>
  92. <groupId>org.springframework</groupId>

  93. <artifactId>spring-jms</artifactId>

  94. <version>${spring.version}</version>

  95. </dependency>
  96. </dependencies>
  97. </project>

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

4、消费者调用提供者

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

[java]  view plain copy

  1. package com.lin.service;

  2. import java.io.IOException;

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. /**

  5. * 功能概要:

  6. *

  7. * @author linbingwen

  8. * @since  2015年8月26日

  9. */

  10. public class ConsumerServiceTest {

  11. /**

  12. * @author linbingwen

  13. * @since  2015年8月26日

  14. * @param args

  15. */

  16. public static void main(String[] args) {

  17. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

  18. new String[] { "applicationConsumer.xml" });

  19. context.start();

  20. ProviderService providerService = (ProviderService) context.getBean("providerService");

  21. System.out.println(providerService.sayHello("林炳文Evankaka"));

  22. System.out.println("Press any key to exit.");

  23. try {

  24. System.in.read();

  25. } catch (IOException e) {

  26. e.printStackTrace();

  27. }

  28. }

  29. }

然后启动:

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

点赞
收藏

评论区

加载中...

相关推荐

springcloud知识点总结

一.SpringCloud面试题口述1.SpringCloud和DubboSpringCloud和Dubbo都是现在主流的微服务架构SpringCloud是Apache旗下的Spring体系下的微服务解决方案Dubbo是阿里系的分布式服务治理框架从技术维度上,其实SpringCloud远远的超过Dubbo,Dubbo本身只是实现

Dubbo架构设计与源码解析(二) 服务注册

作者:黄金一、Dubbo简介Dubbo是一款典型的高扩展、高性能、高可用的RPC微服务框架,用于解决微服务架构下的服务治理与通信问题。其核心模块包含【RPC通信】和【服务治理】,其中服务治理又分为服务注册与发现、服务容错、负载均衡、流量调度等。今天将重点介

Dubbo 学习

Dubbo概念:  Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求。Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务

Dubbo协议及序列化

Dubbo是Alibaba开源的分布式服务框架远程调用框架,在网络间传输数据,就需要通信协议和序列化。一通信协议Dubbo支持dubbo、rmi、hessian、http、webservice、thrift、redis等多种协议,但是Dubbo官网是推荐我们使用Dubbo协议的,默认也是用的dubbo协议。先介绍几种常见的协议:1\.

Dubbo 如何成为连接异构微服务体系的最佳服务开发框架

从编程开发的角度来说,ApacheDubbo(以下简称Dubbo)首先是一款RPC服务框架,它最大的优势在于提供了面向接口代理的服务编程模型,对开发者屏蔽了底层的远程通信细节。同时Dubbo也是一款服务治理框架,它为分布式部署的微服务提供了服务发现、流量调度等服务治理解决方案。在这篇文章中,我们将以以上基础能力为背景,尝试突破Dubbo

Dubbo概述

一、什么是Dubbo        Dubbo是一个分布式框架,以及SOA治理方案。其主要功能包括:高性能DIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等。它有5个节点,分别是Provider、Consumer、Registry、Monitor、Container。其中Prvider是服务提供者,C

Dubbo学习总结(1)——Dubbo入门基础与实例讲解 - HelloWorld