使用 Node.js 的 gRPC 更新 Redis 数据
需要三个东西:设定文件、服务器程序、客户端程序。
配置文件
redis_update.proto
与此文件相同
在Python的gRPC中使用Redis更新数据 (更新)
服务器程序
#! /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()
// ---------------------------------------------------------------
客户端程序
#! /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