gRPCとは何か?

gRPCと英語の勉強を兼ねて
grpc.io
を翻訳。

This document introduces you to gRPC and protocol buffers. gRPC can use protocol buffers as both its IDL and as its underlying message interchange format. If you’re new to gRPC and/or protocol buffers, read this! If you just want to dive in and see gRPC in action first, see our Quick Starts.

このドキュメントでは、gRPCとプロトコルバッファについて紹介します。
gRPCは、プロトコルバッファをIDL(インターフェース定義言語)と、それによって生成されるメッセージ交換フォーマットの両方として使用できます。
gRPCとプロトコルバッファの両方もしくはどちらかを初めて使う方は本ドキュメントを読んでください!
まずはgRPCを見てみたいと思ったら、クイックスタートをご覧ください。

概要

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 for you to create distributed applications and services. 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.

gRPCでは、クライアントアプリケーションは、異なるマシン上のサーバーアプリケーション上のメソッドをローカルオブジェクトであるかのように直接呼び出すことができ、分散アプリケーションやサービスを簡単に作成できます。
多くのRPCシステムと同様に、gRPCはサービスを定義し、そのパラメータと戻り値の型でリモートで呼び出せるメソッドを指定するという考え方に基づいています。
サーバー側では、このインターフェースを実装し、gRPCサーバーを実行してクライアント呼び出しを処理します。
クライアント側では、サーバと同じメソッドを提供するスタブ(いくつかの言語では単にクライアントと呼ばれる)を持ちます。

f:id:hapoon:20170928160006p:plain
(from What is gRPC?)

gRPC clients and servers can run and talk to each other in a variety of environments - from servers inside Google to your own desktop - and can be written in any of gRPC’s supported languages. So, for example, you can easily create a gRPC server in Java with clients in Go, Python, or Ruby. In addition, the latest Google APIs will have gRPC versions of their interfaces, letting you easily build Google functionality into your applications.

gRPCのクライアントとサーバーはさまざまな環境(Google内のサーバーから各自のデスクトップに至るまで)で動作し、お互いに通信することができ、gRPCがサポートしている言語で書くことができます。
たとえば、gRPCサーバーはJavaで、クライアントはGo、PythonRubyのような構成を簡単に作成できます。
さらに、最新のGoogle APIではgRPCバージョンのインターフェースが用意されており、アプリケーションにGoogleの機能を簡単に組み込むことができます。

プロトコルバッファでの作業

By default gRPC uses protocol buffers, Google’s mature open source mechanism for serializing structured data (although it can be used with other data formats such as JSON). Here’s a quick intro to how it works. If you’re already familiar with protocol buffers, feel free to skip ahead to the next section.

The first step when working with protocol buffers is to define the structure for the data you want to serialize in a proto file: this is an ordinary text file with a .proto extension. Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields. Here’s a simple example:

デフォルトでは、gRPCは構造化されたデータをシリアル化するためのGoogleの成熟したオープンソースメカニズムであるプロトコルバッファを使用します。(JSONなどの他のデータフォーマットでも使用できます)
ここではどのように動作するかを簡単に紹介します。
すでにプロトコルバッファに精通している場合は、次のセクションに進んでください。

プロトコルバッファで作業する場合の最初の手順は、protoファイル(.proto拡張子を持つ中身はテキストファイル)でシリアル化するデータの構造を定義することです。
プロトコルバッファデータはメッセージとして構成され、各メッセージはフィールドと呼ばれる一連の名前と値のペアを含む情報の小さな論理レコードです。
ここに簡単な例があります:

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

Then, once you’ve specified your data structures, you use the protocol buffer compiler protoc to generate data access classes in your preferred language(s) from your proto definition. These provide simple accessors for each field (like name() and set_name()) as well as methods to serialize/parse the whole structure to/from raw bytes – so, for instance, if your chosen language is C++, running the compiler on the above example will generate a class called Person. You can then use this class in your application to populate, serialize, and retrieve Person protocol buffer messages.

