본문으로 바로가기

gRPC in the browser

category 카테고리 없음 2022. 9. 1. 17:25
728x90

gRPC는 Protocol Buffer를 이용하여 RESTful HTTP API에 비하여 적은 데이터로 빠른 통신을 할 수 있습니다.

하지만 브라우저-서버 간의 gRPC 통신이 지원되지 않는 단점이 존재하는데요.

 

The gRPC-Gateway

The gRPC-Gateway project started out (as I hear it) after the original author, Yuki Yugui Sonoda, left Google and wanted to use a tool they had been using internally at Google, but couldn’t find an open source version. So they wrote one! At its core the gRPC-Gateway is a translation layer between the HTTP/JSON REStful API paradigm and the gRPC/Protobuf API paradigm. So what does that mean? Lets start by talking about HTTP/JSON.

gRPC-Web

The gRPC-Web project is the official answer from the gRPC project to the question of using gRPC in the browser. Like the gRPC-Gateway, it requires you to run a translation layer between your gRPC service and the browser.

 

gRPC Gateway? Web?

  • Do you want to expose a RESTful HTTP/JSON API?
    • Use the gRPC-Gateway
  • Do you want to use gRPC with a browser client?
    • Use gRPC-Web

라지만 gRPC-Web은 gRPC proxy 서버를 따로 둬야해서 gRPC Gateway를 사용했다가 최근에 leader분께서 

connect-go

라는걸 소개해주셨고 바로 설치까지 해주셨다.

우선 server 입장에선 기존 http와 grpc를 둘다 제공하고, 기존 http 방식과 동일하게 구현할 수 있어서 (gRPC gateway는 상대적으로 이질감이 들었다, 하지만 큰 차이는 아닌거 같다) 보기에 깔끔했다.

client 입장에선 gRPC gateway를 통했을 때는 axios server URL을 통해 통신했었는데, 만약 parameter 이름을 잘못 넣거나 주소를 잘못 호출 했을 경우 문제가 될 수 있는걸 connect-go에서는 IDL에 정의된 함수를 바로 호출하게 하여 실수를 줄일 수 있었다.

 

양쪽 측면에서 편하게 사용할 수 있어서 beta 임에도 좋은 느낌이었다.

 

https://jbrandhorst.com/post/grpc-in-the-browser/