Dubbo入门教程

朴灿烈づ我的快乐病毒、 2021-11-04 23:18 476阅读 0赞

Dubbo

一款分布式服务框架
高性能和透明化的RPC远程服务调用方案
SOA服务治理方案
每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务中。

Dubbo架构

在这里插入图片描述
ps:图片来源于网络

Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次数和调用时间的监控中心。

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

Dubbo注册中心
对于服务提供方,它需要发布服务,而且由于应用系统的复杂性,服务的数量、类型也不断膨胀;
对于服务消费方,它最关心如何获取到它所需要的服务,而面对复杂的应用系统,需要管理大量的服务调用。
而且,对于服务提供方和服务消费方来说,他们还有可能兼具这两种角色,即既需要提供服务,有需要消费服务。

通过将服务统一管理起来,可以有效地优化内部应用对服务发布/使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一。

Dubbo提供的注册中心有如下几种类型可供选择:

Multicast注册中心
Zookeeper注册中心
Redis注册中心
Simple注册中心
Dubbo优缺点

优点

透明化的远程方法调用

  • 像调用本地方法一样调用远程方法;只需简单配置,没有任何API侵入。
    软负载均衡及容错机制
    可在内网替代nginx lvs等硬件负载均衡器。
    服务注册中心自动注册 & 配置管理
    -不需要写死服务提供者地址,注册中心基于接口名自动查询提供者ip。
    使用类似zookeeper等分布式协调服务作为服务注册中心,可以将绝大部分项目配置移入zookeeper集群。
    服务接口监控与治理
    -Dubbo-admin与Dubbo-monitor提供了完善的服务接口管理与监控功能,针对不同应用的不同接口,可以进行 多版本,多协议,多注册中心管理。

缺点

只支持JAVA语言
Dubbo入门Demo
了解了Dubbo以后,自然要搭建一个简单的Demo实现。本文采用Dubbo与Zookeeper、Spring框架的整合。

主要是以下几个步骤:

  1. 安装Zookeeper,启动;
  2. 创建MAVEN项目,构建Dubbo+Zookeeper+Spring实现的简单Demo;
  3. 安装Dubbo-admin,实现监控。

1、 Zookeeper介绍与安装
本Demo中的Dubbo注册中心采用的是Zookeeper。为什么采用Zookeeper呢?

Zookeeper是一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心。

Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求

具体的安装方法在此不一一叙述,可参考博文:zookeeper安装教程
安装完成后,进入到bin目录,并且启动zkServer.cmd,这个脚本中会启动一个java进程:
(注:需要先启动zookeeper后,后续dubbo demo代码运行才能使用zookeeper注册中心的功能)
在这里插入图片描述

2 、创建MAVEN项目

项目结构:
主要分三大模块:
dubbo-api : 存放定义的服务接口;
dubbo-consumer : 服务消费者;
dubbo-provider : 服务提供者(也是服务接口实现者)

在这里插入图片描述

