欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NodeJS GRPC簡單的示例詳解

 更新時間:2024年10月16日 09:38:04   作者:田猿筆記  
本文介紹了如何使用NodeJS GRPC,包括.proto文件的定義、客戶端和服務器的實現(xiàn),首先,創(chuàng)建.proto文件定義服務和消息,然后實現(xiàn)客戶端greeter_client.js文件,最后運行服務器和客戶端,通過這篇文章,你可以快速掌握NodeJS GRPC的基礎使用方法

1. 定義 .proto 文件

首先,創(chuàng)建一個 .proto 文件,定義服務和消息:

syntax = "proto3";
package helloworld;
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
  string name = 1;
}
message HelloReply {
  string message = 1;
}
### 2. 實現(xiàn)服務器
創(chuàng)建 `greeter_server.js` 文件,包含服務的實現(xiàn):
```javascript
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('helloworld.proto', {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true
});
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
function sayHello(call, callback) {
  callback(null, { message: 'Hello ' + call.request.name });
}
function sayHelloAgain(call, callback) {
  callback(null, { message: 'Hello again, ' + call.request.name });
}
function main() {
  const server = new grpc.Server();
  server.addService(helloProto.Greeter.service, { sayHello: sayHello, sayHelloAgain: sayHelloAgain });
  server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
    server.start();
  });
}
main();

3. 實現(xiàn)客戶端

創(chuàng)建 greeter_client.js 文件,包含客戶端的實現(xiàn):

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('helloworld.proto', {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true
});
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
function main() {
  const client = new helloProto.Greeter('localhost:50051', grpc.credentials.createInsecure());
  client.sayHello({ name: 'World' }, (err, response) => {
    if (err) console.error(err);
    else console.log('Greeting:', response.message);
  });
  client.sayHelloAgain({ name: 'World' }, (err, response) => {
    if (err) console.error(err);
    else console.log('Greeting:', response.message);
  });
}
main();

4. 運行服務器和客戶端

確保你已經(jīng)安裝了所有必要的依賴:

npm install @grpc/grpc-js @grpc/proto-loader

然后,分別運行服務器和客戶端:

node greeter_server.js
node greeter_client.js

到此這篇關于NodeJS GRPC簡單的例子的文章就介紹到這了,更多相關NodeJS GRPC例子內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • nodejs簡單抓包工具使用詳解

    nodejs簡單抓包工具使用詳解

    這篇文章主要介紹了nodejs簡單抓包工具使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • nodejs切換版本使用最新教程(不需要卸載重裝)

    nodejs切換版本使用最新教程(不需要卸載重裝)

    有時候需要運行不同的項目,node版本不一致會導致不少問題,特別是最新版本對應的一些插件,由于語法等原因?qū)е虏灰欢嫒莸桶姹?這樣運行低版本環(huán)境的項目的時候很多坑,這篇文章主要給大家介紹了關于nodejs切換版本使用(不需要卸載重裝)的相關資料,需要的朋友可以參考下
    2022-11-11
  • node.js中的querystring.unescape方法使用說明

    node.js中的querystring.unescape方法使用說明

    這篇文章主要介紹了node.js中的querystring.unescape方法使用說明,本文介紹了querystring.unescape的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • 10個Node.js庫幫助你優(yōu)化代碼和簡化開發(fā)

    10個Node.js庫幫助你優(yōu)化代碼和簡化開發(fā)

    這篇文章主要介紹了10個Node.js庫幫助你優(yōu)化代碼和簡化開發(fā),其中包括處理數(shù)組、對象、字符串庫Lodash,緩存數(shù)據(jù)處理庫Node-cache,解析、操作和格式化日期和時間庫Moment.js,Redis操作庫,發(fā)送電子郵件庫Nodemailer
    2023-05-05
  • Node.js的中間件及使用方法詳解

    Node.js的中間件及使用方法詳解

    在Node.js的生態(tài)中,中間件(Middleware)是一個不可或缺的概念,它為構(gòu)建靈活而高效的應用程序提供了強大的支持,下面給大家介紹Node.js的中間件及使用方法,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Node.js中的HTTP請求與響應詳解

    Node.js中的HTTP請求與響應詳解

    本文詳細講解了Node.js中的HTTP請求與響應,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • node.js如何操作MySQL數(shù)據(jù)庫

    node.js如何操作MySQL數(shù)據(jù)庫

    這篇文章主要介紹了node.js如何操作MySQL數(shù)據(jù)庫,幫助大家更好的進行web開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • 使用node.JS中的url模塊解析URL信息

    使用node.JS中的url模塊解析URL信息

    本文將詳細介紹nodeJS中的URL模塊的使用方法,利用URL模塊解析出URL相關信息
    2020-02-02
  • 切換到淘寶最新npm鏡像源的全面指南(支持 Windows、macOS 和多種 Linux 發(fā)行版)

    切換到淘寶最新npm鏡像源的全面指南(支持 Windows、macOS 和多種 Linux

    在開發(fā)過程中,npm 是前端開發(fā)者不可或缺的工具,但對于國內(nèi)的開發(fā)者來說,npm 官方源在下載速度上存在一定的瓶頸,本文將詳細介紹如何在 Windows、macOS 以及各類 Linux 發(fā)行版上切換到淘寶的 npm 鏡像源,需要的朋友可以參考下
    2025-03-03
  • Express框架定制路由實例分析

    Express框架定制路由實例分析

    這篇文章主要介紹了Express定制路由,結(jié)合實例形式分析了express框架定制路由原理、用法及相關注意事項,需要的朋友可以參考下
    2023-05-05

最新評論