node.js - Docker 容器里无法连接其他容器运行的 Redis

浏览:25日期:2022-09-17

问题描述

log

Error: Redis connection to redis:6379 failed - connect ECONNREFUSED 221.179.46.194:6379 at Object.exports._errnoException (util.js:1022:11) at exports._exceptionWithHostPort (util.js:1045:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)

ping

/ # ping mysqlPING mysql (172.19.0.2): 56 data bytes64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.101 ms64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.097 ms64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.100 ms^C--- mysql ping statistics ---3 packets transmitted, 3 packets received, 0% packet lossround-trip min/avg/max = 0.097/0.099/0.101 ms/ # ping redisPING redis (172.19.0.3): 56 data bytes64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.069 ms64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.134 ms64 bytes from 172.19.0.3: seq=2 ttl=64 time=0.112 ms^C--- redis ping statistics ---3 packets transmitted, 3 packets received, 0% packet lossround-trip min/avg/max = 0.069/0.105/0.134 ms/ #

docker-compose

redis: image: redis what: image: daocloud.io/who/what restart: always depends_on:- mysql- redis

问题解答

回答1:

运行容器时候的命令、端口是怎么映射的。

回答2:

找到原因了

这里变动了hostname要另外注入 https://github.com/mcollina/m...server.js

原本的写法

persistence: { factory: mosca.persistence.Redis, url: config.redis.host.concat(’:’).concat(config.redis.port) // 拼接出来不合法的参数redis:6379}

更正

persistence: { factory: mosca.persistence.Redis, host: ’your redis host’, port: ’your redis port’}

persistance的构造方法lib/persistence/redis.js

var Redis = require('ioredis');RedisPersistence.prototype._buildClient = function() { var options = this.options.redisOptions || {}; if (this.options.host) { options.host = this.options.host; } if (this.options.port) { options.port = this.options.port; } if (this.options.db) { options.db = this.options.db; } if (this.options.password) { options.password = this.options.password; } return new Redis(options);};

这是一个ioredis的API,参考 https://github.com/luin/iored...仅仅支持以下参数,并不支持url参数

var redisOnPort6380 = new Redis(6380);var anotherRedis = new Redis(6380, ’192.168.100.1’);var unixSocketRedis = new Redis({ path: ’/tmp/echo.sock’ });var unixSocketRedis2 = new Redis(’/tmp/echo.sock’);var urlRedis = new Redis(’redis://user:password@redis-service.com:6379/’);var urlRedis2 = new Redis(’//localhost:6379’);var authedRedis = new Redis(6380, ’192.168.100.1’, { password: ’password’ });

就是不知道为什么连接出错不会报错???一直重试到,直接内存爆炸

相关文章: