恥知らずのウェブエンジニア -web engineer, shameless

これは一歩を踏み出すことができない者たちのブログ

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

  1. Define proto
  2. Generate code for Go
  3. Create a Go gRPC server with generated code
  4. Generate code for PHP
  5. Create a PHP client with the generated code
  6. 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

grpc.io

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.

github.com

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! ^^

developers.eure.jp

The actual code is here

github.com

Generate code for PHP

Next, I will generate code for PHP for requesting the gRPC server.

grpc.io

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).

github.com

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.

github.com

github.com

github.com