公共依赖pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>dubbo</groupId>
  5. <artifactId>dubbo</artifactId>
  6. <version>1.0.0</version>
  7. <packaging>pom</packaging>
  8. <name>this is fist dubbo demo</name>
  9. <modules>
  10. <module>dubbo-consumer</module>
  11. <module>dubbo-provider</module>
  12. <module>dubbo-api</module>
  13. </modules>
  14. <properties>
  15. <maven.compiler.source>1.8</maven.compiler.source>
  16. <maven.compiler.target>1.8</maven.compiler.target>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  19. <!-- spring版本号 -->
  20. <spring.version>4.2.5.RELEASE</spring.version>
  21. <!-- mybatis版本号 -->
  22. <mybatis.version>3.2.8</mybatis.version>
  23. <!-- mysql驱动版本号 -->
  24. <mysql-driver.version>5.1.29</mysql-driver.version>
  25. <!-- log4j日志包版本号 -->
  26. <slf4j.version>1.7.18</slf4j.version>
  27. <log4j.version>1.2.17</log4j.version>
  28. </properties>
  29. <dependencies>
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>3.8.1</version>
  34. <scope>test</scope>
  35. </dependency>
  36. <!-- 添加jstl依赖 -->
  37. <dependency>
  38. <groupId>jstl</groupId>
  39. <artifactId>jstl</artifactId>
  40. <version>1.2</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>javax</groupId>
  44. <artifactId>javaee-api</artifactId>
  45. <version>7.0</version>
  46. </dependency>
  47. <!-- 添加junit4依赖 -->
  48. <dependency>
  49. <groupId>junit</groupId>
  50. <artifactId>junit</artifactId>
  51. <version>4.11</version>
  52. <!-- 指定范围,在测试时才会加载 -->
  53. <scope>test</scope>
  54. </dependency>
  55. <!-- 添加spring核心依赖 -->
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-core</artifactId>
  59. <version>${spring.version}</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-web</artifactId>
  64. <version>${spring.version}</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-oxm</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-jdbc</artifactId>
  79. <version>${spring.version}</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework</groupId>
  83. <artifactId>spring-webmvc</artifactId>
  84. <version>${spring.version}</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.springframework</groupId>
  88. <artifactId>spring-context</artifactId>
  89. <version>${spring.version}</version>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.springframework</groupId>
  93. <artifactId>spring-context-support</artifactId>
  94. <version>${spring.version}</version>
  95. </dependency>
  96. <dependency>
  97. <groupId>org.springframework</groupId>
  98. <artifactId>spring-aop</artifactId>
  99. <version>${spring.version}</version>
  100. </dependency>
  101. <dependency>
  102. <groupId>org.springframework</groupId>
  103. <artifactId>spring-test</artifactId>
  104. <version>${spring.version}</version>
  105. </dependency>
  106. <!-- 添加mybatis依赖 -->
  107. <dependency>
  108. <groupId>org.mybatis</groupId>
  109. <artifactId>mybatis</artifactId>
  110. <version>${mybatis.version}</version>
  111. </dependency>
  112. <!-- 添加mybatis/spring整合包依赖 -->
  113. <dependency>
  114. <groupId>org.mybatis</groupId>
  115. <artifactId>mybatis-spring</artifactId>
  116. <version>1.2.2</version>
  117. </dependency>
  118. <!-- 添加mysql驱动依赖 -->
  119. <dependency>
  120. <groupId>mysql</groupId>
  121. <artifactId>mysql-connector-java</artifactId>
  122. <version>${mysql-driver.version}</version>
  123. </dependency>
  124. <!-- 添加数据库连接池依赖 -->
  125. <dependency>
  126. <groupId>commons-dbcp</groupId>
  127. <artifactId>commons-dbcp</artifactId>
  128. <version>1.2.2</version>
  129. </dependency>
  130. <!-- 添加fastjson -->
  131. <dependency>
  132. <groupId>com.alibaba</groupId>
  133. <artifactId>fastjson</artifactId>
  134. <version>1.2.22</version>
  135. </dependency>
  136. <!-- 添加日志相关jar包 -->
  137. <dependency>
  138. <groupId>log4j</groupId>
  139. <artifactId>log4j</artifactId>
  140. <version>${log4j.version}</version>
  141. </dependency>
  142. <dependency>
  143. <groupId>org.slf4j</groupId>
  144. <artifactId>slf4j-api</artifactId>
  145. <version>${slf4j.version}</version>
  146. </dependency>
  147. <dependency>
  148. <groupId>org.slf4j</groupId>
  149. <artifactId>slf4j-log4j12</artifactId>
  150. <version>${slf4j.version}</version>
  151. </dependency>
  152. <!-- log end -->
  153. <!-- 映入JSON -->
  154. <dependency>
  155. <groupId>org.codehaus.jackson</groupId>
  156. <artifactId>jackson-mapper-asl</artifactId>
  157. <version>1.9.13</version>
  158. </dependency>
  159. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->;
  160. <dependency>
  161. <groupId>com.fasterxml.jackson.core</groupId>
  162. <artifactId>jackson-core</artifactId>
  163. <version>2.8.0</version>
  164. </dependency>
  165. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->;
  166. <dependency>
  167. <groupId>com.fasterxml.jackson.core</groupId>
  168. <artifactId>jackson-databind</artifactId>
  169. <version>2.8.0</version>
  170. </dependency>
  171. <dependency>
  172. <groupId>commons-fileupload</groupId>
  173. <artifactId>commons-fileupload</artifactId>
  174. <version>1.3.1</version>
  175. </dependency>
  176. <dependency>
  177. <groupId>commons-io</groupId>
  178. <artifactId>commons-io</artifactId>
  179. <version>2.4</version>
  180. </dependency>
  181. <dependency>
  182. <groupId>commons-codec</groupId>
  183. <artifactId>commons-codec</artifactId>
  184. <version>1.9</version>
  185. </dependency>
  186. <dependency>
  187. <groupId>org.quartz-scheduler</groupId>
  188. <artifactId>quartz</artifactId>
  189. <version>2.2.1</version>
  190. </dependency>
  191. <dependency>
  192. <groupId>org.apache.shiro</groupId>
  193. <artifactId>shiro-core</artifactId>
  194. <version>1.3.2</version>
  195. </dependency>
  196. <dependency>
  197. <groupId>org.apache.shiro</groupId>
  198. <artifactId>shiro-web</artifactId>
  199. <version>1.3.2</version>
  200. </dependency>
  201. <dependency>
  202. <groupId>org.apache.shiro</groupId>
  203. <artifactId>shiro-spring</artifactId>
  204. <version>1.3.2</version>
  205. </dependency>
  206. <dependency>
  207. <groupId>org.apache.shiro</groupId>
  208. <artifactId>shiro-ehcache</artifactId>
  209. <version>1.3.2</version>
  210. </dependency>
  211. <dependency>
  212. <groupId>org.apache.zookeeper</groupId>
  213. <artifactId>zookeeper</artifactId>
  214. <version>3.4.9</version>
  215. </dependency>
  216. <!-- dubbo -->
  217. <dependency>
  218. <groupId>com.alibaba</groupId>
  219. <artifactId>dubbo</artifactId>
  220. <version>2.5.3</version>
  221. <exclusions>
  222. <exclusion>
  223. <groupId>org.springframework</groupId>
  224. <artifactId>spring</artifactId>
  225. </exclusion>
  226. </exclusions>
  227. </dependency>
  228. <dependency>
  229. <groupId>com.101tec</groupId>
  230. <artifactId>zkclient</artifactId>
  231. <version>0.10</version>
  232. </dependency>
  233. </dependencies>
  234. <build>
  235. <finalName>dubbo</finalName>
  236. </build>
  237. </project>

