A Brief Walk of gRPC
7114 단어 OpenSourceapi
What is gRPC
gRPC is a high performance, open-source universal RPC framework, developed by Google. In gRPC, a client application can directly call methods on a server application on a different machine as if it was a local object, making it easier to create distributed applications and services.
One of the primary benefits of using gRPC is for documentation, you can use your service configuration and API interface definition files to generate reference documentation for your API.
The reason for this is that, gRPC can use protocol buffers as both its Interface Definition Language (IDL) and as it underlying message interchange format.
As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
On the server side, the server implements this interface and runs a gRPC server to handle client calls.
On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.
source: Introduction to gRPC
Why gRPC?
The most important reason to use gRPC is that, it is fast.
Another important reason that may be not so obvious is that, it helps programmers writer BETTER code, because you have to follow its guidelines to generate API interfaces.
Working with Protocol Buffers
By default, gRPC uses Protocol Buffers, Google's open source mechanism for serializing structured data. And it can also be used with other data formats such as JSON. You can check the language guide below.
Language Guide | Protocol Buffers | Google Developers
A simple example of protocol buffers is as below,
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
}
One great advantage of protocol buffers is that, it is language neutral. Protocol buffers currently support generated code in Java, Python, Objective-C, and C++. And with proto3 language version, you can also work with Dart, Go, Ruby, and C#.
Protocol Buffers with Python
Install
Install gRPC
$ python -m pip install grpcio
Install gRPC tools
$ python -m pip install grpcio-tools
정의 프로토콜 형식
To work with protocol buffers using Python, the first thing you need to do is to define your protocol format. You start with a
.proto
file, the following is an official example.
syntax = "proto2";
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
Compiling Your Protocol Buffers
Once you have the
.proto
file, you need compile it. Download the compiler here.Then compile it using the following command
protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/sample.proto
This will generate a
sample_pb2.py
in the given destination. Then you can easily import it into your python code as follows.
import sample_pb2
Address_Book = sample_pb2.AddressBook()
Reference
이 문제에 관하여(A Brief Walk of gRPC), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/cece/items/d0e687922e0cd621ca3b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)