설치를 완료했으니 예제를 통해 어느정도 기능에 대해 눈으로 익혀본다.

 

예제의 경우 gRPC 코드의 example폴더에 위치한다.

android, cpp, csharp, node, objective-c, php, protos, python, ruby

 

cpp 예제 빌드해보고 동작 확인해보기

  1. compression
  2. helloworld
  3. keyvaluestore - 빌드파일이 없음.
  4. load_balancing
  5. metadata
  6. route_guide

 

compression

빌드 make

실행파일 : greeter_server, greeter_client

proto 파일 : helloworld.proto

 

빌드

mkdir build 

cd build

cmake ..

make -j4

 

동작

Server : Server listening on 0.0.0.0:50051 

(서버실행시)Client : Greeter received: Hello world world world world

(서버실행X) Client: 14: failed to connect to all addresses
Greeter received: RPC failed

 

proto 파일 generate

protoc --cpp_out=cppout/ helloworld.proto

Generator 된 파일

helloworld.pb.cc helloworld.pb.h

 

 

실행파일

greeter_async_client  greeter_async_client2  greeter_async_server  greeter_client  greeter_server

 

metadata

 

route_guide

 

실제로 적용하고 운영할 떄 ProtoBuf, HTTP2 와 같은 이해부분이 필요함.

 

참고

gRPC-web 브라우저 적용 삽질기 : https://velog.io/@kyusung/grpc-web-example#grpc-web-%EB%B8%8C%EB%9D%BC%EC%9A%B0%EC%A0%80-%EC%A0%81%EC%9A%A9-%EC%82%BD%EC%A7%88%EA%B8%B0

Microservices with gRPC

 

 

RPC에서 REST까지 간단한 개념소개: https://www.slideshare.net/WonchangSong1/rpc-restsimpleintro

 

'gRPC' 카테고리의 다른 글

[gRPC]01. 정리 및 설치  (0) 2020.07.16

공식사이트 : https://grpc.io/

 

gRPC

A high-performance, open source universal RPC framework

grpc.io

작성일자 : 2020년 07월 16일 

 

목적 : 새로운 라이브러리에 대해 학습

https://at.projects.genivi.org/wiki/display/DIRO/Poll+-+Your+favorite+protocols

 

Poll - Your favorite protocols - Domain Interaction Strategy Roll Out - Confluence

Introduction During the past months, the Generic Protocol Evaluation Project team (project short name: GPRO) collected and discussed the most popular technologies for communication used today. The project studied not only network protocols but also related

at.projects.genivi.org

 

WAHT IS GRP? 항목 Introduction, Core concepts 를 정리한다.

gRPC 란?

 구글에서 만든 RPC

 gRPC 와 ptorocol buffers 에 대해 소개한다.

RPC 의 개념과 정리는 잘정리된 블로그를 링크함 : https://blog.naver.com/phh0606c/221718739856

 

개요

 원격으로 호출 할 수 있는 방법을 지정할 수 있다.

gRPC 개요

다양한 언어를 지원한다.

 

Protocol Buffers 와의 동작

 데이터를 직렬화하기위한 Protocol Buffers 를 사용한다. 프로토 파일에서 직렬화 할 데이터의 구조를 정의해야한다. 확장자는 .proto 인 일반 텍스트 파일이다.

프로토콜 버퍼 데이터는 메시지로 구성되며 각 메시지는 필드라는 일련의 이름-값 쌍을 포함하는 정보의 작은 논리적 레코드이다.

 

message Person { string name = 1; int32 id = 2; bool has_ponycopter = 3; }

 

생성하면 요론놈이 생성되는것 같다.

 

 

사용가이드는 proto3 : https://developers.google.com/protocol-buffers/docs/proto3

 

Protocol buffer version

proto3 을 지향함 

 

번역기를 돌려서 약간 이상할수 있습니다.

Core concepts

