通信:远程过程调用(RPI)

2018-08-28  本文已影响0人  scheshan

背景

你已经使用了微服务架构。服务需要处理来自应用客户端的请求。将来,服务有时必须协作起来处理这些请求。他们必须采用一种内部通信协议。

限制

解决方案

使用RPI来进行内部服务通信。客户端采用以请求 / 回复为基础的协议请求其他服务。

示例

有一些RPI技术的例子:

微服务示例程序里的RegistrationServiceProxy是用Scala语言写的一个例子,它用Spring框架的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的值通过外部配置提供

结果

这个模式有以下优势:

这个模式有以下弊端:

这个模式有以下问题:

相关模式

上一篇 下一篇

猜你喜欢

热点阅读