有一个服务器上运行两个 Docker 化应用程序实例。其中一台在默认端口 6379 上运行 Redis,另一台(如下所述)在端口 7379 上运行 Redis。
当我尝试启动服务器实例时,显示以下错误:
sw_redis | 1:C 31 Aug 2023 12:04:33.661 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
sw_redis | 1:C 31 Aug 2023 12:04:33.661 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started
sw_redis | 1:C 31 Aug 2023 12:04:33.661 # Configuration loaded
sw_redis | 1:M 31 Aug 2023 12:04:33.661 * monotonic clock: POSIX clock_gettime
sw_redis | 1:M 31 Aug 2023 12:04:33.662 * Running mode=standalone, port=7379.
sw_redis | 1:M 31 Aug 2023 12:04:33.662 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
sw_redis | 1:M 31 Aug 2023 12:04:33.662 # Server initialized
sw_redis | 1:M 31 Aug 2023 12:04:33.662 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
sw_redis | 1:M 31 Aug 2023 12:04:33.662 * Ready to accept connections
sw_server | node:internal/process/promises:288
sw_server | triggerUncaughtException(err, true /* fromPromise */);
sw_server | ^
sw_server |
sw_server | Error: connect ECONNREFUSED 127.0.0.1:6379
sw_server | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16)
sw_server | Emitted 'error' event on Commander instance at:
sw_server | at RedisSocket.<anonymous> (/app/node_modules/@redis/client/dist/lib/client/index.js:395:14)
sw_server | at RedisSocket.emit (node:events:514:28)
sw_server | at RedisSocket._RedisSocket_connect (/app/node_modules/@redis/client/dist/lib/client/socket.js:166:18)
sw_server | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
sw_server | at async prepareServer (file:///app/setupServer.js:97:5)
sw_server | at async startServer (file:///app/setupServer.js:111:20)
sw_server | at async file:///app/server.js:4:5 {
sw_server | errno: -111,
sw_server | code: 'ECONNREFUSED',
sw_server | syscall: 'connect',
sw_server | address: '127.0.0.1',
sw_server | port: 6379
sw_server | }
sw_server |
sw_server | Node.js v18.17.1
sw_server exited with code 1
错误转储引用的片段在这里:
const pubClient = createClient({
host: REDIS_HOST,
port: REDIS_PORT,
no_ready_check: true,
auth_pass: REDIS_PASSWORD,
});
const subClient = pubClient.duplicate();
await pubClient.connect();
await subClient.connect();
此外,Redis 客户端是通过这种方式创建的,如文档中所述:
const client = createClient({
legacyMode: true,
password: REDIS_PASSWORD,
socket: {
host: REDIS_HOST,
port: REDIS_PORT
}
});
client.on('error', (err) => console.log('Redis Client Error', err));
客户版本:
"redis": "^4.6.7"
.
Docker 构建镜像并基于此运行容器
docker-compose.yml
:
version: '3'
services:
server:
container_name: sw_server
networks:
- sw_net
build: .
environment:
REDIS_HOST: sw_redis
REDIS_PORT: 7379
REDIS_PASSWORD: 'Redis-Password-08'
ports:
- '3017:4000'
depends_on:
- sw_redis
links:
- sw_redis
sw_redis:
image: redis
environment:
REDIS_PASSWORD: 'Redis-Password-08'
networks:
- sw_net
command: redis-server --requirepass "Redis-Password-08" --port 7379
ports:
- '7379:7379'
networks:
sw_net:
external: true
已经尝试过这里、这里和这里描述的解决方案(顺便说一句:你应该检查那里的低点答案,太棒了;-))。
发布的解决方案均无效。硬编码例如JS 代码中的端口号也不起作用:不知何故,连接路由到端口 6379 并在那里被拒绝,因为它是一个不同的实例。我的工作在端口 7379,但没有请求到达它。
有什么想法如何进一步调试吗?
已解决:我将
pubClient
的配置修改为:
const pubClient = createClient({
password: REDIS_PASSWORD,
no_ready_check: true,
socket: {
host: REDIS_HOST,
port: REDIS_PORT
}
});
否则,客户端会尝试连接到默认端口 6379 上的
localhost
。