データ構造を指定したら、プロトコルバッファコンパイラprotocを使用して、プロト定義からあなたの選んだ言語でデータアクセスクラスを生成します。
データアクセスクラスは、各フィールドの簡単なアクセサ(name()やset_name()のような)を提供するだけでなく、全構造体をバイト情報にシリアライズするためのメソッドやバイト情報から構造を解析するためのメソッドを提供します。
たとえば、選択した言語がC ++で、上記の例に対してコンパイラを実行するとPersonというクラスが生成されます。
このクラスをアプリケーションで使用して、Personプロトコル・バッファー・メッセージの取り込み、シリアル化、取り出しができます。

As you’ll see in more detail in our examples, you define gRPC services in ordinary proto files, with RPC method parameters and return types specified as protocol buffer messages:

例で詳しく説明すると、通常のprotoファイルでgRPCサービスを定義し、RPCメソッドのパラメータと戻り値の型にはプロトコルバッファのメッセージを指定します。

// The greeter service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

gRPC also uses protoc with a special gRPC plugin to generate code from your proto file. However, with the gRPC plugin, you get generated gRPC client and server code, as well as the regular protocol buffer code for populating, serializing, and retrieving your message types. We’ll look at this example in more detail below.

gRPCも、protoファイルからコードを生成するために特別なgRPCプラグインとともにprotocを使います。
gRPCプラグインを使用すると、通常のプロトコルバッファのコードとともに、gRPCのクライアントとサーバーのコードが生成されます。
この例をさらに詳しく見ていきます。

You can find out lots more about protocol buffers in the Protocol Buffers documentation, and find out how to get and install protoc with gRPC plugins in your chosen language’s Quickstart.

プロトコルバッファドキュメントプロトコルバッファに関するより詳細を知ることができ、選択した言語のQuickstartでgRPCプラグインを含めprotocのインストール方法を知ることができます。

While protocol buffers have been available for open source users for some time, our examples use a new flavor of protocol buffers called proto3, which has a slightly simplified syntax, some useful new features, and supports lots more languages. This is currently available in Java, C++, Python, Objective-C, C#, a lite-runtime (Android Java), Ruby, and JavaScript from the protocol buffers Github repo, as well as a Go language generator from the golang/protobuf Github repo, with more languages in development. You can find out more in the proto3 language guide and the reference documentation for each language (where available), and see the major differences from the current default version in the release notes. Even more proto3 documentation is coming soon.

しばらくの間、オープンソースのユーザにはプロトコルバッファが提供されていましたが、ここではproto3という新しいプロトコルバッファを使用しています。
proto3は構文がやや簡略化され、便利な新機能がいくつか追加され、より多くの言語をサポートしています。
現在、プロトコルバッファのGithubリポジトリからJava、C ++、PythonObjective-C、C#、Lite-Runtime(Android Java)、RubyJavaScriptで使用できます。
またgolang/protobufのGithubリポジトリではGo言語用のジェネレータも使用できます。
その他の多くの言語も開発中です。
proto3の言語ガイドと(使用可能な)各言語のリファレンスドキュメントを見ればより多くのことが分かります。
現行のデフォルトバージョンとの主な相違点はリリースノートを参照してください。
さらに多くのproto3のドキュメントがまもなく公開予定です。

In general, while you can use proto2 (the current default protocol buffers version), we recommend that you use proto3 with gRPC as it lets you use the full range of gRPC-supported languages, as well as avoiding compatibility issues with proto2 clients talking to proto3 servers and vice versa.

一般的にproto2(現在のデフォルトのプロトコルバッファバージョン)は使用できますが、proto3とgRPCを使用することをお勧めします。
これにより、gRPCがサポートしているすべての言語を使用できるだけでなく、proto2クライアントとproto3サーバーの互換性(その逆も)の問題も回避することもできます。