1. dubbo简介
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源 Java RPC 分布式服务框架,它提供了三大核心能力:
- 面向接口的远程方法调用
- 智能容错和负载均衡
- 服务的自动注册和发现
2. 整体设计
图例说明:
- 图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口,位于中轴线上的为双方都用到的接口。
- 图中从下至上分为十层,各层均为单向依赖,右边的黑色箭头代表层之间的依赖关系,每一层都可以剥离上层被复用,其中,Service 和 Config 层为 API,其它各层均为 SPI。
- 图中绿色小块的为扩展接口,蓝色小块为实现类,图中只显示用于关联各层的实现类。
- 图中蓝色虚线为初始化过程,即启动时组装链,红色实线为方法调用过程,即运行时调时链,紫色三角箭头为继承,可以把子类看作父类的同一个节点,线上的文字为调用的方法。
3. 各层说明
- config 配置层:对外配置接口,以 ServiceConfig, ReferenceConfig 为中心,可以直接初始化配置类,也可以通过 spring 解析配置生成配置类
- proxy 服务代理层:服务接口透明代理,生成服务的客户端 Stub 和服务器端 Skeleton, 以 ServiceProxy为中心,扩展接口为 ProxyFactory
- registry 注册中心层:封装服务地址的注册与发现,以服务 URL 为中心,扩展接口为 RegistryFactory, Registry, RegistryService
- cluster 路由层:封装多个提供者的路由及负载均衡,并桥接注册中心,以 Invoker 为中心,扩展接口为 Cluster, Directory, Router, LoadBalance
- monitor 监控层:RPC 调用次数和调用时间监控,以 Statistics 为中心,扩展接口为 MonitorFactory, Monitor, MonitorService
- protocol 远程调用层:封装 RPC 调用,以 Invocation, Result 为中心,扩展接口为 Protocol, Invoker, Exporter
- exchange 信息交换层:封装请求响应模式,同步转异步,以 Request, Response 为中心,扩展接口为 Exchanger, ExchangeChannel, ExchangeClient, ExchangeServer
- transport 网络传输层:抽象 mina 和 netty 为统一接口,以 Message 为中心,扩展接口为 Channel, Transporter, Client, Server, Codec
- serialize 数据序列化层:可复用的一些工具,扩展接口为 Serialization, ObjectInput, ObjectOutput, ThreadPool
4. 关系说明
- 在 RPC 中,Protocol 是核心层,也就是只要有 Protocol + Invoker + Exporter 就可以完成非透明的 RPC 调用,然后在 Invoker 的主过程上 Filter 拦截点。
- 图中的 Consumer 和 Provider 是抽象概念,只是想让看图者更直观的了解哪些类分属于客户端与服务器端,不用 Client 和 Server 的原因是 Dubbo 在很多场景下都使用 Provider, Consumer, Registry, Monitor 划分逻辑拓普节点,保持统一概念。
- 而 Cluster 是外围概念,所以 Cluster 的目的是将多个 Invoker 伪装成一个 Invoker,这样其它人只要关注 Protocol 层 Invoker 即可,加上 Cluster 或者去掉 Cluster 对其它层都不会造成影响,因为只有一个提供者时,是不需要 Cluster 的。
- Proxy 层封装了所有接口的透明化代理,而在其它层都以 Invoker 为中心,只有到了暴露给用户使用时,才用 Proxy 将 Invoker 转成接口,或将接口实现转成 Invoker,也就是去掉 Proxy 层 RPC 是可以 Run 的,只是不那么透明,不那么看起来像调本地服务一样调远程服务。
- 而 Remoting 实现是 Dubbo 协议的实现,如果你选择 RMI 协议,整个 Remoting 都不会用上,Remoting 内部再划为 Transport 传输层和 Exchange 信息交换层,Transport 层只负责单向消息传输,是对 Mina, Netty, Grizzly 的抽象,它也可以扩展 UDP 传输,而 Exchange 层是在传输层之上封装了 Request-Response 语义。
- Registry 和 Monitor 实际上不算一层,而是一个独立的节点,只是为了全局概览,用层的方式画在一起。
5. 模块分包
模块说明:
- dubbo-common 公共逻辑模块:包括 Util 类和通用模型。
- dubbo-remoting 远程通讯模块:相当于 Dubbo 协议的实现,如果 RPC 用 RMI协议则不需要使用此包。
- dubbo-rpc 远程调用模块:抽象各种协议,以及动态代理,只包含一对一的调用,不关心集群的管理。
- dubbo-cluster 集群模块:将多个服务提供方伪装为一个提供方,包括:负载均衡, 容错,路由等,集群的地址列表可以是静态配置的,也可以是由注册中心下发。
- dubbo-registry 注册中心模块:基于注册中心下发地址的集群方式,以及对各种注册中心的抽象。
- dubbo-monitor 监控模块:统计服务调用次数,调用时间的,调用链跟踪的服务。
- dubbo-config 配置模块:是 Dubbo 对外的 API,用户通过 Config 使用Dubbo,隐藏 Dubbo 所有细节。
- dubbo-container 容器模块:是一个 Standlone 的容器,以简单的 Main 加载 Spring 启动,因为服务通常不需要 Tomcat/JBoss 等 Web 容器的特性,没必要用 Web 容器去加载服务。
整体上按照分层结构进行分包,与分层的不同点在于:
- container 为服务容器,用于部署运行服务,没有在层中画出。
- protocol 层和 proxy 层都放在 rpc 模块中,这两层是 rpc 的核心,在不需要集群也就是只有一个提供者时,可以只使用这两层完成 rpc 调用。
- transport 层和 exchange 层都放在 remoting 模块中,为 rpc 调用的通讯基础。
- serialize 层放在 common 模块中,以便更大程度复用。
依赖关系
图例说明:
- 图中小方块 Protocol, Cluster, Proxy, Service, Container, Registry, Monitor 代表层或模块,蓝色的表示与业务有交互,绿色的表示只对 Dubbo 内部交互。
- 图中背景方块 Consumer, Provider, Registry, Monitor 代表部署逻辑拓扑节点。
- 图中蓝色虚线为初始化时调用,红色虚线为运行时异步调用,红色实线为运行时同步调用。
- 图中只包含 RPC 的层,不包含 Remoting 的层,Remoting 整体都隐含在 Protocol 中。
6.实战示例
这里会举一个商品服务的示例。
6.1 创建服务接口应用
6.1.1 使用SpringBoot构建一个maven jar 项目,命名:dubbo-service-product-api,该应用只定义api接口,供服务提供方与消费方使用。
6.1.2 pom.xml如下:
<?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.stwen</groupId>
<artifactId>dubbo-service-product-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service-product-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
6.1.3 创建一个商品服务测试接口
package com.stwen.product.service.api;
/**
* @description: 商品服务接口
* @author: ganxianhao
* @date: 2019/07/29
**/
public interface ProductService {
String helloDubbo();
}
6.2 创建服务提供者应用
6.2.1 使用SpringBoot构建一个应用,命名:dubbo-service-product-provider,依赖上面的服务接口模块。
6.2.2 pom.xml依赖如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stwen</groupId>
<artifactId>dubbo-service-product-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service-product-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 这是dubbo推到apache后孵化版本 -->
<!--<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 依赖接口jar-->
<dependency>
<groupId>com.stwen</groupId>
<artifactId>dubbo-service-product-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主要增加了以下依赖:
- com.alibaba.boot:dubbo-spring-boot-starter:0.2.1:Dubbo Starter,0.2.1 版本支持 Spring Boot 2.x,是一个长期维护的版本。注:0.1.0 版本已经不推荐使用了,是个短期维护的版本,如果你还在用旧版,请大家尽快升级。
- com.alibaba.boot:dubbo-spring-boot-actuator:0.2.1:Dubbo 的服务状态检查
- com.stwen:dubbo-service-product-api:1.0.0-SNAPSHOT:刚才创建的接口应用,如果无法依赖别忘记先 mvn clean install 到本地仓库。
6.2.3 applicaiton.yml配置,zookeeper 注册中心需要本地启动一个,地址默认为 : 127.0.0.1:2181
# product服务版本
product:
service:
version: 0.0.1
# Dubbo Config properties
dubbo:
## Base packages to scan Dubbo Component:@com.alibaba.dubbo.config.annotation.Service
scan:
basePackages: com.stwen.product.service
## ApplicationConfig Bean
application:
id: dubbo-service-product-provider
name: dubbo-service-product-provider
qos-port: 20001
qos-enable: true
## ProtocolConfig Bean
protocol:
id: dubbo
name: dubbo
port: 12345
status: server
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://127.0.0.1:2181
# Enables Dubbo All Endpoints
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
## StatusChecker Name extras (default : empty )
extras: load,threadpool
6.2.4 应用启动类,增加启动Provider 容器
package com.stwen.product.service;
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboServiceProductProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServiceProductProviderApplication.class, args);
// 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
Main.main(args);
}
}
6.2.5 通过dubbo提供的 @Service 注解实现上面创建的接口方法:
package com.stwen.product.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.stwen.product.service.api.ProductService;
/**
* @description: 商品服务实现
* @author: ganxianhao
* @date: 2019/07/29
**/
@Service(version = "${product.service.version}")
public class ProductServiceImpl implements ProductService {
@Override
public String helloDubbo() {
return "hello,spring-boot dubbo";
}
}
6.2.6 启动,可以看到如下(注:需要先启动zk服务):
6.3 创建服务消费者应用
6.3.1 使用SpringBoot构建一个应用,命名:dubbo-service-product-consumer,同样,需要依赖上面的服务接口模块。
6.3.2 pom.xml 依赖如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.stwen</groupId>
<artifactId>dubbo-service-product-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service-product-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 依赖接口jar-->
<dependency>
<groupId>com.stwen</groupId>
<artifactId>dubbo-service-product-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.3.3 application.yml配置如下:
# Spring boot application
spring:
application:
name: hello-dubbo-service-user-consumer
server:
port: 8888
# product服务版本
product:
service:
version: 0.0.1
# Dubbo Config properties
dubbo:
scan:
basePackages: com.stwen.product.consumer.controller
## ApplicationConfig Bean
application:
id: dubbo-service-product-consumer
name: dubbo-service-product-consumer
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://127.0.0.1:2181
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
management:
server:
port: 9091
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
endpoints:
web:
exposure:
include: "*"
6.3.4 通过dubbo提供的 @Reference 注入 ProductService接口
package com.stwen.product.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.stwen.product.service.api.ProductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description: 商品控制器
* @author: ganxianhao
* @date: 2019/07/29
**/
@RestController
public class ProductController {
@Reference(version = "${product.service.version}")
private ProductService productService;
@RequestMapping(value = "test")
public String test() {
return productService.helloDubbo();
}
}
6.3.5 启动消费端
6.4 dubbo-admin管理控制台
新版本采用前后端分离
下载地址如下:
GitHub:https://github.com/apache/incubator-dubbo-ops
下载后,需要执行如下:mvn clean package 打包编译,首次较慢
# 打包
mvn clean package
# 运行
mvn --projects dubbo-admin-backend spring-boot:run
# 浏览
http://localhost:8080
编译好后,运行:mvn --projects dubbo-admin-backend spring-boot:run
然后浏览器访问:http://localhost:8080 ,可以看到刚才的服务提供者已经注册上了zk:
点击详情,可以具体查看服务提供者与消费者
消费者:
浏览器访问刚才的消费端控制器方法:http://localhost:8888/test,便可以看到显示:
hello,spring-boot dubbo
看完后是不是觉得很简单,觉得有用点个赞再走吧0.0,整理不易。需要源码的,请在下方评论,看到会发你哦。
本文来自:CSDN 作者:stwen_gan https://blog.csdn.net/a1036645146/article/details/97651337
转载请注明出处,谢谢。
参考资料: