我在AWS(ElastiCache(上配置了一个Redis集群。
它有一个配置端点和 6 个节点(3 个分片(。
在这里阅读 npm 模块的文档时,它说你必须合并 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;
我的问题是 - 如何合并碘化以支持集群?
这对我来说是一个巨大的障碍,所以任何帮助将不胜感激。
在网上找了个例子,一无所获!
这是最终对我有用的:
应用程序.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;
和配置文件:
配置.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"]
}
};