目录
- 集成
- 架构
- 架构分析
- Spring boot 集成
- 引入依赖
- 提供API 调用桥梁
- 添加Dubbo服务
- 服务提供者-服务实现
- 服务提供者-添加配置
- 服务消费者-添加配置
- 服务消费者-配置消费端请求任务
- 服务调用
- 扩展
- 为什么要新增Dubbo协议
集成
架构
架构分析
Dubbo作为一个RPC调用框架作用就是让服务具有RPC调用的能力
RPC就是远程调用就像本地调用一个Bean的方法那样
调用者不用去写提供者的路径等
zk在这里做注册中心用的
提供者要注册到注册中心,注册:host、port、协议等
消费者读取配置中心配置缓存后,发送RPC请求调用提供者
Spring boot 集成
引入依赖
<!-- 定义了 dubbo 和 zookeeper(以及对应的连接器 curator)的依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
提供API 调用桥梁
消费者和提供者都要使用的
package org.apache.dubbo.springboot.demo;
public interface DemoService {
String sayHello(String name);
}
添加Dubbo服务
需要在消费者和提供者的Application服务类上添加org.apache.dubbo.config.spring.context.annotation.EnableDubbo 注解
服务提供者-服务实现
package org.apache.dubbo.springboot.demo.provider;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.springboot.demo.DemoService;
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
服务提供者-添加配置
- 提供一个名称,方便注册中心区分
- 提供自身对外提供服务的协议
- 提供注册中心的地址
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
服务消费者-添加配置
dubbo:
application:
name: dubbo-springboot-demo-consumer
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
服务消费者-配置消费端请求任务
在Bean中引入依赖
@DubboReference
private DemoService demoService;
//直接调用即可
demoService.sayHello("world")
服务调用
如果需要立即调用的话
启动的时候先启动提供者
后启动消费者
扩展
为什么要新增Dubbo协议
Http 1.0 和1.1 版本会额外带有很多没有用的参数
并且只能一次获取到请求的响应后才能继续下一次的请求
没有参数对应请求和相应
dubbo协议就支撑请求和相应分开(请求中会带有请求id,在相应的时候会根据id关联),且所有传输字段都是自己需要的
但是很显然,dubbo只能本框架使用,所以后续dubbo提供使用了http2为基础的新协议,方便和其他框架集成