1. dubbo介绍
dubbo是一款开源rpc框架,提供rpc调用诸多组件、支持服务注册与发现、服务负载均衡、服务容错、服务降级处理、服务失败尝试机制、服务监控等组件。
当我们项目拆分成微服务时,A服务需要调用B服务的时候,通常使用http请求,但是http请求过于臃肿,并且无法更好实现对服务的管理,因此通常会引入rpc框架。
2. 服务端springboot引入dubbo框架
- 引入dubbo-start pom包
<!-- Spring Boot Dubbo 依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.10</version>
</dependency>
- 定义服务端配置
spring:
application:
name: dubbo-dispatcher
# dubbo 相关配置
dubbo:
application:
id: dubbo-dispatcher
registry:
address: N/A
protocol:
name: dubbo
port: 20890
host: 192.168.3.146
scan:
base-packages: com.nt.transfer.provider
其中 registry.address = N/A 时,注册中心没有设置,表示该配置为直连模式
base-package : xxx,定义提供的服务的接口包。
这样在springboot启动后,会去对应的base-package去扫描对应的注解引入的服务,并且将服务进行管理。
- 定义服务
@Service
@DubboService(version = "1.0.0")
接口实现类
- 启动应用类
@SpringBootApplication
@EnableDubbo
public class TransferApplication {
3. 消费者配置
消费者配置如上提供者配置相同,消费者消费服务如下所示:
@DubboReference()
private A a;