Java嘗試鏈接Sentinel時,拋出Could not get a resource from the pool異常

問題描述

前幾天興致沖沖的學習了哨兵模式,而且可以在阿里雲上本身實現了redis-sentinel的一些功能,今天嘗試去用java客戶端去鏈接sentinel時發生了一系列意外,啓動時便拋出了Could not get a resource from the pool這個異常。無奈去網上搜了一下,無非是bind端口沒打開或是防火牆配置問題等,都跟本身的狀況不同。

廢話少說,先上代碼:java

RedisUtil

package com.lamarsan.sentinel.util;

import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/** * className: RedisUtil * description: TODO * * @author hasee * @version 1.0 * @date 2018/12/26 10:57 */
public class RedisUtil {
    private static final Logger myLogger = Logger.getLogger("com.lamarsan.sentinel.util");

    private static JedisSentinelPool pool = null;

    static {
        try {
            Set<String> sentinels = new HashSet<String>();
            sentinels.add("你的ip:26380");
            sentinels.add("你的ip.140:26379");
            sentinels.add("你的ip:26381");
            String masterName = "mymaster";
            String password = "你的密碼";
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMinIdle(8);
            config.setMaxTotal(100);
            config.setMaxIdle(100);
            config.setMaxWaitMillis(10000);
            pool = new JedisSentinelPool(masterName, sentinels, config, password);
            try {
                pool.getResource();
            } catch (JedisConnectionException e) {
                myLogger.info(e.getMessage());
                e.printStackTrace();
            }
        } catch (Exception e) {
            myLogger.info(e.getMessage());
            e.printStackTrace();
        }
    }

    private static void returnResource(JedisSentinelPool pool, Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /** * <p>經過key獲取儲存在redis中的value</p> * <p>並釋放鏈接</p> * * @param key * @return 成功返回value 失敗返回null */
    public static String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = pool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            if (jedis != null) {
                jedis.close();
            }
            e.printStackTrace();
        } finally {
            returnResource(pool, jedis);
        }
        return value;
    }

    /** * <p>向redis存入key和value,並釋放鏈接資源</p> * <p>若是key已經存在 則覆蓋</p> * * @param key * @param value * @return 成功 返回OK 失敗返回 0 */
    public static String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            if (jedis != null) {
                jedis.close();
            }
            e.printStackTrace();
            return "0";
        } finally {
            returnResource(pool, jedis);
        }
    }
    
    .....
}

複製代碼

main

package com.lamarsan.sentinel;

import com.lamarsan.sentinel.util.RedisUtil;

