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