我在AWS(ElastiCache)上配置了Redis群集。
它有一个配置端点和6个节点(3个分片)。
在阅读npm模块here的文档时,它表示必须使用ioredis才能使其适用于Redis群集。
我分享了下面的配置,该配置适用于单个Redis节点但不适用于群集
"use strict";
// Include modules
var express = require("express"),
cookieParser = require("cookie-parser"),
bodyParser = require("body-parser"),
session = require("express-session"),
RedisStore = require("connect-redis")(session),
middleware = require("./routes/middleware"),
app = express(),
sessionNameOfApp = createAppSpecificSession(configNameOfApp),
//This works but only for a single node. It doesn't work for a Redis Cluster
function createAppSpecificSession(config) {
return session({
store: new RedisStore({
host: config.SESSION.host,
port: config.SESSION.port,
pass: config.SESSION.pass
}),
resave: false,
saveUninitialized: false,
secret: config.SESSION.cookieSecret,
name: config.SESSION.cookieName,
genid: sessionMethods.generateSessionId
});
}
app.use(middleware.setUUIDOnReq);
app.use(middleware.setAgentOnReq);
// additional app modules
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(middleware.setSessionMethods);
// Include routes
require("./routes/app")(express, app);
module.exports = app;
我的问题是 - 如何将ioredis纳入支持群集?
这对我来说是一个巨大的障碍,所以任何帮助都将受到高度赞赏。
在整个网络上寻找一个例子,什么都没有!
答案 0 :(得分:0)
这最终对我有用:
app.js文件:
"use strict";
// Include modules
var express = require("express"),
path = require("path"),
cookieParser = require("cookie-parser"),
bodyParser = require("body-parser"),
session = require("express-session"),
RedisStore = require("connect-redis")(session),
Redis = require('ioredis'),
config = require("./config"),
errorMessage = require("./errors/errorMessage"),
sessionMethods = require("./utils/sessionMethods"),
helmet = require("helmet"),
middleware = require("./routes/middleware"),
app = express(),
session = createAppSpecificSession(config),
function createAppSpecificSession(config) {
var redis = null;
redis = new Redis({
host: config.SESSION.host,
port: config.SESSION.port,
password: config.SESSION.password
});
return session({
store: new RedisStore({
client: redis
}),
resave: false,
saveUninitialized: false,
secret: config.SESSION.cookieSecret,
name: config.SESSION.cookieName,
genid: sessionMethods.generateSessionId
});
}
app.use(helmet());
app.use(middleware.setUUIDOnReq);
app.use(middleware.setAgentOnReq);
// additional app modules
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use("/", session);
// Include routes
require("./routes/app")(express, app);
module.exports = app;
和配置文件:
config.js
module.exports = {
SESSION: {
hosts: [{
host: "endpoint1.0001.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}, {
host: "endpoint2.0002.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}, {
host: "endpoint3.0003.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}],
cookieName: "connect.sid",
cookieSecret: ["blablabla", "blablabla"]
}
};