/** * className: Test * description: TODO * * @author hasee * @version 1.0 * @date 2019/9/13 15:54 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        RedisUtil.setnx("hello", "world");
        while (true) {
            Thread.sleep(10000);
            String result = RedisUtil.get("hello");
            System.out.println(result);
        }
    }
}
複製代碼

解決思路

從代碼角度來看,並無什麼配置是有問題的,並且服務器上的配置也沒問題,但就是鏈接時在pool.getResource();拋出鏈接超時的異常,正當我抓耳撓腮中,索性斷個點看看吧!pool的屬性:currentHostMaster的ip竟然是127.0.0.1!!!!redis

彷佛有了必定的頭緒,讓咱們跟進JedisSentinelPool的源碼看看:

public JedisSentinelPool(String masterName, Set<String> sentinels, final GenericObjectPoolConfig poolConfig, final String password) {
    this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password);
}
複製代碼

連續點擊幾回this,這都是JedisSentinelPool的構造方法,會進入以下的構造方法:服務器

public JedisSentinelPool(String masterName, Set<String> sentinels, final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName) {
    this.poolConfig = poolConfig;
    this.connectionTimeout = connectionTimeout;
    this.soTimeout = soTimeout;
    this.password = password;
    this.database = database;
    this.clientName = clientName;

    HostAndPort master = initSentinels(sentinels, masterName);
    initPool(master);
  }
複製代碼

經過斷點能夠發現master的host屬性爲127.0.0.1。咱們進入initSentinels方法一看究竟:app

private HostAndPort initSentinels(Set<String> sentinels, final String masterName) {

    HostAndPort master = null;
    boolean sentinelAvailable = false;

    log.info("Trying to find master from available Sentinels...");

    for (String sentinel : sentinels) {
      final HostAndPort hap = HostAndPort.parseString(sentinel);

      log.fine("Connecting to Sentinel " + hap);

      Jedis jedis = null;
      try {
        jedis = new Jedis(hap.getHost(), hap.getPort());

        List<String> masterAddr = jedis.sentinelGetMasterAddrByName(masterName);

        // connected to sentinel...
        sentinelAvailable = true;

        if (masterAddr == null || masterAddr.size() != 2) {
          log.warning("Can not get master addr, master name: " + masterName + ". Sentinel: " + hap
              + ".");
          continue;
        }

        master = toHostAndPort(masterAddr);
        log.fine("Found Redis master at " + master);
        break;
      } catch (JedisException e) {
        // resolves #1036, it should handle JedisException there's another chance
        // of raising JedisDataException
        log.warning("Cannot get master address from sentinel running @ " + hap + ". Reason: " + e
            + ". Trying next one.");
      } finally {
        if (jedis != null) {
          jedis.close();
        }
      }
    }
複製代碼

觀察能夠發現,master是與masterAddr變量相關,並且經過斷點能夠得masterAddr的ip爲127.0.0.1。socket

masterAddr是經過sentinelGetMasterAddrByName方法賦值的,讓咱們進入這個方法:

public List<String> sentinelGetMasterAddrByName(String masterName) {
    client.sentinel(Protocol.SENTINEL_GET_MASTER_ADDR_BY_NAME, masterName);
    final List<Object> reply = client.getObjectMultiBulkReply();
    return BuilderFactory.STRING_LIST.build(reply);
}
複製代碼

進入sentinel方法:ide

public void sentinel(final String... args) {
    final byte[][] arg = new byte[args.length][];
    for (int i = 0; i < arg.length; i++) {
      arg[i] = SafeEncoder.encode(args[i]);
    }
    sentinel(arg);
}
複製代碼

繼續進入sentinel方法:學習

public void sentinel(final byte[]... args) {
    sendCommand(SENTINEL, args);
}
複製代碼

能夠發現,它是發送了一些命令,但是發送命令到哪裏呢?讓咱們繼續進入sendCommand方法:ui

protected Connection sendCommand(final Command cmd, final byte[]... args) {
    try {
      connect();
      Protocol.sendCommand(outputStream, cmd, args);
      pipelinedCommands++;
      return this;
    } catch (JedisConnectionException ex) {
      /* * When client send request which formed by invalid protocol, Redis send back error message * before close connection. We try to read it to provide reason of failure. */
      try {
        String errorMessage = Protocol.readErrorLineIfPossible(inputStream);
        if (errorMessage != null && errorMessage.length() > 0) {
          ex = new JedisConnectionException(errorMessage, ex.getCause());
        }
      } catch (Exception e) {
        /* * Catch any IOException or JedisConnectionException occurred from InputStream#read and just * ignore. This approach is safe because reading error message is optional and connection * will eventually be closed. */
      }
      // Any other exceptions related to connection?
      broken = true;
      throw ex;
    }
}
複製代碼

出現了,connect();~~~~,真相就在眼前,讓咱們進入它!!this

public void connect() {
    if (!isConnected()) {
      try {
        socket = new Socket();
        // ->@wjw_add
        socket.setReuseAddress(true);
        socket.setKeepAlive(true); // Will monitor the TCP connection is
        // valid
        socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
        // ensure timely delivery of data
        socket.setSoLinger(true, 0); // Control calls close () method,
        // the underlying socket is closed
        // immediately
        // <-@wjw_add

        socket.connect(new InetSocketAddress(host, port), connectionTimeout);
        socket.setSoTimeout(soTimeout);

        if (ssl) {
          if (null == sslSocketFactory) {
            sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
          }
          socket = (SSLSocket) sslSocketFactory.createSocket(socket, host, port, true);
          if (null != sslParameters) {
            ((SSLSocket) socket).setSSLParameters(sslParameters);
          }
          if ((null != hostnameVerifier) &&
              (!hostnameVerifier.verify(host, ((SSLSocket) socket).getSession()))) {
            String message = String.format(
                "The connection to '%s' failed ssl/tls hostname verification.", host);
            throw new JedisConnectionException(message);
          }
        }

        outputStream = new RedisOutputStream(socket.getOutputStream());
        inputStream = new RedisInputStream(socket.getInputStream());
      } catch (IOException ex) {
        broken = true;
        throw new JedisConnectionException(ex);
      }
    }
}
複製代碼

真相大白!!!原來Jedis的底層是經過Socket鏈接的~接下來就很容易猜到,其經過Socket獲取到了服務器的sentinel配置,將其的ip和port填入了masterAddr中,那麼咱們只須要檢查服務器的sentinel配置文件便可。而後我發現了這一句:
sentinel monitor mymaster 127.0.0.1 6380 2
啊,原來是這裏!!!在將127.0.0.1的ip改爲阿里雲的公網ip並重啓服務redis-sentinel服務後,java就能正常對redis進行操做了~~~~阿里雲

總結

當遇到問題時,若是直接搜索不能找到答案,那麼嘗試閱讀源碼不失爲一種好選擇。

相關文章
相關標籤/搜索