개요 

 Service definition

 gRPC 는 서비스 인터페이스와 페이로드 메시지의 구조를 모두 설명하기위해  protocol buffers (IDL)를 사용한다.

원하는 경우 다른 대안을 사용할 수 있다.

service HelloService {
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string greeting = 1;
}

message HelloResponse {
  string reply = 1;
}

gRPC 를 사용하면 4가지 서비스 방법을 정의 할 수 있다.

기본함수가 들어가냐  stream 내용이 들어가냐에따라 구분하여 4가지 상태가 되어 보인다.

 

  • 클라이언트가 단일 함수를 서버에 보내고 단일 함수호출과 같은 단일 응답받는 , 일반적인 함수 콜
rpc SayHello(HelloRequest) returns (HelloResponse);
  • 클라이언트가 서버에 요청을 보내고 스트림을 가져와서 일련의 메시지를 다시 읽는 서버 스트리밍 RPC. 클라이언트는 메시지가 없을 때까지 리턴된 스트림을 가진다. gRPC 는 개별 RPC 호출 내에서 메시지 순서를 보장한다.
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
  • 클라이언트 스트리밍 RPC 는 클라이언트가 일련의 메시지를 작성하고 제공된 스트림을 사용하여 다시 서버로 보낸다. 클라이언트가 메시지 쓰기를 마치면 서버가 메시지를 읽고 응답을 반환 할 때까지 기다린다. 다시 gRPC 는 개별 RPC 호출 내에서 메시지 순서를 보장한다.
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
  • 양방향 스트리밍 RPC 는 양쪽에서 읽기/ 쓰기 스트림을 사용하여 일련의 메시지를 보낸다. 두 스트림은 독립적으로 작동하므로 클라이언트와 서버는 원하는 순서대로 읽고 쓸수 있다. 예를 들어, 서버는 응답을 쓰기 전에 모든 클라이언트 메시지를 기다리거나 메시지를 읽은 다음 메시지를 쓸수 있다. 읽기와 쓰기의 다른 조합. 각 스트림의 메시지 순서는 유지된다.
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);

 

API 사용하기

 .proto 파일을 작성하여 인터페이스를 정의하고 protoc 를 통하여 인터페이스 코드를 generator 한다. gRPC 사용자는 클라이언트 측에서 이러한 API 를 호출하고 서버 측에서 해당 API 를 구현한다.

 

 

 

설치하기

Linux : $ sudo apt install -y build-essential autoconf libtool pkg-config

sudo apt-get install autoconf automake libtool curl make g++ unzip

 

protobuf 설치하기 필요없음 

다운로드:  https://developers.google.com/protocol-buffers/docs/downloads

git clone github.com/protocolbuffers/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
./configure --prefix=/opt/gRPC
make -j4 & make install
sudo ldconfig # refresh shared library cache

//설치완료

 

Path, LD Library Path 를 설정함.

gRPC 설치

$ git clone https://github.com/grpc/grpc
$ git submodule update --init
$ mkdir build 
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=/opt/
$ make -j4
$ make install

github.com/abseil/abseil-cpp.git

기본 빌드 수행 


설치 pc

gpr.pc grpc++.pc grpc.pc grpc++_unsecure.pc grpc_unsecure.pc libcares.pc protobuf-lite.pc protobuf.pc zlib.pc

 

Error : 위에서 git submodule update --init 을 안해줘서 발생했음

ABSL_ROOT_DIR abseil-cpp
PROTOBUF_ROOT_DIR
BORINGSSL_ROOT_DIR
ZLIB_ROOT_DIR

 

참고 사이트

공식사이트 : https://grpc.io/

python으로 gRPC / tools 설치 및 작동시키기 : https://hororolol.tistory.com/188

뱅크셀러드 기술블로그 : https://blog.banksalad.com/tech/production-ready-grpc-in-golang/

'gRPC' 카테고리의 다른 글

[gRPC] 02. 예제 테스트  (0) 2020.07.16

+ Recent posts