使用 Node.js 的 gRPC 删除 Redis 数据 (Delete)
需要三个东西:设置文件、服务器程序和客户端程序。
配置文件
redis_delete.proto
与这个文件相同
使用 Python 的 gRPC 来删除 Redis 数据(Delete)
服务器程序
#! /usr/bin/node
// ---------------------------------------------------------------
// redis_delete_server.js
//
// Feb/08/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_delete.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_delete_proto = grpc.loadPackageDefinition(packageDefinition).redis_delete
// ---------------------------------------------------------------
function redis_delete_proc(key_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.del(key_in)
client.quit()
}
// ---------------------------------------------------------------
function RedisDelete(call, callback) {
console.error("*** RedisDelete ***")
const key_in = call.request.key
const str_out = 'RedisDelete ' + call.request.key
console.error(str_out)
redis_delete_proc(key_in)
callback(null, {key: call.request.key});
}
// ---------------------------------------------------------------
function main() {
var server = new grpc.Server()
server.addService(redis_delete_proto.Greeter.service, {RedisDelete: RedisDelete })
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
}
// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------
客户端程序
#! /usr/bin/node
// ---------------------------------------------------------------
// redis_delete_client.js
//
// Feb/08/2020
// ---------------------------------------------------------------
var PROTO_PATH = 'redis_delete.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_delete_proto = grpc.loadPackageDefinition(packageDefinition).redis_delete
// ---------------------------------------------------------------
function main() {
var client = new redis_delete_proto.Greeter('localhost:50051',
grpc.credentials.createInsecure())
var key_in = process.argv[2]
client.RedisDelete({key: key_in}, function(err, response) {
console.log('key:', response.key)
})
}
// ---------------------------------------------------------------
main()
// ---------------------------------------------------------------
启动服务器程序
export NODE_PATH=/usr/lib/node_modules
./redis_delete_server.js
执行客户端程序
$ export NODE_PATH=/usr/lib/node_modules
$ ./redis_delete_client.js t1855
key: t1855