前端使用 gRPC

源代碼位置

1. 下載官方示例

git clone git@github.com:grpc/grpc-node.git
  • 復(fù)制examples\helloworld\static_codegendemo

2. 得到如下目錄

image.png
  • 安裝項(xiàng)目依賴
npm i @grpc/grpc-js
npm i google-protobuf
npm i minimist

2.1 安裝工具依賴

npm install -g grpc-tools

2.2 編譯 .proto 文件

cd ./pb
grpc_tools_node_protoc.cmd --js_out=import_style=commonjs,binary:./ --grpc_out=grpc_js:./ helloworld.proto
  • 這里根據(jù)官方給的grpc_tools_node_protoc --js_out=import_style=commonjs,binary:./ --grpc_out=grpc_js:./ helloworld.proto得不到helloworld_pb.js文件; 詳情請(qǐng)看helloworld_pb.js file is not generated #2600

3. 修改官方示例

// greeter_server.js

  - server.addService(services.GreeterService, {sayHello: sayHello});
  + server.addService(services['helloworld.Greeter'], {sayHello: sayHello});
// greeter_client.js
- var client = new services.GreeterClient(target,
                                          grpc.credentials.createInsecure());

+ var GreeterClient = grpc.makeGenericClientConstructor(services['helloworld.Greeter']);

+ const client = new GreeterClient(target, grpc.credentials.createInsecure())

4.運(yùn)行

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

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

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