【发布时间】:2021-03-19 20:04:15
【问题描述】:
我们正在使用 AWS Elasticache 和 Redis 实现缓存,并使用 nodejs 将其与 Redis 连接。创建一个有序集合并在其中添加和获取元素。它在本地机器上运行良好,而在暂存上不工作。这是代码,
const redis = require("redis")
const redisOption = {
host: redis_host_url,
port: redis_port
}
let client = redis.createClient(redisOption);
console.log(client);
client = promisify(client.zadd).bind(client);
console.log(client);
let response = await client("abc",1,"string_to_add");
console.log(response);
在本地机器上打印响应,但 Lambda 日志不包含最后一个控制台。客户是:
RedisClient {
_events: [Object: null prototype] { newListener: [Function] },
_eventsCount: 1,
_maxListeners: undefined,
address: 'XXXXXXXXXX:6379',
connection_options: { port: 6379, host: 'XXXXXXXXXX', family: 4 },
connection_id: 0,
connected: false,
ready: false,
should_buffer: false,
command_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
offline_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
pipeline_queue:
Denque {
_head: 0,
_tail: 0,
_capacityMask: 3,
_list: [ <4 empty items> ] },
connect_timeout: 3600000,
enable_offline_queue: true,
retry_timer: null,
retry_totaltime: 0,
retry_delay: 200,
retry_backoff: 1.7,
attempts: 1,
pub_sub_mode: 0,
subscription_set: {},
monitoring: false,
message_buffers: false,
closing: false,
server_info: {},
auth_pass: undefined,
selected_db: undefined,
fire_strings: true,
pipeline: false,
sub_commands_left: 0,
times_connected: 0,
buffers: false,
options:
{ host: XXXXXXXXXX,
port: 6379,
socket_keepalive: true,
socket_initial_delay: 0,
return_buffers: false,
detect_buffers: false },
reply: 'ON',
reply_parser:
JavascriptRedisParser {
optionReturnBuffers: false,
optionStringNumbers: false,
returnError: [Function: returnError],
returnFatalError: [Function: returnFatalError],
returnReply: [Function: returnReply],
offset: 0,
buffer: null,
bigStrSize: 0,
totalChunkSize: 0,
bufferCache: [],
arrayCache: [],
arrayPos: [] },
stream:
Socket {
connecting: true,
_hadError: false,
_handle:
TCP {
reading: false,
onread: [Function: onStreamRead],
onconnection: null,
[Symbol(owner)]: [Circular] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: null,
pipesCount: 0,
flowing: true,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: true,
paused: false,
emitClose: false,
autoDestroy: false,
destroyed: false,
defaultEncoding: 'utf8',
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
_events:
[Object: null prototype] {
end: [Array],
connect: [Function],
data: [Function],
error: [Function],
close: [Function],
drain: [Function] },
_eventsCount: 6,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: false,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
[Symbol(asyncId)]: 7,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0 } }
promisify 之后的控制台也在工作,并且正在打印:
[Function: bound zadd]
但是最后一个控制台没有打印任何东西。我无法调试,因为 AWS Elasticache 不提供 Redis 日志。
【问题讨论】:
-
你是否从 lambda 超时?
-
@LostJon 不,lambda 的超时是默认值。我没有明确指定超时。但在我得到的 Lambda 日志中,
Task timed out after 6.01 seconds -
所以..你“正在”超时。这里的一切都指向网络问题。确保您的 lambda 具有对 redis 实例的网络访问权限...您可能需要将 lambda 配置为使用与您的 redis 相同的 VPC,如果它不能公开访问
-
@LostJon 是的,很可能这就是问题所在。我正朝着这个方向前进。甚至在 EC2 上设置 Redis 并尝试连接。不过,遇到同样的问题。 VPC 和访问权限有问题。
-
将您的 lambda 配置为与您的 EC2 在同一个 VPC 中,然后在您的 EC2 上配置安全网关以允许来自该 VPC 的所有流量。
标签: node.js redis aws-lambda amazon-elasticache