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

- 日理万妓 2022-01-13 04:05 317阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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

一、Dubbo简介

1.1、Dubbo是什么?

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

其核心部分包含:

  1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
  2. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
  3. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

1.2. Dubbo能做什么?

1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。

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

1.3. dubbo的架构

dubbo架构图如下所示:

05140229_ZZAt.jpg

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

05140230_Vw4l.jpg

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

首先,选择创建

05140230_ftYB.jpg

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

05140230_hgl5.jpg

最后,输入项目名等

05140230_zDK3.jpg

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

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. <!— 使用multicast广播注册中心暴露服务地址
  9. —>

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. <**dependency**>
  28. <**groupId**>org.springframework</**groupId**>
  29. <**artifactId**>spring-core</**artifactId**>
  30. <**version**>${spring.version}</**version**>
  31. </**dependency**>
  32. <**dependency**>
  33. <**groupId**>org.springframework</**groupId**>
  34. <**artifactId**>spring-beans</**artifactId**>
  35. <**version**>${spring.version}</**version**>
  36. </**dependency**>
  37. <**dependency**>
  38. <**groupId**>org.springframework</**groupId**>
  39. <**artifactId**>spring-context</**artifactId**>
  40. <**version**>${spring.version}</**version**>
  41. </**dependency**>
  42. <**dependency**>
  43. <**groupId**>org.springframework</**groupId**>
  44. <**artifactId**>spring-jdbc</**artifactId**>
  45. <**version**>${spring.version}</**version**>
  46. </**dependency**>
  47. <**dependency**>
  48. <**groupId**>org.springframework</**groupId**>
  49. <**artifactId**>spring-web</**artifactId**>
  50. <**version**>${spring.version}</**version**>
  51. </**dependency**>
  52. <**dependency**>
  53. <**groupId**>org.springframework</**groupId**>
  54. <**artifactId**>spring-webmvc</**artifactId**>
  55. <**version**>${spring.version}</**version**>
  56. </**dependency**>
  57. <**dependency**>
  58. <**groupId**>org.springframework</**groupId**>
  59. <**artifactId**>spring-aop</**artifactId**>
  60. <**version**>${spring.version}</**version**>
  61. </**dependency**>
  62. <**dependency**>
  63. <**groupId**>org.springframework</**groupId**>
  64. <**artifactId**>spring-tx</**artifactId**>
  65. <**version**>${spring.version}</**version**>
  66. </**dependency**>
  67. <**dependency**>
  68. <**groupId**>org.springframework</**groupId**>
  69. <**artifactId**>spring-orm</**artifactId**>
  70. <**version**>${spring.version}</**version**>
  71. </**dependency**>
  72. <**dependency**>
  73. <**groupId**>org.springframework</**groupId**>
  74. <**artifactId**>spring-context-support</**artifactId**>
  75. <**version**>${spring.version}</**version**>
  76. </**dependency**>
  77. <**dependency**>
  78. <**groupId**>org.springframework</**groupId**>
  79. <**artifactId**>spring-test</**artifactId**>
  80. <**version**>${spring.version}</**version**>
  81. </**dependency**>
  82. <**dependency**>
  83. <**groupId**>org.springframework</**groupId**>
  84. <**artifactId**>spring-jms</**artifactId**>
  85. <**version**>${spring.version}</**version**>
  86. </**dependency**>
  87. </**dependencies**>
  88. </**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,然后一直开着,不要关了!!!!!!!!!!!!!然后一直开着,不要关了!!!!!!!!!!!!!

05140230_rU3G.jpg

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

05140230_U1vl.jpg

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

05140230_egJw.jpg

05140231_m1re.jpg

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

三、消费者

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

05140231_f0Ex.jpg

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. <**dubbo:application name=”dubbo_consumer” />**
  9. <**dubbo:registry protocol=”zookeeper” address=”zookeeper://127.0.0.1:2181” />**
  10. <**dubbo:reference id=”providerService” interface=”com.lin.service.ProviderService” />**
  11. </**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. 4.0.0
  4. com.lin
  5. dubbo_consumer
  6. 0.0.1-SNAPSHOT
  7. 3.2.8.RELEASE
  8. com.lin
  9. dubbo_provider
  10. 0.0.1-SNAPSHOT
  11. com.alibaba
  12. dubbo
  13. 2.5.3
  14. org.springframework
  15. spring
  16. com.github.sgroschupf
  17. zkclient
  18. 0.1
  19. org.springframework
  20. spring-core
  21. ${spring.version}
  22. org.springframework
  23. spring-beans
  24. ${spring.version}
  25. org.springframework
  26. spring-context
  27. ${spring.version}
  28. org.springframework
  29. spring-jdbc
  30. ${spring.version}
  31. org.springframework
  32. spring-web
  33. ${spring.version}
  34. org.springframework
  35. spring-webmvc
  36. ${spring.version}
  37. org.springframework
  38. spring-aop
  39. ${spring.version}
  40. org.springframework
  41. spring-tx
  42. ${spring.version}
  43. org.springframework
  44. spring-orm
  45. ${spring.version}
  46. org.springframework
  47. spring-context-support
  48. ${spring.version}
  49. org.springframework
  50. spring-test
  51. ${spring.version}
  52. org.springframework
  53. spring-jms
  54. ${spring.version}

和提供者的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. }

然后启动:

05140231_wVcD.jpg

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

05140231_Ojt1.jpg

转载于:https://my.oschina.net/zhanghaiyang/blog/597688

发表评论

表情:
评论列表 (有 0 条评论,317人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Dubbo+Zookeeper 基础讲解

    一、dubbo是什么? 1)本质:一个Jar包,一个分布式框架,,一个远程服务调用的分布式框架。 既然是新手教学,肯定很多同学不明白什么是分布式和远程服务调用,为什么要

    相关 1Dubbo入门

    阿里的分布式框架,不废话,直接上demo 有demo 才能有干货,实践才是硬道理; 1、第一步,安装ZK 我这里是在阿里云上安装的单机zk,至于各位看客,本地开虚拟机

    相关 Dubbo入门实例

    导语   在之前的学习中了解了关于Dubbo的一些基本的概念,但是概念毕竟是概念,更多的还需要通过自己编写代码的方式进行进一步的提高下面就来编写第一个入门的Dubbo应用