gRPCをMacに導入し、Hello Worldを実行する
ゴール
手元のMacでgRPCを導入し、Hello Worldを実行する
Mac環境
System Version: OS X 10.10.2
gRPCインストール
- goのバージョンを1.4以上にする
- GOPATHの設定
goのバージョンを1.4以上にする
$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
.bashrcの最後に以下を入れる
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
go1.4.2を導入する
$ gvm install go1.4.2
$ gvm use go1.4.2 --default
$ gvm list
$ go version
go version go1.4.2 darwin/amd64
gRPCを導入する
リポジトリをcloneしてくる
$ git clone git@github.com:grpc/grpc.git
$ cd grpc
INSTALLファイルにいろいろ書いてあるので参考にしながらインストールする
$ git submodule update --init
$ make
$ sudo make install
最後にProtocol Bufferの3.0.0+がインストールされていないと意味あるコードがかけないよと出てきた
We couldn't find protoc 3.0.0+ installed on your system. While this
won't prevent grpc from working, you won't be able to compile
and run any meaningful code with it.
Please download and install protobuf 3.0.0+ from:
https://github.com/google/protobuf/releases
Once you've done so, or if you think this message is in error,
you can re-run this check by doing:
make verify-install
Protocol Bufferの導入
3.0.0+を入れます。以下適切な場所で導入します。checkoutするtagは適宜読み変えてください。
$ git clone https://github.com/google/protobuf
$ cd protobuf
$ git checkout v3.0.0-alpha-2
$ ./configure
$ make
$ make check
$ make install
$ which protoc
/usr/local/bin/protoc
$ protoc --version
libprotoc 3.0.0
gRPCをverifyする
gRPCのリポジトリに移動し、verificationを実施する
$ make verify-install
Your system looks ready to go.
goのProtocol Bufferのプラグインを導入する
$ go get -a github.com/golang/protobuf/protoc-gen-go
pbのファイル更新
普通に実行するとgrc-commonのリポジトリで以下を行う
$ cd go
$ protoc -I ../protos ../protos/helloworld.proto --go_out=plugins=grpc:helloworld
更新されるので、packageにあるファイルを上書きする
$ cp helloworld/helloworld.pb.go ~/.gvm/pkgsets/go1.4.2/global/src/github.com/grpc/grpc-common/go/helloworld/helloworld.pb.go
Hello Worldの実行
サーバ、クライアントと順に実行すると、goでgRPCのHello Worldができました
サーバ側
$ go run greeter_server/main.go
クライアント側
$ go run greeter_client/main.go
2015/03/27 15:48:27 Greeting: Hello world
おまけ
C++のクライアントでGoのサーバへつなぎにいきます。grpc-commonの以下のようにディレクトリ移動します。 その後、Protocol Bufferの定義をコンパイルします。
$ cd cpp/helloworld
$ make hellowrold.pb.cc
以下のようにmakeしてクライアントを実行します。
$ make
$ ./greeter_client
Greeter received: Hello world
Pythonもやろうとしたのですが、Mac上だと各種エラーがでたので、Linuxをプラットフォームとしてコンパイルなどをしたほうがいいかもしれません。