node.js - Docker 容器里无法连接其他容器运行的 Redis
黄舟
黄舟 2017-04-17 16:18:12
0
2
749

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 mysql
PING mysql (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.101 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.097 ms
64 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 loss
round-trip min/avg/max = 0.097/0.099/0.101 ms
/ # ping redis
PING redis (172.19.0.3): 56 data bytes
64 bytes from 172.19.0.3: seq=0 ttl=64 time=0.069 ms
64 bytes from 172.19.0.3: seq=1 ttl=64 time=0.134 ms
64 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 loss
round-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
黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

Antworte allen(2)
小葫芦

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

PHPzhong

找到原因了

  1. 这里变动了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'
}
  1. 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);
};
  1. 这是一个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' });

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

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage