搜尋

首頁  >  問答  >  主體

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

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
黄舟黄舟2787 天前717

全部回覆(2)我來回復

  • 高洛峰

    高洛峰2017-04-17 16:20:12

    運行容器時候的指令、連接埠是怎麼映射的。

    回覆
    0
  • PHPz

    PHPz2017-04-17 16:20:12

    找到原因了

    1. 這裡變動了hostname要另外注入 https://github.com/mcollina/m...
      server.jsserver.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. 更正

      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' });

    2. persistance的建構方法
    lib/persistence/redis.js

    rrreee

    這是一個ioredis的API,參考 https://github.com/luin/iored...🎜僅支援以下參數,並不支援url參數🎜🎜🎜 rrreee 🎜🎜🎜就是不知道為什麼連線出錯不會報錯? ? ?一直重試到,直接記憶體爆炸🎜🎜。 🎜

    回覆
    0
  • 取消回覆