- Spring Cloud 是 RPC 吗
- Spring Cloud 还需要 Dubbo 吗
- 关键代码
- 项目结构
- 生产者
- 消费者
RPC定义:RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
RPC解决问题:让分布式或者微服务系统中不同服务之间的调用像本地调用一样简单。
RPC中重要协议:传输协议、序列化(反序列化)协议。
Spring Cloud 中 通过 Feign 使用HTTP协议实现远程过程调用,不需要了解底层网路协议。通过HttpMessageConverter进行JSON序列化和反序列化。满足RPC的定义,属于RPC调用过程。
Spring Cloud 还需要 Dubbo 吗在Spring Cloud构建的微服务系统中,大多数的开发者使用都是官方提供的Feign组件来进行内部服务通信,这种声明式的HTTP客户端使用起来非常的简洁、方便、优雅,并且和开发平台、语言无关,但是通常情况下,HTTP并不会开启KeepAlive功能,即当前连接为短连接,短连接的缺点是每次请求都需要建立TCP连接,这使得其效率变的相当低下。
对外部提供REST API服务是一件非常好的事情,但是如果内部调用也是使用HTTP调用方式,就会显得显得性能低下,Spring Cloud默认使用的Feign组件进行内部服务调用就是使用的HTTP协议进行调用,这时,我们如果内部服务使用RPC调用,对外使用REST API,将会是一个非常不错的选择。
关键代码 项目结构# 消费者 wfbi-dubbo-base-consumer # 生产者 wfbi-dubbo-base-provider # 生产者接口模块 ++dubbo-base-provider-api # 生产者业务模块 ++dubbo-base-provider-business生产者
pom关键配置
wfbi-dubbo-base-provider
1.8 Hoxton.SR12 2.2.13.RELEASE 2.2.5.RELEASEorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoveryorg.projectlombok lombokprovided
dubbo-base-provider-api 接口模块不需要依赖
dubbo-base-provider-business
org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest com.wfbi dubbo-base-provider-api0.0.1-SNAPSHOT com.alibaba.cloud spring-cloud-starter-dubbo
配置文件
dubbo-base-provider-business > application.yml
spring:
application:
name: dubbo-base-provider
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
server-addr: xxx.xxx.xxx.xxx:8848
dubbo:
scan:
base-packages: com.wfbi.dubbo.base.provider.business.service
protocol:
name: dubbo
# -1 代表 dubbo 自动扫描并使用可用端口(从20880开始递增)
port: -1
registry:
# 挂载到 Spring Cloud 注册中心, 也可通过nacos://ip:port指定挂载的注册中心
address: spring-cloud://localhost
# 只注册不订阅服务
subscribe: false
业务代码
dubbo-base-provider-api > EchoService
public interface EchoService {
String sayHello(String words);
}
dubbo-base-provider-business > EchoServiceImpl
@DubboService(version = "1.0")
public class EchoServiceImpl implements EchoService {
public String sayHello(String words) {
return "Echo Hello, " + words;
}
}
消费者
pom 关键配置
wfbi-dubbo-base-consumer
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discoverycom.alibaba.cloud spring-cloud-starter-dubbocom.wfbi dubbo-base-provider-api0.0.1-SNAPSHOT org.projectlombok lombokprovided
配置文件
wfbi-dubbo-base-consumer > application.yml
spring:
application:
name: dubbo-base-consumer
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
server-addr: xxx.xxx.xxx.xxx:8848
dubbo:
protocol:
name: dubbo
port: -1
scan:
base-packages: com.funtl.apache.dubbo.consumer.controller
registry:
# 挂载到 Spring Cloud 注册中心, 也可通过nacos://ip:port指定挂载的注册中心
address: spring-cloud://localhost
cloud:
# 默认* 即订阅所有服务 占用内存及cpu。强烈推荐配置需要的服务,多个用,分隔
subscribed-services: dubbo-base-provider
业务代码
wfbi-dubbo-base-consumer > EchoController
@RestController
public class EchoController {
@DubboReference(version = "1.0")
private EchoService echoService;
@GetMapping(value = "/echo/{words}")
public String echo(@PathVariable String words) {
return echoService.sayHello(words);
}
}



