2021-04-09 grpcurl使用

參考文章使用grpcurl訪問gRPC服務(wù)使用grpcurl,但是我的代碼是c代碼,因此又參考Quick start,找到了一個c代碼的例子,操作如下:

export MY_INSTALL_DIR=$HOME/.local
mkdir -p $MY_INSTALL_DIR
export PATH="$PATH:$MY_INSTALL_DIR/bin"

git clone --recurse-submodules -b v1.35.0 https://github.com/grpc/grpc
$ cd grpc
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DgRPC_INSTALL=ON \
      -DgRPC_BUILD_TESTS=OFF \
      -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
      ../..
$ make -j
$ make install
$ popd

cd examples/cpp/helloworld
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
$ make -j

這個時候c代碼編譯成功了,然后

$ ./greeter_server
Server listening on 0.0.0.0:50051
$ ./greeter_client
Greeter received: Hello world

2、下載grpcurl
下載最新版本的,在下載找到最新版本下載

3、使用

$ grpcurl -plaintext 127.0.0.1:50051 list
grpc.health.v1.Health
grpc.reflection.v1alpha.ServerReflection
helloworld.Greeter

這里找到服務(wù)helloworld.Greeter

$ grpcurl -plaintext 127.0.0.1:50051 describe helloworld.Greeter
helloworld.Greeter is a service:
service Greeter {
  rpc SayHello ( .helloworld.HelloRequest ) returns ( .helloworld.HelloReply );
}

這里找到函數(shù)helloworld.Greeter.SayHello和輸入?yún)?shù)helloworld.HelloRequest

$ grpcurl -plaintext 127.0.0.1:50051 describe helloworld.HelloRequest
helloworld.HelloRequest is a message:
message HelloRequest {
  string name = 1;
}

這里查看輸入?yún)?shù)helloworld.HelloRequest的格式,然后構(gòu)造json數(shù)據(jù):

$ grpcurl -d '{"name": "abc"}' -plaintext 127.0.0.1:50051 helloworld.Greeter.SayHello
{
  "message": "Hello abc"
}

通過調(diào)用函數(shù) helloworld.Greeter.SayHello 返回結(jié)果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容