🍓 简介:java系列技术分享(👉持续更新中…🔥)
🍓 初衷:一起学习、一起进步、坚持不懈
🍓 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正🙏
🍓 希望这篇文章对你有所帮助,欢迎点赞 👍 收藏 ⭐留言 📝
文章目录
一、Dubbo简介
Dubbo官网
:https://cn.dubbo.apache.org
- Dubbo是阿里巴巴推出的一个高性能、可扩展、可靠的
RPC框架
- 基于Dubbo进行远程调用
- Dubboo基于TCP(传输层)协议,效率更高,可以替换Feign,提升高并发压力
二、Dubbo入门
2.1 Dubbo的基础架构
2.2 搭建Nacos 和 DubboAdmin
Nacos安装查看
:Docker如何安装Nacos
dubbo-admin安装
- DubboAdmin是阿里巴巴管理提供的管理控制台,可以实现服务查询,详情展示,服务测试等功能,可以更好的帮助开发人员对服务进行管理和监控
#1、下载代码: git clone https://github.com/apache/dubbo-admin.git #2、在 dubbo-admin-server/src/main/resources/application.properties中指定注册中心地址 #3、构建 mvn clean package -D maven.test.skip=true #4、启动 mvn --projects dubbo-admin-server spring-boot:run #或者 cd dubbo-admin-distribution/target; java -jar dubbo-admin-0.1.jar #5、访问 http://localhost:8080
2.3 服务提供者
-
引入依赖
<!--dubbo的起步依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>2.7.8</version> </dependency>
-
需要远程调用的接口添加
@DubboService
注解 -
添加配置
dubbo: protocol: name: dubbo #协议 port: 20881 #端口从20880开始 registry: address: nacos://127.0.0.1:8848 scan: base-packages: com.use.service #@DubboService注解所在包
2.4 服务消费者
因为在DubboAdmin使用的接口全类名,所以调用接口服务提供者和消费者包名必须一致
引入依赖
<!--dubbo的起步依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.8</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>2.7.8</version> </dependency>
- 需要远程调用注入的Bean添加
@DubboReference
注解 - 添加配置
dubbo: registry: address: nacos://127.0.0.1:8848
2.5 抽取公共接口
服务提供者和消费者调用接口包名必须一致,为了保证双方接口一致,则抽取公共接口模块
在提供者和消费者中引入公共接口模块依赖,实现复用
2.6 对象序列化
-
如果要传输对象, 对象必须实现序列化接口
implements Serializable
-
因为:RPC通信,采用二进制数据格式,所以需要进行序列化处理。
三、高级特性
3.1 Dubbo启动检查
如果先启动服务消费者,@DubboReference
引入远程服务时,因为提供者还没有启动,所以找不到提供者,则会抛出异常
在消费者
添加如下配置即可:
dubbo:
registry:
address: nacos://127.0.0.1:8848
consumer:
check: false #关闭启动检查
3.2 多版本
- 暴露服务时,指定服务版本
@DubboService(version = "2.0.0")
- 消费者引入服务时,指定引用的服务版本
@DubboReference(version = "2.0.0")
3.3 超时与重试
- Dubbo默认超时时间:
1000毫秒
- 调用远程服务时,如果发生网络异常或者其他异常,Dubbo会进行重试,重试次数
默认是3次
,可以通过配置进行修改。 - 重试机制会发送多次请求,可能会带来问题,例如保存会触发多次,
- 第一种(只针对该注解引入的服务)
@DubboReference(retries = 0)
- 第二种(全局生效)
dubbo: registry: address: nacos://127.0.0.1:8848 consumer: check: false #关闭启动检查 retries: 0 # 重试次数为0
3.4 负载均衡
- Random(随机):将请求
按权重随机
分配给不同的服务节点。 - Round Robin(轮询):将请求
按权重轮训
到不同的服务节点上。 - Least Active:将请求按
最少活跃调用数,相同的活跃数随机
。 - ConsistentHash:
一致性hash,相同参数的请求总是发到同一提供者。
在服务消费者端:
@DubboReference(loadbalance= "random")
四、SpringCloud整合Dubbo`
SpringCloud整合Dubbo只需要在nacos中查看即可,不需要在DubboAdmin中查看
4.1 服务提供者
- 引入依赖
<!--nacos注册中心的依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--springcloud alibaba dubbo依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency>
- 需要远程调用的接口添加
@DubboService
注解 - 添加配置
注意区别
spring: application: name: consumer-service cloud: nacos: discovery: server-addr: localhost:8848 #配置dubbo,注册中心,暴露的端口和协议,dubbo注解的包扫描 dubbo: protocol: name: dubbo port: 20881 registry: address: spring-cloud://localhost #使用SpringCloud中的注册中心 scan: base-packages: com.use.service #dubbo中包扫描
4.2 服务消费者
引入依赖
<!--nacos注册中心的依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--springcloud alibaba dubbo依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency>
- 需要远程调用注入的Bean添加
@DubboReference
注解 - 添加配置
spring: application: name: provider-service cloud: nacos: discovery: server-addr: localhost:8848 #配置dubbo,注册中心,暴露的端口和协议,dubbo注解的包扫描 dubbo: registry: address: spring-cloud://localhost #使用SpringCloud中的注册中心 consumer: check: false #关闭启动检查 retries: 0 # 重试设置为0