Redis学習(一):redisクラスタの哨兵モードでの負荷均衡
29157 ワード
説明
Redisクラスタの導入を学習研究する過程で、哨兵モードでクラスタを配置する場合、Jedisをクライアントとしてホストに接続するだけで、スレーブからバックアップとしてのみ高可用性を保証できることが分かった.このように読み書きはホストにあり,比較的高い読み取りでホストに大きなプレッシャーを与える.JedisSentinelPoolのソースコードを読むことで、このクラスに基づいてJedisSentinelMasterSlavePoolクラスを実現し、このクラスによってredis哨兵モードでの読み取り操作負荷の均衡を実現する.
本文
基礎知識
redisクラスタの基礎知識については、ここではまとめずに、以下の資料を見て学習と構築を行うことができる:Redisシリーズを深く分析する(一)-Redis入門の紹介と主従構築からRedisシリーズを深く分析する(二)-Redis哨兵モードと高利用可能なクラスタを深く分析するRedisシリーズ(三)-Redisクラスタモード構築と原理を詳しく理解Redisシリーズを深く分析する(四)-Redisデータ構造とグローバルコマンドの概要Redisシリーズを深く分析する(五)-Redisデータ構造の文字列深く分析Redisシリーズ(六)-Redisデータ構造のハッシュ深く分析Redisシリーズ(七)-Redisデータ構造のリスト深く分析Redisシリーズ(八)-Redisデータ構造の集合
哨兵モードでは、一般的に3つのノードを哨兵クラスタとして配置し、哨兵の高可用性を保証する.同時に、各masterノードは主従レプリケーションのモードを採用し、masterで書き込み、slaveノードにデータを同期します.ここでの哨兵はmasterノードの状態を監視し,masterノードが正常に動作しない場合にslaveノードから新しいmasterノードを自動的にフェイルオーバして選択し,古いmasterノードが正常に戻った後,新しいslaveノードとしてクラスタに再加入できることを保証するために用いられる.
JedisSentinelPool
このクラスはJedisがredis哨兵モードをサポートする接続プールで、このクラスにGenericObjectPoolオブジェクトがあり、このクラスを初期化するとホスト接続プールが作成され、MasterListenerが作成され、フェイルオーバが発生するとホスト接続プールが再初期化されます.MasterListenerリスナーでは主に+switch-masterチャネルの傍受を購読しており、ホスト切替が発生した場合、哨兵はこのチャネルを通じて同名のイベントを送信し、このイベントJedisSentinelPoolを傍受することで接続プールの再初期化を実現する.
詳細は公式ドキュメントを参照this.j.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) {
JedisSentinelPool.this.log.debug("Sentinel {}:{} published: {}.", new Object[]{MasterListener.this.host, MasterListener.this.port, message});
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
if (MasterListener.this.masterName.equals(switchMasterMsg[0])) {
JedisSentinelPool.this.initPool(JedisSentinelPool.this.toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
} else {
JedisSentinelPool.this.log.debug("Ignoring message on +switch-master for master name {}, our master name is {}", switchMasterMsg[0], MasterListener.this.masterName);
}
} else {
JedisSentinelPool.this.log.error("Invalid message received on Sentinel {}:{} on channel +switch-master: {}", new Object[]{MasterListener.this.host, MasterListener.this.port, message});
}
}
}, new String[]{"+switch-master"});
以上の特性に基づいて、JedisSentinelPoolを書き換えることにより、ホスト接続プールがある場合、スレーブ接続プールを携帯し、リスナーを書き換えて他のイベントをリスニングし、スレーブ変化時にスレーブ接続プールが変化に追従できるようにする.
JedisSentinelMasterSlavePool
このクラスはJedisSentinelPoolクラスに基づいて実現される.スレーブ接続プールアドレスリストslavesAddrを追加し、スレーブ接続プール集合Map>slavePoolsからMasterListenerを書き換え、リスニング+slave,+sdown,-sdownイベントを増加し、ホスト切替が発生し、スレーブダウン時に自動的に接続プールを変更できるようにした.同時にThreadLocal>objectPoolThreadLocal変数を追加し、主にリソースを返却できるようにするため、ホストを介してjedisオブジェクトを取得する際にDataSourceを設定し、閉じるとjedisはこの変数を介して接続プールオブジェクトを取得し、接続プールpoolはreturnResource()メソッドを使用してリソースを返します.この特性に基づいて、ThreadLocal変数を追加し、slaveからリソースを取得するときに、そのスレッドがマシン接続プールから取得したJedisを保存し、リソースを返却するときに対応する接続プールを見つけることができます.
このようにしてスレーブ上でのリード動作の負荷等化を実現し,クラスタ状態が変化すると接続プールも変化し,接続が得られない場合があり,フォールトトレランス処理が必要となる.
ここで,マシン接続の取得にはランダムアルゴリズムを用い,他のアルゴリズムを用いてもよい.
新規変数private volatile Map> slavePools;
private volatile List slavesAddr;
private final Object changeSlavePoolLock;
private final ThreadLocal> objectPoolThreadLocal = new ThreadLocal<>();
private volatile JedisFactory2 factory;
JedisFactoryはパッケージ内のリソースに属するため、JedisFactory 2を新規作成します.
ランダムアルゴリズムによるスレーブ接続の取得public Jedis getSlaveResource() {
try{
if (this.slavePools != null && this.slavePools.size() > 0) {
Random random = new Random();
HostAndPort slaveHP = this.slavesAddr.get(random.nextInt(slavePools.size()));
GenericObjectPool pool = this.slavePools.get(slaveHP);
this.log.info("Get a slave pool, the address is {}", slaveHP);
objectPoolThreadLocal.set(pool);
return pool.borrowObject();
}
}catch(Exception e){
this.log.debug("Could not get a resource form slave pools");
}
return this.getResource();
}
スレーブ接続プールリソースの返却public void closeSlaveJedis(Jedis jedis) {
GenericObjectPool pool = objectPoolThreadLocal.get();
//
if (pool != null) {
pool.returnObject(jedis);
} else {
jedis.close();
}
}
Listenerの変更、その他のListenerイベントの追加// , 。 ( , ),
//
this.j.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) {
JedisSentinelMasterSlavePool.this.log.debug("Sentinel {}:{}, channel is {} == published: {}.", new Object[]{JedisSentinelMasterSlavePool.MasterListener.this.host, JedisSentinelMasterSlavePool.MasterListener.this.port, channel, message});
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3 && channel.equals("+switch-master")) {
if (JedisSentinelMasterSlavePool.MasterListener.this.masterName.equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +switch-master for master name {}, the new master address is {} : {}", switchMasterMsg[0], switchMasterMsg[3], switchMasterMsg[4]);
JedisSentinelMasterSlavePool.this.initPool(JedisSentinelMasterSlavePool.this.toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
}
}
if (switchMasterMsg.length > 5 && JedisSentinelMasterSlavePool.MasterListener.this.masterName.equals(switchMasterMsg[5])) {
if (channel.equals("+slave")) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +slave for master name {}, the new slave address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.addSlavePool(switchMasterMsg);
}
if (channel.equals("+sdown")) {
if ("slave".equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +sdown for master name {}, the slave is now in Subjectively Down state, remove the slave, the address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.removeSlavePool(switchMasterMsg);
}
}
if (channel.equals("-sdown")) {
if ("slave".equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on -sdown for master name {}, the slave is no logger in Subjectively Down state, readd the slave, the address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.addSlavePool(switchMasterMsg);
}
}
}
}
}, new String[]{"+switch-master", "+slave", "+sdown", "-sdown"});
ソースコード
public class JedisSentinelMasterSlavePool extends JedisPoolAbstract {
protected GenericObjectPoolConfig poolConfig;
protected int connectionTimeout;
protected int soTimeout;
protected String password;
protected int database;
protected String clientName;
protected Set masterListeners;
protected Logger log;
private volatile HostAndPort currentHostMaster;
private volatile JedisFactory2 factory;
private volatile Map> slavePools;
private volatile List slavesAddr;
private final Set sentinels;
private final Object initPoolLock;
private final Object changeSlavePoolLock;
private final ThreadLocal> objectPoolThreadLocal = new ThreadLocal<>();
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig) {
this(masterName, sentinels, poolConfig, 2000, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels) {
this(masterName, sentinels, new GenericObjectPoolConfig(), 2000, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, String password) {
this(masterName, sentinels, new GenericObjectPoolConfig(), 2000, password);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password) {
this(masterName, sentinels, poolConfig, timeout, password, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout) {
this(masterName, sentinels, poolConfig, timeout, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, String password) {
this(masterName, sentinels, poolConfig, 2000, password);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password, int database) {
this(masterName, sentinels, poolConfig, timeout, timeout, password, database);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password, int database, String clientName) {
this(masterName, sentinels, poolConfig, timeout, timeout, password, database, clientName);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, int soTimeout, String password, int database) {
this(masterName, sentinels, poolConfig, timeout, soTimeout, password, database, (String)null);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, int database, String clientName) {
this.connectionTimeout = 2000;
this.soTimeout = 2000;
this.database = 0;
this.masterListeners = new HashSet();
this.log = LoggerFactory.getLogger(JedisSentinelMasterSlavePool.class);
this.initPoolLock = new Object();
this.changeSlavePoolLock = new Object();
this.poolConfig = poolConfig;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.password = password;
this.database = database;
this.clientName = clientName;
this.sentinels = sentinels;
HostAndPort master = this.initSentinels(sentinels, masterName);
this.initPool(master, this.slavesAddr);
}
public void destroy() {
Iterator var1 = this.masterListeners.iterator();
while(var1.hasNext()) {
JedisSentinelMasterSlavePool.MasterListener m = (JedisSentinelMasterSlavePool.MasterListener)var1.next();
m.shutdown();
}
super.destroy();
//
this.destroySlavePool(this.slavePools);
}
private void destroySlavePool(Map> slavePools) {
for (GenericObjectPool pool : slavePools.values()) {
pool.close();
}
}
public HostAndPort getCurrentHostMaster() {
return this.currentHostMaster;
}
private void initPool(HostAndPort master) {
Object var2 = this.initPoolLock;
synchronized(this.initPoolLock) {
if (!master.equals(this.currentHostMaster)) {
this.currentHostMaster = master;
if (this.factory == null) {
this.factory = new JedisFactory2(master.getHost(), master.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName);
this.initPool(this.poolConfig, this.factory);
} else {
this.factory.setHostAndPort(this.currentHostMaster);
this.internalPool.clear();
}
this.log.info("Rcreated JedisPool to master at " + master);
}
}
}
// initPool
private void initPool(HostAndPort master, List slaves) {
Object var2 = this.initPoolLock;
synchronized(this.initPoolLock) {
if (!master.equals(this.currentHostMaster)) {
this.currentHostMaster = master;
this.slavesAddr = slaves;
if (this.factory == null) {
this.factory = new JedisFactory2(master.getHost(), master.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName);
this.initPool(this.poolConfig, this.factory);
//
this.initSlavePool(slaves);
} else {
this.factory.setHostAndPort(this.currentHostMaster);
this.internalPool.clear();
}
this.log.info("Created JedisPool to master at " + master);
}
}
}
//
private void initSlavePool(List slaves) {
Map> slavePools = new HashMap<>();
for (HostAndPort slave : slaves) {
GenericObjectPool slavePool = new GenericObjectPool(new JedisFactory2(slave.getHost(), slave.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName), this.poolConfig);
this.log.info("Found Redis slave at {}, created a Slave JedisPool", slave);
slavePools.put(slave, slavePool);
}
this.slavePools = slavePools;
}
private HostAndPort initSentinels(Set sentinels, String masterName) {
HostAndPort master = null;
boolean sentinelAvailable = false;
this.log.info("Trying to find master from available Sentinels...");
Iterator var5 = sentinels.iterator();
String sentinel;
HostAndPort hap;
while(var5.hasNext()) {
sentinel = (String)var5.next();
hap = HostAndPort.parseString(sentinel);
this.log.debug("Connecting to Sentinel {}", hap);
Jedis jedis = null;
try {
jedis = new Jedis(hap);
List masterAddr = jedis.sentinelGetMasterAddrByName(masterName);
sentinelAvailable = true;
//
List
ソースアドレス:https://github.com/Edenwds/redis_study/tree/master/sentinelslaveredis
基礎知識
redisクラスタの基礎知識については、ここではまとめずに、以下の資料を見て学習と構築を行うことができる:Redisシリーズを深く分析する(一)-Redis入門の紹介と主従構築からRedisシリーズを深く分析する(二)-Redis哨兵モードと高利用可能なクラスタを深く分析するRedisシリーズ(三)-Redisクラスタモード構築と原理を詳しく理解Redisシリーズを深く分析する(四)-Redisデータ構造とグローバルコマンドの概要Redisシリーズを深く分析する(五)-Redisデータ構造の文字列深く分析Redisシリーズ(六)-Redisデータ構造のハッシュ深く分析Redisシリーズ(七)-Redisデータ構造のリスト深く分析Redisシリーズ(八)-Redisデータ構造の集合
哨兵モードでは、一般的に3つのノードを哨兵クラスタとして配置し、哨兵の高可用性を保証する.同時に、各masterノードは主従レプリケーションのモードを採用し、masterで書き込み、slaveノードにデータを同期します.ここでの哨兵はmasterノードの状態を監視し,masterノードが正常に動作しない場合にslaveノードから新しいmasterノードを自動的にフェイルオーバして選択し,古いmasterノードが正常に戻った後,新しいslaveノードとしてクラスタに再加入できることを保証するために用いられる.
JedisSentinelPool
このクラスはJedisがredis哨兵モードをサポートする接続プールで、このクラスにGenericObjectPoolオブジェクトがあり、このクラスを初期化するとホスト接続プールが作成され、MasterListenerが作成され、フェイルオーバが発生するとホスト接続プールが再初期化されます.MasterListenerリスナーでは主に+switch-masterチャネルの傍受を購読しており、ホスト切替が発生した場合、哨兵はこのチャネルを通じて同名のイベントを送信し、このイベントJedisSentinelPoolを傍受することで接続プールの再初期化を実現する.
詳細は公式ドキュメントを参照
this.j.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) {
JedisSentinelPool.this.log.debug("Sentinel {}:{} published: {}.", new Object[]{MasterListener.this.host, MasterListener.this.port, message});
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
if (MasterListener.this.masterName.equals(switchMasterMsg[0])) {
JedisSentinelPool.this.initPool(JedisSentinelPool.this.toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
} else {
JedisSentinelPool.this.log.debug("Ignoring message on +switch-master for master name {}, our master name is {}", switchMasterMsg[0], MasterListener.this.masterName);
}
} else {
JedisSentinelPool.this.log.error("Invalid message received on Sentinel {}:{} on channel +switch-master: {}", new Object[]{MasterListener.this.host, MasterListener.this.port, message});
}
}
}, new String[]{"+switch-master"});
以上の特性に基づいて、JedisSentinelPoolを書き換えることにより、ホスト接続プールがある場合、スレーブ接続プールを携帯し、リスナーを書き換えて他のイベントをリスニングし、スレーブ変化時にスレーブ接続プールが変化に追従できるようにする.
JedisSentinelMasterSlavePool
このクラスはJedisSentinelPoolクラスに基づいて実現される.スレーブ接続プールアドレスリストslavesAddrを追加し、スレーブ接続プール集合Map>slavePoolsからMasterListenerを書き換え、リスニング+slave,+sdown,-sdownイベントを増加し、ホスト切替が発生し、スレーブダウン時に自動的に接続プールを変更できるようにした.同時にThreadLocal>objectPoolThreadLocal変数を追加し、主にリソースを返却できるようにするため、ホストを介してjedisオブジェクトを取得する際にDataSourceを設定し、閉じるとjedisはこの変数を介して接続プールオブジェクトを取得し、接続プールpoolはreturnResource()メソッドを使用してリソースを返します.この特性に基づいて、ThreadLocal変数を追加し、slaveからリソースを取得するときに、そのスレッドがマシン接続プールから取得したJedisを保存し、リソースを返却するときに対応する接続プールを見つけることができます.
このようにしてスレーブ上でのリード動作の負荷等化を実現し,クラスタ状態が変化すると接続プールも変化し,接続が得られない場合があり,フォールトトレランス処理が必要となる.
ここで,マシン接続の取得にはランダムアルゴリズムを用い,他のアルゴリズムを用いてもよい.
新規変数
private volatile Map> slavePools;
private volatile List slavesAddr;
private final Object changeSlavePoolLock;
private final ThreadLocal> objectPoolThreadLocal = new ThreadLocal<>();
private volatile JedisFactory2 factory;
JedisFactoryはパッケージ内のリソースに属するため、JedisFactory 2を新規作成します.
ランダムアルゴリズムによるスレーブ接続の取得
public Jedis getSlaveResource() {
try{
if (this.slavePools != null && this.slavePools.size() > 0) {
Random random = new Random();
HostAndPort slaveHP = this.slavesAddr.get(random.nextInt(slavePools.size()));
GenericObjectPool pool = this.slavePools.get(slaveHP);
this.log.info("Get a slave pool, the address is {}", slaveHP);
objectPoolThreadLocal.set(pool);
return pool.borrowObject();
}
}catch(Exception e){
this.log.debug("Could not get a resource form slave pools");
}
return this.getResource();
}
スレーブ接続プールリソースの返却
public void closeSlaveJedis(Jedis jedis) {
GenericObjectPool pool = objectPoolThreadLocal.get();
//
if (pool != null) {
pool.returnObject(jedis);
} else {
jedis.close();
}
}
Listenerの変更、その他のListenerイベントの追加
// , 。 ( , ),
//
this.j.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) {
JedisSentinelMasterSlavePool.this.log.debug("Sentinel {}:{}, channel is {} == published: {}.", new Object[]{JedisSentinelMasterSlavePool.MasterListener.this.host, JedisSentinelMasterSlavePool.MasterListener.this.port, channel, message});
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3 && channel.equals("+switch-master")) {
if (JedisSentinelMasterSlavePool.MasterListener.this.masterName.equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +switch-master for master name {}, the new master address is {} : {}", switchMasterMsg[0], switchMasterMsg[3], switchMasterMsg[4]);
JedisSentinelMasterSlavePool.this.initPool(JedisSentinelMasterSlavePool.this.toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
}
}
if (switchMasterMsg.length > 5 && JedisSentinelMasterSlavePool.MasterListener.this.masterName.equals(switchMasterMsg[5])) {
if (channel.equals("+slave")) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +slave for master name {}, the new slave address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.addSlavePool(switchMasterMsg);
}
if (channel.equals("+sdown")) {
if ("slave".equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on +sdown for master name {}, the slave is now in Subjectively Down state, remove the slave, the address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.removeSlavePool(switchMasterMsg);
}
}
if (channel.equals("-sdown")) {
if ("slave".equals(switchMasterMsg[0])) {
JedisSentinelMasterSlavePool.this.log.debug("Listening messgae on -sdown for master name {}, the slave is no logger in Subjectively Down state, readd the slave, the address is {} : {}", switchMasterMsg[5], switchMasterMsg[2], switchMasterMsg[3]);
JedisSentinelMasterSlavePool.this.addSlavePool(switchMasterMsg);
}
}
}
}
}, new String[]{"+switch-master", "+slave", "+sdown", "-sdown"});
ソースコード
public class JedisSentinelMasterSlavePool extends JedisPoolAbstract {
protected GenericObjectPoolConfig poolConfig;
protected int connectionTimeout;
protected int soTimeout;
protected String password;
protected int database;
protected String clientName;
protected Set masterListeners;
protected Logger log;
private volatile HostAndPort currentHostMaster;
private volatile JedisFactory2 factory;
private volatile Map> slavePools;
private volatile List slavesAddr;
private final Set sentinels;
private final Object initPoolLock;
private final Object changeSlavePoolLock;
private final ThreadLocal> objectPoolThreadLocal = new ThreadLocal<>();
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig) {
this(masterName, sentinels, poolConfig, 2000, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels) {
this(masterName, sentinels, new GenericObjectPoolConfig(), 2000, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, String password) {
this(masterName, sentinels, new GenericObjectPoolConfig(), 2000, password);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password) {
this(masterName, sentinels, poolConfig, timeout, password, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout) {
this(masterName, sentinels, poolConfig, timeout, (String)null, 0);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, String password) {
this(masterName, sentinels, poolConfig, 2000, password);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password, int database) {
this(masterName, sentinels, poolConfig, timeout, timeout, password, database);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, String password, int database, String clientName) {
this(masterName, sentinels, poolConfig, timeout, timeout, password, database, clientName);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int timeout, int soTimeout, String password, int database) {
this(masterName, sentinels, poolConfig, timeout, soTimeout, password, database, (String)null);
}
public JedisSentinelMasterSlavePool(String masterName, Set sentinels, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, int database, String clientName) {
this.connectionTimeout = 2000;
this.soTimeout = 2000;
this.database = 0;
this.masterListeners = new HashSet();
this.log = LoggerFactory.getLogger(JedisSentinelMasterSlavePool.class);
this.initPoolLock = new Object();
this.changeSlavePoolLock = new Object();
this.poolConfig = poolConfig;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.password = password;
this.database = database;
this.clientName = clientName;
this.sentinels = sentinels;
HostAndPort master = this.initSentinels(sentinels, masterName);
this.initPool(master, this.slavesAddr);
}
public void destroy() {
Iterator var1 = this.masterListeners.iterator();
while(var1.hasNext()) {
JedisSentinelMasterSlavePool.MasterListener m = (JedisSentinelMasterSlavePool.MasterListener)var1.next();
m.shutdown();
}
super.destroy();
//
this.destroySlavePool(this.slavePools);
}
private void destroySlavePool(Map> slavePools) {
for (GenericObjectPool pool : slavePools.values()) {
pool.close();
}
}
public HostAndPort getCurrentHostMaster() {
return this.currentHostMaster;
}
private void initPool(HostAndPort master) {
Object var2 = this.initPoolLock;
synchronized(this.initPoolLock) {
if (!master.equals(this.currentHostMaster)) {
this.currentHostMaster = master;
if (this.factory == null) {
this.factory = new JedisFactory2(master.getHost(), master.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName);
this.initPool(this.poolConfig, this.factory);
} else {
this.factory.setHostAndPort(this.currentHostMaster);
this.internalPool.clear();
}
this.log.info("Rcreated JedisPool to master at " + master);
}
}
}
// initPool
private void initPool(HostAndPort master, List slaves) {
Object var2 = this.initPoolLock;
synchronized(this.initPoolLock) {
if (!master.equals(this.currentHostMaster)) {
this.currentHostMaster = master;
this.slavesAddr = slaves;
if (this.factory == null) {
this.factory = new JedisFactory2(master.getHost(), master.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName);
this.initPool(this.poolConfig, this.factory);
//
this.initSlavePool(slaves);
} else {
this.factory.setHostAndPort(this.currentHostMaster);
this.internalPool.clear();
}
this.log.info("Created JedisPool to master at " + master);
}
}
}
//
private void initSlavePool(List slaves) {
Map> slavePools = new HashMap<>();
for (HostAndPort slave : slaves) {
GenericObjectPool slavePool = new GenericObjectPool(new JedisFactory2(slave.getHost(), slave.getPort(), this.connectionTimeout, this.soTimeout, this.password, this.database, this.clientName), this.poolConfig);
this.log.info("Found Redis slave at {}, created a Slave JedisPool", slave);
slavePools.put(slave, slavePool);
}
this.slavePools = slavePools;
}
private HostAndPort initSentinels(Set sentinels, String masterName) {
HostAndPort master = null;
boolean sentinelAvailable = false;
this.log.info("Trying to find master from available Sentinels...");
Iterator var5 = sentinels.iterator();
String sentinel;
HostAndPort hap;
while(var5.hasNext()) {
sentinel = (String)var5.next();
hap = HostAndPort.parseString(sentinel);
this.log.debug("Connecting to Sentinel {}", hap);
Jedis jedis = null;
try {
jedis = new Jedis(hap);
List masterAddr = jedis.sentinelGetMasterAddrByName(masterName);
sentinelAvailable = true;
//
List
ソースアドレス:https://github.com/Edenwds/redis_study/tree/master/sentinelslaveredis