下面,我为大家具体讲解三个子项目。

1、服务接口dubbo-api

pom.xml

  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>dubbo</groupId>
  7. <artifactId>dubbo</artifactId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <groupId>dubbo</groupId>
  11. <artifactId>dubbo-api</artifactId>
  12. <version>1.0.0</version>
  13. <name>dubbo-api</name>
  14. <url>http://maven.apache.org</url>
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>3.8.1</version>
  23. <scope>test</scope>
  24. </dependency>
  25. </dependencies>
  26. </project>

DemoService.java

  1. package com.dubbo.api.demo;
  2. /**
  3. *
  4. * @ClassName: DemoService
  5. * @Description: 定义服务接口
  6. * @author chenqi
  7. * @date 2018年10月4日
  8. *
  9. */
  10. public interface DemoService {
  11. String helloDubbo(String str);
  12. }

2、 服务提供者(也是服务接口实现者)dubbo-provider

pom依赖

  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>dubbo</groupId>
  7. <artifactId>dubbo</artifactId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <artifactId>dubbo-provider</artifactId>
  11. <name>dubbo-provider</name>
  12. <description>服务提供者</description>
  13. <url>http://maven.apache.org</url>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17. <dependencies>
  18. <!-- 引入api模块 -->
  19. <dependency>
  20. <groupId>dubbo</groupId>
  21. <artifactId>dubbo-api</artifactId>
  22. <version>${project.version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>3.8.1</version>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. </project>

配置文件provider.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"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  9. <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
  10. <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
  11. <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
  12. <dubbo:registry address="zookeeper://localhost:2181"/>
  13. <!-- 用dubbo协议在20880端口暴露服务 -->
  14. <dubbo:protocol name="dubbo" port="20880" />
  15. <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
  16. <dubbo:service interface="com.dubbo.api.demo.DemoService" ref="demoService" protocol="dubbo" />
  17. <!--具体实现该接口的 bean-->
  18. <bean id="demoService" class="com.dubbo.provider.demo.DemoServiceImpl"/>
  19. </beans>

服务接口实现类(用于提供服务)
实现服务接口,此实现对消费者隐藏

  1. package com.dubbo.provider.demo;
  2. import com.dubbo.api.demo.DemoService;
  3. /**
  4. *
  5. * @ClassName: DemoServiceImpl
  6. * @Description: 服务接口实现类(用于提供服务)
  7. * @author chenqi
  8. * @date 2018年10月4日
  9. *
  10. */
  11. public class DemoServiceImpl implements DemoService {
  12. /**
  13. * 实现 DemoService 中的 helloDubbo 接口
  14. */
  15. public String helloDubbo(String str) {
  16. return "hello " + str;
  17. }
  18. }

服务启动类

  1. package com.dubbo.provider.demo;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import java.io.IOException;
  4. /**
  5. *
  6. * @ClassName: Provider
  7. * @Description: 服务启动类
  8. * @author chenqi
  9. * @date 2018年10月4日
  10. *
  11. */
  12. public class ProviderApplication {
  13. public static void main(String[] args) throws IOException {
  14. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
  15. System.out.println(context.getDisplayName() + ": here");
  16. context.start();
  17. System.out.println("服务已经启动...");
  18. System.in.read();
  19. }
  20. }

3、服务消费者dubbo-consumer

pom依赖

  1. <?xml version="1.0"?>
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>dubbo</groupId>
  7. <artifactId>dubbo</artifactId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <artifactId>dubbo-consumer</artifactId>
  11. <name>dubbo-consumer</name>
  12. <description>服务消费者</description>
  13. <url>http://maven.apache.org</url>
  14. <properties>
  15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16. </properties>
  17. <dependencies>
  18. <!-- 引入api模块 -->
  19. <dependency>
  20. <groupId>dubbo</groupId>
  21. <artifactId>dubbo-api</artifactId>
  22. <version>${project.version}</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>junit</groupId>
  26. <artifactId>junit</artifactId>
  27. <version>3.8.1</version>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. </project>

配置文件consumer.xml
通过Spring配置引用远程服务:

  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"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  7. <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
  8. <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
  9. <dubbo:registry address="zookeeper://localhost:2181"/>
  10. <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
  11. <dubbo:reference id="permissionService" interface="com.dubbo.api.demo.DemoService"/>
  12. </beans>

测试服务消费者获取服务接口

  1. package com.dubbo.consumer.demo;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. import com.dubbo.api.demo.DemoService;
  4. /**
  5. *
  6. * @ClassName: ConsumerTest
  7. * @Description: 测试服务消费者获取服务接口
  8. * @author chenqi
  9. * @date 2018年10月7日
  10. *
  11. */
  12. public class ConsumerTest {
  13. public static void main(String[] args) {
  14. //测试常规服务
  15. ClassPathXmlApplicationContext context =
  16. new ClassPathXmlApplicationContext("consumer.xml");
  17. context.start();
  18. System.out.println("consumer start");
  19. DemoService demoService = context.getBean(DemoService.class);
  20. System.out.println("consumer");
  21. System.out.println(demoService.helloDubbo("dubbo"));
  22. }
  23. }

OK,下面我们依次启动服务提供者和服务消费者,注意确保provider已被运行后再启动consumer
启动服务提供者:
在这里插入图片描述
启动报了点错误,暂时忽略。

运行消费者进行测试:
在这里插入图片描述

可以看到,消费者成功调用提供者所提供的远程服务。当然,这只是一个模拟的项目,实际中有多提供者多消费者情况,比这要复杂的多,当然只有这样才能体现dubbo的特性。

Dubbo管理控制台介绍

管理控制台功能

路由规则,动态配置,服务降级
访问控制,权重调整
负载均衡
在这里插入图片描述

网上自行下载dubbo-admin,下载后是一个war包,直接放到tomcat安装目录的webapps目录下,然后启动tomcat即可
在这里插入图片描述

在这里插入图片描述

浏览器访问:http://localhost:28080/dubbo-admin/
输入用户名密码 root

在这里插入图片描述

查看服务提供者

在这里插入图片描述

查看服务消费者
在这里插入图片描述

通过上面的内容,相信大家应该对dubbo有了初步的认知和了解。

ps:如果该文章有帮助到您,就点个赞吧!您的支持与肯定是我持续更新最大的动力。

发表评论

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

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

相关阅读

    相关 Dubbo入门

    Dubbo开始于电商项目,是用Java语言写的, 首先介绍一下电商项目的古老历史: 从刚开始的ORM框架,全称是Object Relational Mapping中文那,全

    相关 Dubbo入门

    Dubbo入门 1. 服务容器负责启动,加载,运行服务提供者,这个图上没标识出来,服务端启动就是0. 2. 服务提供者在启动时,向注册中心注册自己提供的服务。 3. 服

    相关 dubbo入门教程-从零搭建dubbo服务

    【原创 转载请注明出处】 本文是学习了dubbo之后自己手动写的,比较通俗,很多都是自己学习之后的理解,写的过程中没有参考任何文章。 另外dubbo也有官方文档,但是比较官