Set up gRPC server with Go and try requesting with PHP
Hi guys. In this time, I will set up the gRPC server and try requesting it.
By the way, it seems that the gRPC server with PHP is spoken by ↓, but it seems that is no plan now.(2018 / February) groups.google.com
So this time, I will set up a gRPC server with Go and I will try requesting it with PHP.
Flow
- Define proto
- Generate code for Go
- Create a Go gRPC server with generated code
- Generate code for PHP
- Create a PHP client with the generated code
- Set up a gRPC server and request it with PHP client
Define proto
Prepare the following proto.Simply to accept the string and just return it.
syntax = "proto3"; package ping; service Ping { rpc Hello (HelloReqest) returns (HelloResponse) { } } message HelloReqest { string toMessage = 1; } message HelloResponse { string resMessage = 1; }
Generate code for Go
The official is here
What is necessary for generation is as follows.
- google.golang.org/grpc - github.com/golang/protobuf/protoc-gen-go // plugin for generating gRPC go code from proto - protoc // protoc compiler
google.golang.org / grpc
,protoc-gen-go
you can go get
go get -u google.golang.org/grpc go get -u github.com/golang/protobuf/protoc-gen-go
You can download protoc
that fitting your platform from below, And place it in /bin or the like.
So I use mac, I've downloaded protoc-3.5.1-osx-x86_64.zip
.
The code generation command for Go is as follows
// For protos/*.proto, The code generated for gRPC is under. /go protoc - go_out = plugins = grpc: ./ go protos / *. proto
The actual generated code will look like this.
come-on-proto/ping.pb.go at master · ogataka50/come-on-proto · GitHub
Create a Go gRPC server with generated code
I prepare a gRPC server with Go based on the generated code.
This time I've taken the following article quite nicely and I just made a reference as it is! Thank you! ^^
The actual code is here
Generate code for PHP
Next, I will generate code for PHP for requesting the gRPC server.
What we need to generate is below
- grpc-extension - protobuf-extension - protoc // same as installing it at the time of Go - grpc_php_plugin // plugin for generating gRPC PHP code from proto
grpc-extension
,protobuf-extension
can be installed with pecl, brew etc.
brew install php71-grpc brew install php71-protobuf pecl install grpc pecl install protobuf
grpc_php_plugin
can be created by cloning the repo of grpc and make grpc_php_plugin
git clone -b v1.9.x https://github.com/grpc/grpc cd grpc && git submodule update --init && make grpc_php_plugin
The code generation command for PHP is as follows
// For protos/*.proto, The code generated for gRPC is under. /php protoc --proto_path = protos / \ - php_out = php \ --grpc_out = php \ --plugin = protoc-gen-grpc =. / grpc_php_plugin \ ./protos /*.proto
The actual generated code will look like this.
come-on-proto/PingClient.php at master · ogataka50/come-on-proto · GitHub
Create a PHP client with the generated code
Next, we will create a PHP code to request using generated code.
Almost all of the following are just new request()
and set to generatedclient
.
This time we specified wait ()
to do UnaryCall
(once requesting and receiving a response once).
Set up a gRPC server and request it with PHP client
Preparation is done, so I will actually check it.
Build GogRPC Server and run
go build -o bin/server server.go go build -o bin/client client.go - you can use `make build` too. ./bin/server
Request with PHP client
Go back to gRPC-client-PHP
and request it withphp ./bin/console helloGRPC
.
$ php ./bin/console helloGRPC object (stdClass) # 52 (3) { ["metadata"] => array (0) { } ["code"] => int (0) ["details"] => string (0) "" } string (24) "I hear hogeeeeeeeeeeeeee"
If you can successfully request, it should be output as above. It seems like it is done!
Summary
In this time, I set up a gRPC server with Go and requested it with PHP.
At first, there were some confused things, but I was able to check simple operation.
Next time, I'd like to try Streaming RPC
etc. I also care about performance.
I can use in multiple languages based on common proto, I realized that micro-services
certainly seems to be compatible! ^^
The repo used this time will be below. thanks.