grpc-practise: using go as server and php | node as client
Clone this repository or open it on Github Codespaces.
Then download submodule:
git submodule update --init --recursive
# download protobuf compiler and make it
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protobuf-all-3.14.0.tar.gz
tar -zxvf protobuf-all-3.14.0.tar.gz
cd protobuf-3.14.0
./configure
make
make install
# load the dynamic library
ldconfig
# check version
protoc --version
GO111MODULE=on GOPROXY=https://goproxy.cn go get github.com/golang/protobuf/[email protected]
GO111MODULE=on GOPROXY=https://goproxy.cn go get github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v1
pecl install grpc-1.34.0
## find the location of php.ini and enable grpc plugin
php -i | grep php.ini
echo 'extension=grpc.so' >> php.ini
## install composer dependencies
composer install
## install protobuf runtime library
pecl install protobuf
echo 'extension=protobuf.so' >> php.ini
## check plugin status
php -m | egrep 'grpc|protobuf'
Before build grpc_php_plugin
, install bazel
.
sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
apt update
apt install bazel
Then build grpc_php_plugin
git submodule update --init --recursive
cd grpc
## build grpc_php_plugin with bazel
bazel build @com_google_protobuf//:protoc
bazel build src/compiler:grpc_php_plugin
cd node-client
yarn add grpc @grpc/proto-loader
From the repository root:
protoc -I ./proto -I./proto/googleapis --go_out=plugins=grpc:./go-server/proto/greeter ./proto/*.proto
protoc -I ./proto -I./proto/googleapis --grpc-gateway_out=:./go-server/proto/greeter ./proto/*.proto
Then two files greeter.pb.go
and greeter.pb.gw.go
shall show under the path go-server/proto/greeter
.
You can start the server in first console and test the client request on the second and post a curl request on the third.
# console 1
go run .
# console 2
cd go-server && go test -v .
# === RUN TestSayHello
# pkg_test.go:27: get response from SayHello: hello, yuchanns
# --- PASS: TestSayHello (0.00s)
# PASS
# ok github.com/yuchanns/grpc-practise 0.007s
# console 3
curl -X POST -d '{"name": "curl"}' localhost:8080/api/greeter/say_hello
# {"msg":"hello, curl"}
From the repository root:
protoc -I ./proto -I./proto/googleapis --php_out=./php-client/proto --grpc_out=./php-client/proto --plugin=protoc-gen-grpc=./grpc/bazel-bin/src/compiler/grpc_php_plugin ./proto/*.proto
Then two folders GPBMetadata
and Greeter
shall show under the path php-client/proto
You can run the php client to request go grpc server and get response.
php php-client/main.php
# hello, php
Simply load the protofile
// node-client/index.js
const PROTO_PATH = __dirname + '/../proto/greeter.proto'
const grpc = require('grpc')
const protoLoader = require('@grpc/proto-loader')
const packageDefinition = protoLoader.loadSync(PROTO_PATH)
const greeter = grpc.loadPackageDefinition(packageDefinition).greeter
const client = new greeter.Greeter("localhost:9090", grpc.credentials.createInsecure())
client.SayHello({name: "node"}, (error, resp) => {
if (error) {
console.log(error)
return
}
console.log(resp)
})
Then run in console.
node index.js
# { msg: 'hello, node' }