背景
您已应用微服务架构模式。服务必须处理来自应用程序客户端的请求。此外,服务有时必须协作处理这些请求。他们必须使用进程间通信协议。
解决方案
使用RPI进行服务间通信。客户端使用基于请求/回复的协议向服务发出请求。
例子
RPI技术有很多例子
- REST
- GRPC
- Apache Thrift
来自Microservices示例应用程序的RegistrationServiceProxy是一个用Scala编写的组件示例,它使用Spring Framework的RestTemplate发出REST请求:
@Component
class RegistrationServiceProxy @Autowired()(restTemplate: RestTemplate) extends RegistrationService {
@Value("${user_registration_url}")
var userRegistrationUrl: String = _
@HystrixCommand(commandProperties=Array(new HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="800")))
override def registerUser(emailAddress: String, password: String): Either[RegistrationError, String] = {
try {
val response = restTemplate.postForEntity(userRegistrationUrl,
RegistrationBackendRequest(emailAddress, password),
classOf[RegistrationBackendResponse])
response.getStatusCode match {
case HttpStatus.OK =>
Right(response.getBody.id)
}
} catch {
case e: HttpClientErrorException if e.getStatusCode == HttpStatus.CONFLICT =>
Left(DuplicateRegistrationError)
}
}
}
user_registration_url的值使用Externalized配置提供。
结果
这种模式具有以下好处:
- 简单而熟悉
- 请求/回复很简单
- 更简单的系统,因为没有中间代理
这种模式有以下缺点:
- 通常只支持请求/回复,而不支持其他交互模式,如通知,请求/异步响应,发布/订阅,发布/异步响应
- 由于客户端和服务必须在交互期间可用,因此可用性降低
此模式存在以下问题:
- 客户端需要发现服务实例的位置
相关模式
- 特定于域的协议是一种替代模式
- 消息传递是一种替代模式
- 外部化配置提供(逻辑)网络位置,例如,服务的URL。
- 客户端必须使用客户端发现和服务器端发现来查找服务实例
- 客户端通常会使用断路器模式来提高可靠性