LoginSignup
0
0

More than 3 years have passed since last update.

Node.js の gRPC で Redis のデータを読む (Read)

Last updated at Posted at 2020-02-09

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

設定ファイル

redis_read.proto
こちらと同じ
Python の gRPC で Redis のデータを読む (Read)

サーバープログラム

redis_read_server.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  redis_read_server.js
//
//                  Feb/08/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_read.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 redis_read_proto = grpc.loadPackageDefinition(packageDefinition).redis_read

// ---------------------------------------------------------------
function redis_read_proc(key_in,callback)
{
    const redis = require ("redis")
    const client = redis.createClient (6379,'localhost')

client.get (key_in, function (err, reply)
    {
    if (err)
        {
        console.log("Get error: " + err)
            }
     else if (reply != null)
        {
        const str_json = reply
        const data = JSON.parse(str_json)

        var out_str = key_in + "\t"
        out_str  += data.name + "\t"
        out_str += data.population + "\t"
        out_str += data.date_mod
        console.log (out_str)
    callback(null, {str_json: str_json})
        }

    client.quit()
    })
}

// ---------------------------------------------------------------
function RedisRead(call, callback) {
    console.error("*** RedisDelete ***")
    const key_in = call.request.key
    const str_out = 'RedisDelete ' + key_in
    console.error(str_out)
    redis_read_proc(key_in,callback)
//  callback(null, {key: key_in})
}

// ---------------------------------------------------------------
function main() {
    var server = new grpc.Server()
    server.addService(redis_read_proto.Greeter.service, {RedisRead: RedisRead })
    server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
    server.start()
}

// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------

クライアントプログラム

redis_read_client.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  redis_read_client.js
//
//                      Feb/08/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_read.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 redis_read_proto = grpc.loadPackageDefinition(packageDefinition).redis_read

// ---------------------------------------------------------------
function main() {
  var client = new redis_read_proto.Greeter('localhost:50051',
                                       grpc.credentials.createInsecure())

    var key_in = process.argv[2]
    client.RedisRead({key: key_in}, function(err, response) {
//    console.log('str_json:', response.str_json)

    const data = JSON.parse(response.str_json)
    var out_str = data.name + "\t"
    out_str += data.population + "\t"
    out_str += data.date_mod
    console.log (out_str)
  })

}

// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------

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

export NODE_PATH=/usr/lib/node_modules
./redis_read_server.js

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

$ export NODE_PATH=/usr/lib/node_modules
$ ./redis_read_client.js t1852
敦賀  41295   2003-5-10
0
0
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
0