NodeJS GRPC簡(jiǎn)單的示例詳解
1. 定義 .proto 文件
首先,創(chuàng)建一個(gè) .proto 文件,定義服務(wù)和消息:
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. 實(shí)現(xiàn)服務(wù)器
創(chuàng)建 `greeter_server.js` 文件,包含服務(wù)的實(shí)現(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. 實(shí)現(xiàn)客戶端
創(chuàng)建 greeter_client.js 文件,包含客戶端的實(shí)現(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. 運(yùn)行服務(wù)器和客戶端
確保你已經(jīng)安裝了所有必要的依賴:
npm install @grpc/grpc-js @grpc/proto-loader
然后,分別運(yùn)行服務(wù)器和客戶端:
node greeter_server.js node greeter_client.js
到此這篇關(guān)于NodeJS GRPC簡(jiǎn)單的例子的文章就介紹到這了,更多相關(guān)NodeJS GRPC例子內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node+Express連接MySQL實(shí)現(xiàn)增刪改查功能
這篇文章主要為大家詳細(xì)介紹了Node如何結(jié)合Express連接MySQL實(shí)現(xiàn)增刪改查功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-05-05
socket.io學(xué)習(xí)教程之深入學(xué)習(xí)篇(三)
這篇文章更加深入的給大家介紹了socket.io的相關(guān)資料,之前已經(jīng)介紹了socket.io的基本教程和應(yīng)用,本文更為深入的來(lái)介紹下socket.io的使用,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-04-04
NodeJS http模塊用法示例【創(chuàng)建web服務(wù)器/客戶端】
這篇文章主要介紹了NodeJS http模塊用法,結(jié)合實(shí)例形式分析了node.js創(chuàng)建web服務(wù)器與客戶端,進(jìn)行HTTP通信的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
淺析node連接數(shù)據(jù)庫(kù)(express+mysql)
Node是一個(gè)Javascript運(yùn)行環(huán)境(runtime)。實(shí)際上它是對(duì)Google V8引擎進(jìn)行了封裝。V8引 擎執(zhí)行Javascript的速度非???,性能非常好。Node對(duì)一些特殊用例進(jìn)行了優(yōu)化,提供了替代的API,使得V8在非瀏覽器環(huán)境下運(yùn)行得更好2015-11-11
node.js平臺(tái)下利用cookie實(shí)現(xiàn)記住密碼登陸(Express+Ejs+Mysql)
這篇文章主要介紹了node.js平臺(tái)下利用cookie實(shí)現(xiàn)記住密碼登陸(Express+Ejs+Mysql),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Node.js 實(shí)現(xiàn) Stripe 支付的實(shí)現(xiàn)方法
本文介紹了使用Stripe支付系統(tǒng)的實(shí)現(xiàn)方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-11-11
Node.js讀取本地CSV文件并且寫入為JSON格式文件過(guò)程
本文介紹了使用Node.js v14.18.1讀取CSV文件并處理中文亂碼的方法,包括使用`fs.readdirSync`讀取文件夾、`TextDecoder`處理編碼、以及如何解析和處理CSV數(shù)據(jù)2026-01-01

