服務通訊之遠端過程呼叫(RPI)
阿新 • • 發佈:2018-12-19
背景
您已應用微服務架構模式。服務必須處理來自應用程式客戶端的請求。此外,服務有時必須協作處理這些請求。他們必須使用程序間通訊協議。
解決方案
使用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。
- 客戶端必須使用客戶端發現和伺服器端發現來查詢服務例項
- 客戶端通常會使用斷路器模式來提高可靠性