0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gRPC の使い方 (Node.js)

Last updated at Posted at 2020-02-05

参考ページ
Node Quick Start

必要なライブラリーのインストール

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

設定ファイル、サーバープログラム、クライアントプログラムの3つが必要です。

設定ファイル

helloworld.proto
こちらと同じ
gRPC の使い方 (python)

サーバープログラム

greeter_server.js
var PROTO_PATH = 'helloworld.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

function sayHello(call, callback) {
  console.error("*** sayHello ***")
  const str_out = 'Test Hello ' + call.request.name
  console.error(str_out)
  callback(null, {message: str_out});
}

function sayHello2(call, callback) {
  console.error("*** sayHello2 ***")
  const str_out = 'Test2 Hello again ' + call.request.name
  console.error(str_out)
  callback(null, {message: str_out});
}


function main() {
  var server = new grpc.Server();
  server.addService(hello_proto.Greeter.service, {sayHello: sayHello,
		sayHello2: sayHello2});
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  server.start();
}

main();

クライアントプログラム

greeter_client.js

var PROTO_PATH = 'helloworld.proto';

var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

function main() {
  var client = new hello_proto.Greeter('localhost:50051',
                                       grpc.credentials.createInsecure());
  var user;
  user = 'John';

  client.sayHello({name: user}, function(err, response) {
    console.log('Greeting:', response.message);
  });

  user = 'Tom';
  client.sayHello2({name: user}, function(err, response) {
    console.log('Greeting:', response.message);
  });


}

main();

サーバープログラムの起動

export NODE_PATH=/usr/lib/node_modules
node greeter_server.js

クライアントプログラムの実行

$ export NODE_PATH=/usr/lib/node_modules
$ node greeter_client.js
Greeting: Test2 Hello again Tom
Greeting: Test Hello John

サーバーのコンソールには次のようなメッセージが出ます。

$ export NODE_PATH=/usr/lib/node_modules
$ node greeter_server.js
*** sayHello2 ***
Test2 Hello again Tom
*** sayHello ***
Test Hello John
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?