0
0

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 5 years have passed since last update.

Node.js の gRPC で Redis のデータを更新 (Update)

Posted at

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

設定ファイル

redis_update.proto
こちらと同じ
Python の gRPC で Redis のデータを更新 (Update)

サーバープログラム

redis_update_server.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	redis_update_server.js
//
//					Feb/09/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_update.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_update_proto = grpc.loadPackageDefinition(packageDefinition).redis_update

// ---------------------------------------------------------------
function get_current_date_proc()
{
	const today = new Date ()
	var ddx = (1900 + today.getYear ()) + "-" + (today.getMonth () +1)
	ddx += "-" + today.getDate ()

	return ddx
}

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

	client.on("error", function (err)
		{
		console.log("Redis connection error to "
			+ client.host + ":" + client.port + " - " + err)
		})

client.get (key_in, function (err, reply)
	{
	if (err)
		{
		console.log("Get error: " + err)
        	}
	 else if (reply != null)
		{
		var json_str = reply

		var unit_aa = JSON.parse (json_str)

		unit_aa.population = population_in
		unit_aa.date_mod =  get_current_date_proc ()
		var json_out = JSON.stringify (unit_aa)

		console.log (json_out)

		client.set (key_in,json_out, redis.print)
		client.quit()
		}
	})

}

// ---------------------------------------------------------------
function RedisUpdate(call, callback) {
	console.error("*** RedisUpdate ***")
	const key_in = call.request.key
	const population_in = call.request.population
	const str_out = 'RedisUpdate ' + call.request.key
	console.error(str_out)
	redis_update_proc(key_in,population_in)
	callback(null, {key: call.request.key});
}

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

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

クライアントプログラム

redis_update_client.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	redis_update_client.js
//
//						Feb/09/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_update.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_update_proto = grpc.loadPackageDefinition(packageDefinition).redis_update

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

	var key_in = process.argv[2]
	var population_in = process.argv[3]
	client.RedisUpdate({key: key_in,population: population_in},
		function(err, response) {
	console.log('key:', response.key)
	})

}

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

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

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

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

$ export NODE_PATH=/usr/lib/node_modules
$ ./redis_update_client.js t1857 8234500
key: t1857
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?