├── .gitignore ├── README.md ├── architecture ├── MasterSlaveSetinelPool.jpg ├── MasterSlaveSetinelPool.vsdx ├── ShardedMasterSlaveSetinelPool.jpg └── ShardedMasterSlaveSetinelPool.vsdx ├── build.gradle ├── gradle.properties └── src ├── main └── java │ └── com │ └── penglecode │ └── common │ └── redis │ └── jedis │ ├── JedisCallback.java │ ├── JedisTemplate.java │ └── ms │ ├── MasterSlaveHostAndPort.java │ ├── MasterSlaveJedis.java │ ├── MasterSlaveJedisSentinelPool.java │ ├── MasterSlaveJedisShardInfo.java │ ├── ShardedMasterSlaveJedis.java │ └── ShardedMasterSlaveJedisSentinelPool.java └── test ├── java └── com │ └── penglecode │ └── common │ └── redis │ └── jedis │ └── test │ ├── AbstractJedisExample.java │ └── example │ ├── JedisSentinelExample.java │ ├── MasterSlaveJedisSentinelPoolExample.java │ └── ShardedMasterSlaveJedisSentinelPoolExample.java └── resources └── logback.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /build 3 | /.classpath 4 | /.project 5 | /.settings 6 | /.gradle -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jedis-ms-sentinel 2 | ================= 3 | 4 | This is a Redis Master-Slave system architecture based jedis client. 5 | It can provide master-slave redundancy、failover by redis-sentinel、sharding and so on. 6 | 7 | 1、Master-Slaves no sharding 8 | 9 | 10 | ![image](https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/master/architecture/MasterSlaveSetinelPool.jpg) 11 | 12 | Set sentinels = new LinkedHashSet(); 13 | sentinels.add("192.168.137.101:63791"); 14 | sentinels.add("192.168.137.101:63792"); 15 | Pool masterSlaveJedisPool = new MasterSlaveJedisSentinelPool("master-1", sentinels,jedisPoolConfig); 16 | 17 | MasterSlaveJedis masterSlaveJedis = masterSlaveJedisPool.getResource(); 18 | //>>> masterSlaveJedis = MasterSlaveJedis {master=192.168.137.101:6379, slaves=[192.168.137.101:6380, 192.168.137.101:6381]} 19 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis); 20 | 21 | masterSlaveJedis.set("nowTime", "2015-03-16 15:34:55"); // The underlying actually call the master.set("nowTime", "2015-03-16 15:34:55"); 22 | 23 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200)); 24 | 25 | String slaveHolder1 = "myslave1"; 26 | Jedis slave1 = masterSlaveJedis.opsForSlave(slaveHolder); // if no any slave found, opsForSlave() will return master as a slave to be use 27 | System.out.println(">>> nowTime = " + slave1.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55 28 | 29 | String slaveHolder2 = "myslave1"; 30 | Jedis slave2 = masterSlaveJedis.opsForSlave(slaveHolder); 31 | System.out.println(">>> nowTime = " + slave2.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55 32 | 33 | System.out.println(slave1.equals(slave2)); // must be true if slaveHolder1 equals slaveHolder2 34 | 35 | masterSlaveJedisPool.returnResource(masterSlaveJedis); 36 | 37 | 2、Master-Slaves with sharding 38 | 39 | ![image](https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/master/architecture/ShardedMasterSlaveSetinelPool.jpg) 40 | 41 | master-1 : master=192.168.137.101:6379 slaves=[192.168.137.101:6380, 192.168.137.101:6381] 42 | master-2 : master=192.168.137.101:6382 slaves=[192.168.137.101:6383, 192.168.137.101:6384] 43 | 44 | Set masterNames = new LinkedHashSet(); 45 | masterNames.add("master-1"); 46 | masterNames.add("master-2"); 47 | Set sentinels = new LinkedHashSet(); 48 | sentinels.add("192.168.137.101:63791"); 49 | sentinels.add("192.168.137.101:63792"); 50 | Pool shardedMasterSlaveJedisPool = new ShardedMasterSlaveJedisSentinelPool(masterNames, sentinels,jedisPoolConfig); 51 | 52 | ShardedMasterSlaveJedis shardedMasterSlaveJedis = shardedMasterSlaveJedisPool.getResource(); 53 | for(int i = 0; i < 10; i++){ 54 | String key = "shard-" + i; 55 | shardedMasterSlaveJedis.set(key, String.valueOf(i)); 56 | // sharded in master-1[192.168.137.101:6379] for keys : shard-0, shard-2, shard-6, shard-8, shard-9, and sharded in master-2[192.168.137.101:6382] for keys : shard-1, shard-3, shard-4, shard-5, shard-7 57 | System.out.println(key + " = " + shardedMasterSlaveJedis.get(key)); 58 | 59 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200)); 60 | System.out.println(key + " = " + shardedMasterSlaveJedis.getShard(key).opsForSlave().get(key)); // Get from one master group's one slave 61 | } 62 | 63 | shardedMasterSlaveJedisPool.returnResource(shardedMasterSlaveJedis); 64 | -------------------------------------------------------------------------------- /architecture/MasterSlaveSetinelPool.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/0f6f77c74374f90e7e1213faa2299a340ed3265e/architecture/MasterSlaveSetinelPool.jpg -------------------------------------------------------------------------------- /architecture/MasterSlaveSetinelPool.vsdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/0f6f77c74374f90e7e1213faa2299a340ed3265e/architecture/MasterSlaveSetinelPool.vsdx -------------------------------------------------------------------------------- /architecture/ShardedMasterSlaveSetinelPool.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/0f6f77c74374f90e7e1213faa2299a340ed3265e/architecture/ShardedMasterSlaveSetinelPool.jpg -------------------------------------------------------------------------------- /architecture/ShardedMasterSlaveSetinelPool.vsdx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/penggle/jedis-ms-sentinel/0f6f77c74374f90e7e1213faa2299a340ed3265e/architecture/ShardedMasterSlaveSetinelPool.vsdx -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'eclipse' 3 | apply plugin: 'maven' 4 | apply plugin: 'signing' //使用signing plugin做数字签名 5 | 6 | group = 'com.github.penggle' 7 | version = '1.0.0' 8 | sourceCompatibility = 1.7 9 | targetCompatibility = 1.7 10 | 11 | ext.versions = [ 12 | junit: '4.11', 13 | slf4j: '1.7.7', 14 | logback: '1.1.2', 15 | jedis: '2.6.2' 16 | ] 17 | 18 | [compileJava, javadoc, compileTestJava]*.options*.encoding = "UTF-8" 19 | 20 | jar { 21 | manifest { 22 | attributes 'Implementation-Title': 'jedis-ms-sentinel', 'Implementation-Version': version 23 | } 24 | } 25 | 26 | repositories { 27 | mavenCentral() 28 | } 29 | 30 | dependencies { 31 | compile( 32 | "org.slf4j:slf4j-api:${versions.slf4j}", 33 | "ch.qos.logback:logback-core:${versions.logback}", 34 | "ch.qos.logback:logback-classic:${versions.logback}", 35 | "redis.clients:jedis:${versions.jedis}" 36 | ) 37 | 38 | testCompile( 39 | "junit:junit:${versions.junit}" 40 | ) 41 | } 42 | 43 | //参见Part 2, 为项目生成**.jar/**-javadoc.jar/**-sources.jar 44 | task javadocJar(type: Jar, dependsOn: javadoc) { 45 | classifier = 'javadoc' 46 | from 'build/docs/javadoc' 47 | } 48 | 49 | task sourcesJar(type: Jar) { 50 | classifier = 'sources' 51 | from sourceSets.main.allSource 52 | } 53 | 54 | artifacts { 55 | archives jar 56 | archives javadocJar 57 | archives sourcesJar 58 | } 59 | 60 | //为所有的jar包做数字签名 61 | signing { 62 | sign configurations.archives 63 | } 64 | 65 | uploadArchives { 66 | repositories { 67 | mavenDeployer { 68 | //为Pom文件做数字签名 69 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } 70 | 71 | //指定项目部署到的中央库地址,UserName和Password就是Part 1中注册的账号, 72 | /** 73 | * 看jira中的comment,snapshot和release版本的提交地址是不一样的(注意版本号后面是否带-SNAPSHOT),否则报400 74 | * Configuration has been prepared, now you can: 75 | * Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots 76 | * Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2 77 | * Promote staged artifacts into repository 'Releases' 78 | * Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public 79 | * Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging 80 | */ 81 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") { 82 | authentication(userName: nexusUsername, password: nexusPassword) 83 | } 84 | //构造项目的Pom文件,参见Part 2中Pom文件的规范,不要遗漏必填项 85 | pom.project { 86 | name project.name 87 | packaging 'jar' 88 | description 'commons is a little java tool to make your development easier in your work.' 89 | url 'https://github.com/penggle/jedis-ms-sentinel' 90 | 91 | scm { 92 | url 'scm:git@github.com:penggle/jedis-ms-sentinel.git' 93 | connection 'scm:git@github.com:penggle/jedis-ms-sentinel.git' 94 | developerConnection 'git@github.com:penggle/jedis-ms-sentinel.git' 95 | } 96 | 97 | licenses { 98 | license { 99 | name 'The Apache Software License, Version 2.0' 100 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 101 | distribution 'repo' 102 | } 103 | } 104 | 105 | developers { 106 | developer { 107 | id 'pengpeng' 108 | name 'Peng Peng' 109 | } 110 | } 111 | } 112 | } 113 | } 114 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | signing.keyId=73218729 2 | signing.password=********** 3 | signing.secretKeyRingFile=C:/Users/DN0128/AppData/Roaming/gnupg/secring.gpg 4 | nexusUsername=pengle 5 | nexusPassword=********** -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/JedisCallback.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis; 2 | 3 | public interface JedisCallback { 4 | 5 | public O doInJedis(I jedis); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/JedisTemplate.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis; 2 | 3 | import redis.clients.util.Pool; 4 | 5 | public class JedisTemplate { 6 | 7 | private final Pool jedisPool; 8 | 9 | public JedisTemplate(Pool jedisPool){ 10 | this.jedisPool = jedisPool; 11 | } 12 | 13 | protected I getResource(){ 14 | return jedisPool.getResource(); 15 | } 16 | 17 | protected void returnResource(I jedis){ 18 | if(jedis != null){ 19 | jedisPool.returnResource(jedis); 20 | } 21 | } 22 | 23 | public O execute(JedisCallback callback){ 24 | if(callback != null){ 25 | I jedis = null; 26 | try { 27 | jedis = getResource(); 28 | return callback.doInJedis(jedis); 29 | } finally { 30 | returnResource(jedis); 31 | } 32 | } 33 | return null; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/MasterSlaveHostAndPort.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.util.Set; 4 | 5 | import redis.clients.jedis.HostAndPort; 6 | 7 | public class MasterSlaveHostAndPort { 8 | 9 | private final String masterName; 10 | 11 | private final HostAndPort master; 12 | 13 | private final Set slaves; 14 | 15 | public MasterSlaveHostAndPort(String masterName, HostAndPort master, 16 | Set slaves) { 17 | super(); 18 | this.masterName = masterName; 19 | this.master = master; 20 | this.slaves = slaves; 21 | } 22 | 23 | public String getMasterName() { 24 | return masterName; 25 | } 26 | 27 | public HostAndPort getMaster() { 28 | return master; 29 | } 30 | 31 | public Set getSlaves() { 32 | return slaves; 33 | } 34 | 35 | 36 | public int hashCode() { 37 | final int prime = 31; 38 | int result = 1; 39 | result = prime * result 40 | + ((master == null) ? 0 : master.hashCode()); 41 | result = prime * result 42 | + ((masterName == null) ? 0 : masterName.hashCode()); 43 | result = prime * result 44 | + ((slaves == null) ? 0 : slaves.hashCode()); 45 | return result; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) 50 | return true; 51 | if (obj == null) 52 | return false; 53 | if (getClass() != obj.getClass()) 54 | return false; 55 | MasterSlaveHostAndPort other = (MasterSlaveHostAndPort) obj; 56 | if (master == null) { 57 | if (other.master != null) 58 | return false; 59 | } else if (!master.equals(other.master)) 60 | return false; 61 | if (masterName == null) { 62 | if (other.masterName != null) 63 | return false; 64 | } else if (!masterName.equals(other.masterName)) 65 | return false; 66 | if (slaves == null) { 67 | if (other.slaves != null) 68 | return false; 69 | } else if (!slaves.equals(other.slaves)) 70 | return false; 71 | return true; 72 | } 73 | 74 | public String toString() { 75 | return "{masterName=" + masterName + ", master=" + master + ", slaves=" 76 | + slaves + "}"; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/MasterSlaveJedis.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.io.Closeable; 4 | import java.util.Collection; 5 | import java.util.LinkedHashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Map.Entry; 9 | import java.util.Set; 10 | import java.util.regex.Pattern; 11 | 12 | import redis.clients.jedis.AdvancedBinaryJedisCommands; 13 | import redis.clients.jedis.AdvancedJedisCommands; 14 | import redis.clients.jedis.BasicCommands; 15 | import redis.clients.jedis.BinaryClient.LIST_POSITION; 16 | import redis.clients.jedis.exceptions.JedisConnectionException; 17 | import redis.clients.jedis.BinaryJedisCommands; 18 | import redis.clients.jedis.BinaryJedisPubSub; 19 | import redis.clients.jedis.BinaryScriptingCommands; 20 | import redis.clients.jedis.BitOP; 21 | import redis.clients.jedis.Client; 22 | import redis.clients.jedis.DebugParams; 23 | import redis.clients.jedis.HostAndPort; 24 | import redis.clients.jedis.Jedis; 25 | import redis.clients.jedis.JedisCommands; 26 | import redis.clients.jedis.JedisPubSub; 27 | import redis.clients.jedis.JedisShardInfo; 28 | import redis.clients.jedis.MultiKeyBinaryCommands; 29 | import redis.clients.jedis.MultiKeyCommands; 30 | import redis.clients.jedis.ScanResult; 31 | import redis.clients.jedis.ScriptingCommands; 32 | import redis.clients.jedis.SortingParams; 33 | import redis.clients.jedis.Tuple; 34 | import redis.clients.jedis.ZParams; 35 | import redis.clients.util.Hashing; 36 | import redis.clients.util.Pool; 37 | import redis.clients.util.Sharded; 38 | import redis.clients.util.Slowlog; 39 | /** 40 | * MasterSlaveJedis默认情况下,MasterSlaveJedis下的所有方法实际都在Master上执行, 41 | * 仅仅提供了一个新方法opsForSlave(...)可以获取对应所有Slave中的某个Slave 42 | * 43 | * @author pengpeng 44 | * @date 2015年3月14日 上午10:09:41 45 | * @version 1.0 46 | */ 47 | public class MasterSlaveJedis extends Sharded implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, 48 | AdvancedBinaryJedisCommands, BinaryScriptingCommands, JedisCommands, MultiKeyCommands, 49 | AdvancedJedisCommands, ScriptingCommands, Closeable { 50 | 51 | protected final MasterSlaveHostAndPort connectionDesc; 52 | 53 | protected final JedisShardInfo masterShard; 54 | 55 | protected final List slaveShards; 56 | 57 | protected final Jedis master; 58 | 59 | protected Pool dataSource = null; 60 | 61 | public MasterSlaveJedis(JedisShardInfo masterShard, List slaveShards, Hashing algo, Pattern tagPattern) { 62 | super(slaveShards, algo, tagPattern); 63 | this.masterShard = masterShard; 64 | this.slaveShards = slaveShards; 65 | this.master = new Jedis(masterShard); 66 | Set slaves = new LinkedHashSet(); 67 | for(JedisShardInfo slaveShard : slaveShards){ 68 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 69 | } 70 | this.connectionDesc = new MasterSlaveHostAndPort(masterShard.getName(), new HostAndPort(masterShard.getHost(), masterShard.getPort()), slaves); 71 | } 72 | 73 | public MasterSlaveJedis(JedisShardInfo masterShard, List slaveShards, Hashing algo) { 74 | super(slaveShards, algo); 75 | this.masterShard = masterShard; 76 | this.slaveShards = slaveShards; 77 | this.master = new Jedis(masterShard); 78 | Set slaves = new LinkedHashSet(); 79 | for(JedisShardInfo slaveShard : slaveShards){ 80 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 81 | } 82 | this.connectionDesc = new MasterSlaveHostAndPort(masterShard.getName(), new HostAndPort(masterShard.getHost(), masterShard.getPort()), slaves); 83 | } 84 | 85 | public MasterSlaveJedis(JedisShardInfo masterShard, List slaveShards, Pattern tagPattern) { 86 | super(slaveShards, tagPattern); 87 | this.masterShard = masterShard; 88 | this.slaveShards = slaveShards; 89 | this.master = new Jedis(masterShard); 90 | Set slaves = new LinkedHashSet(); 91 | for(JedisShardInfo slaveShard : slaveShards){ 92 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 93 | } 94 | this.connectionDesc = new MasterSlaveHostAndPort(masterShard.getName(), new HostAndPort(masterShard.getHost(), masterShard.getPort()), slaves); 95 | } 96 | 97 | public MasterSlaveJedis(JedisShardInfo masterShard, List slaveShards) { 98 | super(slaveShards); 99 | this.masterShard = masterShard; 100 | this.slaveShards = slaveShards; 101 | this.master = new Jedis(masterShard); 102 | Set slaves = new LinkedHashSet(); 103 | for(JedisShardInfo slaveShard : slaveShards){ 104 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 105 | } 106 | this.connectionDesc = new MasterSlaveHostAndPort(masterShard.getName(), new HostAndPort(masterShard.getHost(), masterShard.getPort()), slaves); 107 | } 108 | 109 | /** 110 | * 获取当前所有Slave中的某个Slave 111 | * @param slaveHolder - 如果每次调用opsForSlave()方法的入参slaveHolder相同的话,那么其获取的Slave也是相同的 112 | * @return - 返回从Jedis 113 | */ 114 | public Jedis opsForSlave(String ... slaveHolder) { 115 | String holder = null; 116 | if(slaveHolder == null || slaveHolder.length == 0){ 117 | holder = String.valueOf(System.currentTimeMillis()); 118 | }else{ 119 | holder = slaveHolder[0]; 120 | if(holder == null || holder.trim().equals("")){ 121 | holder = String.valueOf(System.currentTimeMillis()); 122 | } 123 | } 124 | return getShard(holder); 125 | } 126 | 127 | public MasterSlaveHostAndPort getConnectionDesc() { 128 | return connectionDesc; 129 | } 130 | 131 | public Client getClient() { 132 | return master.getClient(); 133 | } 134 | 135 | public Object eval(String script, int keyCount, String... params) { 136 | return master.eval(script, keyCount, params); 137 | } 138 | 139 | public Object eval(String script, List keys, List args) { 140 | return master.eval(script, keys, args); 141 | } 142 | 143 | public Object eval(String script) { 144 | return master.eval(script); 145 | } 146 | 147 | public Object evalsha(String script) { 148 | return master.evalsha(script); 149 | } 150 | 151 | public Object evalsha(String sha1, List keys, List args) { 152 | return master.evalsha(sha1, keys, args); 153 | } 154 | 155 | public Object evalsha(String sha1, int keyCount, String... params) { 156 | return master.evalsha(sha1, keyCount, params); 157 | } 158 | 159 | public Boolean scriptExists(String sha1) { 160 | return master.scriptExists(sha1); 161 | } 162 | 163 | public List scriptExists(String... sha1) { 164 | return master.scriptExists(sha1); 165 | } 166 | 167 | public String scriptLoad(String script) { 168 | return master.scriptLoad(script); 169 | } 170 | 171 | public List configGet(String pattern) { 172 | return master.configGet(pattern); 173 | } 174 | 175 | public String configSet(String parameter, String value) { 176 | return master.configSet(parameter, value); 177 | } 178 | 179 | public List slowlogGet() { 180 | return master.slowlogGet(); 181 | } 182 | 183 | public List slowlogGet(long entries) { 184 | return master.slowlogGet(entries); 185 | } 186 | 187 | public Long objectRefcount(String string) { 188 | return master.objectRefcount(string); 189 | } 190 | 191 | public String objectEncoding(String string) { 192 | return master.objectEncoding(string); 193 | } 194 | 195 | public Long objectIdletime(String string) { 196 | return master.objectIdletime(string); 197 | } 198 | 199 | public Long del(String... keys) { 200 | return master.del(keys); 201 | } 202 | 203 | public List blpop(int timeout, String... keys) { 204 | return master.blpop(timeout, keys); 205 | } 206 | 207 | public List brpop(int timeout, String... keys) { 208 | return master.brpop(timeout, keys); 209 | } 210 | 211 | public List blpop(String... args) { 212 | return master.blpop(args); 213 | } 214 | 215 | public List brpop(String... args) { 216 | return master.brpop(args); 217 | } 218 | 219 | public Set keys(String pattern) { 220 | return master.keys(pattern); 221 | } 222 | 223 | public List mget(String... keys) { 224 | return master.mget(keys); 225 | } 226 | 227 | public String mset(String... keysvalues) { 228 | return master.mset(keysvalues); 229 | } 230 | 231 | public Long msetnx(String... keysvalues) { 232 | return master.msetnx(keysvalues); 233 | } 234 | 235 | public String rename(String oldkey, String newkey) { 236 | return master.rename(oldkey, newkey); 237 | } 238 | 239 | public Long renamenx(String oldkey, String newkey) { 240 | return master.renamenx(oldkey, newkey); 241 | } 242 | 243 | public String rpoplpush(String srckey, String dstkey) { 244 | return master.rpoplpush(srckey, dstkey); 245 | } 246 | 247 | public Set sdiff(String... keys) { 248 | return master.sdiff(keys); 249 | } 250 | 251 | public Long sdiffstore(String dstkey, String... keys) { 252 | return master.sdiffstore(dstkey, keys); 253 | } 254 | 255 | public Set sinter(String... keys) { 256 | return master.sinter(keys); 257 | } 258 | 259 | public Long sinterstore(String dstkey, String... keys) { 260 | return master.sinterstore(dstkey, keys); 261 | } 262 | 263 | public Long smove(String srckey, String dstkey, String member) { 264 | return master.smove(srckey, dstkey, member); 265 | } 266 | 267 | public Long sort(String key, SortingParams sortingParameters, String dstkey) { 268 | return master.sort(key, sortingParameters, dstkey); 269 | } 270 | 271 | public Long sort(String key, String dstkey) { 272 | return master.sort(key, dstkey); 273 | } 274 | 275 | public Set sunion(String... keys) { 276 | return master.sunion(keys); 277 | } 278 | 279 | public Long sunionstore(String dstkey, String... keys) { 280 | return master.sunionstore(dstkey, keys); 281 | } 282 | 283 | public String watch(String... keys) { 284 | return master.watch(keys); 285 | } 286 | 287 | public Long zinterstore(String dstkey, String... sets) { 288 | return master.zinterstore(dstkey, sets); 289 | } 290 | 291 | public Long zinterstore(String dstkey, ZParams params, String... sets) { 292 | return master.zinterstore(dstkey, params, sets); 293 | } 294 | 295 | public Long zunionstore(String dstkey, String... sets) { 296 | return master.zunionstore(dstkey, sets); 297 | } 298 | 299 | public Long zunionstore(String dstkey, ZParams params, String... sets) { 300 | return master.zunionstore(dstkey, params, sets); 301 | } 302 | 303 | public String brpoplpush(String source, String destination, int timeout) { 304 | return master.brpoplpush(source, destination, timeout); 305 | } 306 | 307 | public Long publish(String channel, String message) { 308 | return master.publish(channel, message); 309 | } 310 | 311 | public void subscribe(JedisPubSub jedisPubSub, String... channels) { 312 | master.subscribe(jedisPubSub, channels); 313 | } 314 | 315 | public void psubscribe(JedisPubSub jedisPubSub, String... patterns) { 316 | master.psubscribe(jedisPubSub, patterns); 317 | } 318 | 319 | public String randomKey() { 320 | return master.randomKey(); 321 | } 322 | 323 | public Long bitop(BitOP op, String destKey, String... srcKeys) { 324 | return master.bitop(op, destKey, srcKeys); 325 | } 326 | 327 | @Deprecated 328 | public ScanResult scan(int cursor) { 329 | return master.scan(cursor); 330 | } 331 | 332 | public ScanResult scan(String cursor) { 333 | return master.scan(cursor); 334 | } 335 | 336 | public String pfmerge(String destkey, String... sourcekeys) { 337 | return master.pfmerge(destkey, sourcekeys); 338 | } 339 | 340 | public long pfcount(String... keys) { 341 | return master.pfcount(keys); 342 | } 343 | 344 | public String set(String key, String value) { 345 | return master.set(key, value); 346 | } 347 | 348 | public String set(String key, String value, String nxxx, String expx, 349 | long time) { 350 | return master.set(key, value, nxxx, expx, time); 351 | } 352 | 353 | public String get(String key) { 354 | return master.get(key); 355 | } 356 | 357 | public Boolean exists(String key) { 358 | return master.exists(key); 359 | } 360 | 361 | public Long persist(String key) { 362 | return master.persist(key); 363 | } 364 | 365 | public String type(String key) { 366 | return master.type(key); 367 | } 368 | 369 | public Long expire(String key, int seconds) { 370 | return master.expire(key, seconds); 371 | } 372 | 373 | public Long expireAt(String key, long unixTime) { 374 | return master.expireAt(key, unixTime); 375 | } 376 | 377 | public Long ttl(String key) { 378 | return master.ttl(key); 379 | } 380 | 381 | public Boolean setbit(String key, long offset, boolean value) { 382 | return master.setbit(key, offset, value); 383 | } 384 | 385 | public Boolean setbit(String key, long offset, String value) { 386 | return master.setbit(key, offset, value); 387 | } 388 | 389 | public Boolean getbit(String key, long offset) { 390 | return master.getbit(key, offset); 391 | } 392 | 393 | public Long setrange(String key, long offset, String value) { 394 | return master.setrange(key, offset, value); 395 | } 396 | 397 | public String getrange(String key, long startOffset, long endOffset) { 398 | return master.getrange(key, startOffset, endOffset); 399 | } 400 | 401 | public String getSet(String key, String value) { 402 | return master.getSet(key, value); 403 | } 404 | 405 | public Long setnx(String key, String value) { 406 | return master.setnx(key, value); 407 | } 408 | 409 | public String setex(String key, int seconds, String value) { 410 | return master.setex(key, seconds, value); 411 | } 412 | 413 | public Long decrBy(String key, long integer) { 414 | return master.decrBy(key, integer); 415 | } 416 | 417 | public Long decr(String key) { 418 | return master.decr(key); 419 | } 420 | 421 | public Long incrBy(String key, long integer) { 422 | return master.incrBy(key, integer); 423 | } 424 | 425 | public Double incrByFloat(String key, double integer) { 426 | return master.incrByFloat(key, integer); 427 | } 428 | 429 | public Long incr(String key) { 430 | return master.incr(key); 431 | } 432 | 433 | public Long append(String key, String value) { 434 | return master.append(key, value); 435 | } 436 | 437 | public String substr(String key, int start, int end) { 438 | return master.substr(key, start, end); 439 | } 440 | 441 | public Long hset(String key, String field, String value) { 442 | return master.hset(key, field, value); 443 | } 444 | 445 | public String hget(String key, String field) { 446 | return master.hget(key, field); 447 | } 448 | 449 | public Long hsetnx(String key, String field, String value) { 450 | return master.hsetnx(key, field, value); 451 | } 452 | 453 | public String hmset(String key, Map hash) { 454 | return master.hmset(key, hash); 455 | } 456 | 457 | public List hmget(String key, String... fields) { 458 | return master.hmget(key, fields); 459 | } 460 | 461 | public Long hincrBy(String key, String field, long value) { 462 | return master.hincrBy(key, field, value); 463 | } 464 | 465 | public Double hincrByFloat(String key, String field, double value) { 466 | return master.hincrByFloat(key, field, value); 467 | } 468 | 469 | public Boolean hexists(String key, String field) { 470 | return master.hexists(key, field); 471 | } 472 | 473 | public Long hdel(String key, String... fields) { 474 | return master.hdel(key, fields); 475 | } 476 | 477 | public Long hlen(String key) { 478 | return master.hlen(key); 479 | } 480 | 481 | public Set hkeys(String key) { 482 | return master.hkeys(key); 483 | } 484 | 485 | public List hvals(String key) { 486 | return master.hvals(key); 487 | } 488 | 489 | public Map hgetAll(String key) { 490 | return master.hgetAll(key); 491 | } 492 | 493 | public Long rpush(String key, String... strings) { 494 | return master.rpush(key, strings); 495 | } 496 | 497 | public Long lpush(String key, String... strings) { 498 | return master.lpush(key, strings); 499 | } 500 | 501 | public Long llen(String key) { 502 | return master.llen(key); 503 | } 504 | 505 | public List lrange(String key, long start, long end) { 506 | return master.lrange(key, start, end); 507 | } 508 | 509 | public String ltrim(String key, long start, long end) { 510 | return master.ltrim(key, start, end); 511 | } 512 | 513 | public String lindex(String key, long index) { 514 | return master.lindex(key, index); 515 | } 516 | 517 | public String lset(String key, long index, String value) { 518 | return master.lset(key, index, value); 519 | } 520 | 521 | public Long lrem(String key, long count, String value) { 522 | return master.lrem(key, count, value); 523 | } 524 | 525 | public String lpop(String key) { 526 | return master.lpop(key); 527 | } 528 | 529 | public String rpop(String key) { 530 | return master.rpop(key); 531 | } 532 | 533 | public Long sadd(String key, String... members) { 534 | return master.sadd(key, members); 535 | } 536 | 537 | public Set smembers(String key) { 538 | return master.smembers(key); 539 | } 540 | 541 | public Long srem(String key, String... members) { 542 | return master.srem(key, members); 543 | } 544 | 545 | public String spop(String key) { 546 | return master.spop(key); 547 | } 548 | 549 | public Long scard(String key) { 550 | return master.scard(key); 551 | } 552 | 553 | public Boolean sismember(String key, String member) { 554 | return master.sismember(key, member); 555 | } 556 | 557 | public String srandmember(String key) { 558 | return master.srandmember(key); 559 | } 560 | 561 | public List srandmember(String key, int count) { 562 | return master.srandmember(key, count); 563 | } 564 | 565 | public Long strlen(String key) { 566 | return master.strlen(key); 567 | } 568 | 569 | public Long zadd(String key, double score, String member) { 570 | return master.zadd(key, score, member); 571 | } 572 | 573 | public Long zadd(String key, Map scoreMembers) { 574 | return master.zadd(key, scoreMembers); 575 | } 576 | 577 | public Set zrange(String key, long start, long end) { 578 | return master.zrange(key, start, end); 579 | } 580 | 581 | public Long zrem(String key, String... members) { 582 | return master.zrem(key, members); 583 | } 584 | 585 | public Double zincrby(String key, double score, String member) { 586 | return master.zincrby(key, score, member); 587 | } 588 | 589 | public Long zrank(String key, String member) { 590 | return master.zrank(key, member); 591 | } 592 | 593 | public Long zrevrank(String key, String member) { 594 | return master.zrevrank(key, member); 595 | } 596 | 597 | public Set zrevrange(String key, long start, long end) { 598 | return master.zrevrange(key, start, end); 599 | } 600 | 601 | public Set zrangeWithScores(String key, long start, long end) { 602 | return master.zrangeWithScores(key, start, end); 603 | } 604 | 605 | public Set zrevrangeWithScores(String key, long start, long end) { 606 | return master.zrevrangeWithScores(key, start, end); 607 | } 608 | 609 | public Long zcard(String key) { 610 | return master.zcard(key); 611 | } 612 | 613 | public Double zscore(String key, String member) { 614 | return master.zscore(key, member); 615 | } 616 | 617 | public List sort(String key) { 618 | return master.sort(key); 619 | } 620 | 621 | public List sort(String key, SortingParams sortingParameters) { 622 | return master.sort(key, sortingParameters); 623 | } 624 | 625 | public Long zcount(String key, double min, double max) { 626 | return master.zcount(key, min, max); 627 | } 628 | 629 | public Long zcount(String key, String min, String max) { 630 | return master.zcount(key, min, max); 631 | } 632 | 633 | public Set zrangeByScore(String key, double min, double max) { 634 | return master.zrangeByScore(key, min, max); 635 | } 636 | 637 | public Set zrangeByScore(String key, String min, String max) { 638 | return master.zrangeByScore(key, min, max); 639 | } 640 | 641 | public Set zrevrangeByScore(String key, double max, double min) { 642 | return master.zrevrangeByScore(key, max, min); 643 | } 644 | 645 | public Set zrangeByScore(String key, double min, double max, 646 | int offset, int count) { 647 | return master.zrangeByScore(key, min, max, offset, count); 648 | } 649 | 650 | public Set zrevrangeByScore(String key, String max, String min) { 651 | return master.zrevrangeByScore(key, max, min); 652 | } 653 | 654 | public Set zrangeByScore(String key, String min, String max, 655 | int offset, int count) { 656 | return master.zrangeByScore(key, min, max, offset, count); 657 | } 658 | 659 | public Set zrevrangeByScore(String key, double max, double min, 660 | int offset, int count) { 661 | return master.zrevrangeByScore(key, max, min, offset, count); 662 | } 663 | 664 | public Set zrangeByScoreWithScores(String key, double min, double max) { 665 | return master.zrangeByScoreWithScores(key, min, max); 666 | } 667 | 668 | public Set zrevrangeByScoreWithScores(String key, double max, 669 | double min) { 670 | return master.zrevrangeByScoreWithScores(key, max, min); 671 | } 672 | 673 | public Set zrangeByScoreWithScores(String key, double min, 674 | double max, int offset, int count) { 675 | return master.zrangeByScoreWithScores(key, min, max, offset, count); 676 | } 677 | 678 | public Set zrevrangeByScore(String key, String max, String min, 679 | int offset, int count) { 680 | return master.zrevrangeByScore(key, max, min, offset, count); 681 | } 682 | 683 | public Set zrangeByScoreWithScores(String key, String min, String max) { 684 | return master.zrangeByScoreWithScores(key, min, max); 685 | } 686 | 687 | public Set zrevrangeByScoreWithScores(String key, String max, 688 | String min) { 689 | return master.zrevrangeByScoreWithScores(key, max, min); 690 | } 691 | 692 | public Set zrangeByScoreWithScores(String key, String min, 693 | String max, int offset, int count) { 694 | return master.zrangeByScoreWithScores(key, min, max, offset, count); 695 | } 696 | 697 | public Set zrevrangeByScoreWithScores(String key, double max, 698 | double min, int offset, int count) { 699 | return master.zrevrangeByScoreWithScores(key, max, min, offset, count); 700 | } 701 | 702 | public Set zrevrangeByScoreWithScores(String key, String max, 703 | String min, int offset, int count) { 704 | return master.zrevrangeByScoreWithScores(key, max, min, offset, count); 705 | } 706 | 707 | public Long zremrangeByRank(String key, long start, long end) { 708 | return master.zremrangeByRank(key, start, end); 709 | } 710 | 711 | public Long zremrangeByScore(String key, double start, double end) { 712 | return master.zremrangeByScore(key, start, end); 713 | } 714 | 715 | public Long zremrangeByScore(String key, String start, String end) { 716 | return master.zremrangeByScore(key, start, end); 717 | } 718 | 719 | public Long zlexcount(String key, String min, String max) { 720 | return master.zlexcount(key, min, max); 721 | } 722 | 723 | public Set zrangeByLex(String key, String min, String max) { 724 | return master.zrangeByLex(key, min, max); 725 | } 726 | 727 | public Set zrangeByLex(String key, String min, String max, 728 | int offset, int count) { 729 | return master.zrangeByLex(key, min, max, offset, count); 730 | } 731 | 732 | public Long zremrangeByLex(String key, String min, String max) { 733 | return master.zremrangeByLex(key, min, max); 734 | } 735 | 736 | public Long linsert(String key, LIST_POSITION where, String pivot, 737 | String value) { 738 | return master.linsert(key, where, pivot, value); 739 | } 740 | 741 | public Long lpushx(String key, String... string) { 742 | return master.lpushx(key, string); 743 | } 744 | 745 | public Long rpushx(String key, String... string) { 746 | return master.rpushx(key, string); 747 | } 748 | 749 | @Deprecated 750 | public List blpop(String arg) { 751 | return master.blpop(arg); 752 | } 753 | 754 | public List blpop(int timeout, String key) { 755 | return master.blpop(timeout, key); 756 | } 757 | 758 | @Deprecated 759 | public List brpop(String arg) { 760 | return master.brpop(arg); 761 | } 762 | 763 | public List brpop(int timeout, String key) { 764 | return master.brpop(timeout, key); 765 | } 766 | 767 | public Long del(String key) { 768 | return master.del(key); 769 | } 770 | 771 | public String echo(String string) { 772 | return master.echo(string); 773 | } 774 | 775 | public Long move(String key, int dbIndex) { 776 | return master.move(key, dbIndex); 777 | } 778 | 779 | public Long bitcount(String key) { 780 | return master.bitcount(key); 781 | } 782 | 783 | public Long bitcount(String key, long start, long end) { 784 | return master.bitcount(key, start, end); 785 | } 786 | 787 | @Deprecated 788 | public ScanResult> hscan(String key, int cursor) { 789 | return master.hscan(key, cursor); 790 | } 791 | 792 | @Deprecated 793 | public ScanResult sscan(String key, int cursor) { 794 | return master.sscan(key, cursor); 795 | } 796 | 797 | @Deprecated 798 | public ScanResult zscan(String key, int cursor) { 799 | return master.zscan(key, cursor); 800 | } 801 | 802 | public ScanResult> hscan(String key, String cursor) { 803 | return master.hscan(key, cursor); 804 | } 805 | 806 | public ScanResult sscan(String key, String cursor) { 807 | return master.sscan(key, cursor); 808 | } 809 | 810 | public ScanResult zscan(String key, String cursor) { 811 | return master.zscan(key, cursor); 812 | } 813 | 814 | public Long pfadd(String key, String... elements) { 815 | return master.pfadd(key, elements); 816 | } 817 | 818 | public long pfcount(String key) { 819 | return master.pfcount(key); 820 | } 821 | 822 | public Object eval(byte[] script, byte[] keyCount, byte[]... params) { 823 | return master.eval(script, keyCount, params); 824 | } 825 | 826 | public Object eval(byte[] script, int keyCount, byte[]... params) { 827 | return master.eval(script, keyCount, params); 828 | } 829 | 830 | public Object eval(byte[] script, List keys, List args) { 831 | return master.eval(script, keys, args); 832 | } 833 | 834 | public Object eval(byte[] script) { 835 | return master.eval(script); 836 | } 837 | 838 | public Object evalsha(byte[] script) { 839 | return master.evalsha(script); 840 | } 841 | 842 | public Object evalsha(byte[] sha1, List keys, List args) { 843 | return master.evalsha(sha1, keys, args); 844 | } 845 | 846 | public Object evalsha(byte[] sha1, int keyCount, byte[]... params) { 847 | return master.evalsha(sha1, keyCount, params); 848 | } 849 | 850 | public List scriptExists(byte[]... sha1) { 851 | return master.scriptExists(sha1); 852 | } 853 | 854 | public byte[] scriptLoad(byte[] script) { 855 | return master.scriptLoad(script); 856 | } 857 | 858 | public String scriptFlush() { 859 | return master.scriptFlush(); 860 | } 861 | 862 | public String scriptKill() { 863 | return master.scriptKill(); 864 | } 865 | 866 | public List configGet(byte[] pattern) { 867 | return master.configGet(pattern); 868 | } 869 | 870 | public byte[] configSet(byte[] parameter, byte[] value) { 871 | return master.configSet(parameter, value); 872 | } 873 | 874 | public String slowlogReset() { 875 | return master.slowlogReset(); 876 | } 877 | 878 | public Long slowlogLen() { 879 | return master.slowlogLen(); 880 | } 881 | 882 | public List slowlogGetBinary() { 883 | return master.slowlogGetBinary(); 884 | } 885 | 886 | public List slowlogGetBinary(long entries) { 887 | return master.slowlogGetBinary(entries); 888 | } 889 | 890 | public Long objectRefcount(byte[] key) { 891 | return master.objectRefcount(key); 892 | } 893 | 894 | public byte[] objectEncoding(byte[] key) { 895 | return master.objectEncoding(key); 896 | } 897 | 898 | public Long objectIdletime(byte[] key) { 899 | return master.objectIdletime(key); 900 | } 901 | 902 | public Long del(byte[]... keys) { 903 | return master.del(keys); 904 | } 905 | 906 | public List blpop(int timeout, byte[]... keys) { 907 | return master.blpop(timeout, keys); 908 | } 909 | 910 | public List brpop(int timeout, byte[]... keys) { 911 | return master.brpop(timeout, keys); 912 | } 913 | 914 | public List blpop(byte[]... args) { 915 | return master.blpop(args); 916 | } 917 | 918 | public List brpop(byte[]... args) { 919 | return master.brpop(args); 920 | } 921 | 922 | public Set keys(byte[] pattern) { 923 | return master.keys(pattern); 924 | } 925 | 926 | public List mget(byte[]... keys) { 927 | return master.mget(keys); 928 | } 929 | 930 | public String mset(byte[]... keysvalues) { 931 | return master.mset(keysvalues); 932 | } 933 | 934 | public Long msetnx(byte[]... keysvalues) { 935 | return master.msetnx(keysvalues); 936 | } 937 | 938 | public String rename(byte[] oldkey, byte[] newkey) { 939 | return master.rename(oldkey, newkey); 940 | } 941 | 942 | public Long renamenx(byte[] oldkey, byte[] newkey) { 943 | return master.renamenx(oldkey, newkey); 944 | } 945 | 946 | public byte[] rpoplpush(byte[] srckey, byte[] dstkey) { 947 | return master.rpoplpush(srckey, dstkey); 948 | } 949 | 950 | public Set sdiff(byte[]... keys) { 951 | return master.sdiff(keys); 952 | } 953 | 954 | public Long sdiffstore(byte[] dstkey, byte[]... keys) { 955 | return master.sdiffstore(dstkey, keys); 956 | } 957 | 958 | public Set sinter(byte[]... keys) { 959 | return master.sinter(keys); 960 | } 961 | 962 | public Long sinterstore(byte[] dstkey, byte[]... keys) { 963 | return master.sinterstore(dstkey, keys); 964 | } 965 | 966 | public Long smove(byte[] srckey, byte[] dstkey, byte[] member) { 967 | return master.smove(srckey, dstkey, member); 968 | } 969 | 970 | public Long sort(byte[] key, SortingParams sortingParameters, byte[] dstkey) { 971 | return master.sort(key, sortingParameters, dstkey); 972 | } 973 | 974 | public Long sort(byte[] key, byte[] dstkey) { 975 | return master.sort(key, dstkey); 976 | } 977 | 978 | public Set sunion(byte[]... keys) { 979 | return master.sunion(keys); 980 | } 981 | 982 | public Long sunionstore(byte[] dstkey, byte[]... keys) { 983 | return master.sunionstore(dstkey, keys); 984 | } 985 | 986 | public String watch(byte[]... keys) { 987 | return master.watch(keys); 988 | } 989 | 990 | public String unwatch() { 991 | return master.unwatch(); 992 | } 993 | 994 | public Long zinterstore(byte[] dstkey, byte[]... sets) { 995 | return master.zinterstore(dstkey, sets); 996 | } 997 | 998 | public Long zinterstore(byte[] dstkey, ZParams params, byte[]... sets) { 999 | return master.zinterstore(dstkey, params, sets); 1000 | } 1001 | 1002 | public Long zunionstore(byte[] dstkey, byte[]... sets) { 1003 | return master.zunionstore(dstkey, sets); 1004 | } 1005 | 1006 | public Long zunionstore(byte[] dstkey, ZParams params, byte[]... sets) { 1007 | return master.zunionstore(dstkey, params, sets); 1008 | } 1009 | 1010 | public byte[] brpoplpush(byte[] source, byte[] destination, int timeout) { 1011 | return master.brpoplpush(source, destination, timeout); 1012 | } 1013 | 1014 | public Long publish(byte[] channel, byte[] message) { 1015 | return master.publish(channel, message); 1016 | } 1017 | 1018 | public void subscribe(BinaryJedisPubSub jedisPubSub, byte[]... channels) { 1019 | master.subscribe(jedisPubSub, channels); 1020 | } 1021 | 1022 | public void psubscribe(BinaryJedisPubSub jedisPubSub, byte[]... patterns) { 1023 | master.psubscribe(jedisPubSub, patterns); 1024 | } 1025 | 1026 | public byte[] randomBinaryKey() { 1027 | return master.randomBinaryKey(); 1028 | } 1029 | 1030 | public Long bitop(BitOP op, byte[] destKey, byte[]... srcKeys) { 1031 | return master.bitop(op, destKey, srcKeys); 1032 | } 1033 | 1034 | public String pfmerge(byte[] destkey, byte[]... sourcekeys) { 1035 | return master.pfmerge(destkey, sourcekeys); 1036 | } 1037 | 1038 | public Long pfcount(byte[]... keys) { 1039 | return master.pfcount(keys); 1040 | } 1041 | 1042 | public String set(byte[] key, byte[] value) { 1043 | return master.set(key, value); 1044 | } 1045 | 1046 | public byte[] get(byte[] key) { 1047 | return master.get(key); 1048 | } 1049 | 1050 | public Boolean exists(byte[] key) { 1051 | return master.exists(key); 1052 | } 1053 | 1054 | public Long persist(byte[] key) { 1055 | return master.persist(key); 1056 | } 1057 | 1058 | public String type(byte[] key) { 1059 | return master.type(key); 1060 | } 1061 | 1062 | public Long expire(byte[] key, int seconds) { 1063 | return master.expire(key, seconds); 1064 | } 1065 | 1066 | public Long expireAt(byte[] key, long unixTime) { 1067 | return master.expireAt(key, unixTime); 1068 | } 1069 | 1070 | public Long ttl(byte[] key) { 1071 | return master.ttl(key); 1072 | } 1073 | 1074 | public Boolean setbit(byte[] key, long offset, boolean value) { 1075 | return master.setbit(key, offset, value); 1076 | } 1077 | 1078 | public Boolean setbit(byte[] key, long offset, byte[] value) { 1079 | return master.setbit(key, offset, value); 1080 | } 1081 | 1082 | public Boolean getbit(byte[] key, long offset) { 1083 | return master.getbit(key, offset); 1084 | } 1085 | 1086 | public Long setrange(byte[] key, long offset, byte[] value) { 1087 | return master.setrange(key, offset, value); 1088 | } 1089 | 1090 | public byte[] getrange(byte[] key, long startOffset, long endOffset) { 1091 | return master.getrange(key, startOffset, endOffset); 1092 | } 1093 | 1094 | public byte[] getSet(byte[] key, byte[] value) { 1095 | return master.getSet(key, value); 1096 | } 1097 | 1098 | public Long setnx(byte[] key, byte[] value) { 1099 | return master.setnx(key, value); 1100 | } 1101 | 1102 | public String setex(byte[] key, int seconds, byte[] value) { 1103 | return master.setex(key, seconds, value); 1104 | } 1105 | 1106 | public Long decrBy(byte[] key, long integer) { 1107 | return master.decrBy(key, integer); 1108 | } 1109 | 1110 | public Long decr(byte[] key) { 1111 | return master.decr(key); 1112 | } 1113 | 1114 | public Long incrBy(byte[] key, long integer) { 1115 | return master.incrBy(key, integer); 1116 | } 1117 | 1118 | public Double incrByFloat(byte[] key, double value) { 1119 | return master.incrByFloat(key, value); 1120 | } 1121 | 1122 | public Long incr(byte[] key) { 1123 | return master.incr(key); 1124 | } 1125 | 1126 | public Long append(byte[] key, byte[] value) { 1127 | return master.append(key, value); 1128 | } 1129 | 1130 | public byte[] substr(byte[] key, int start, int end) { 1131 | return master.substr(key, start, end); 1132 | } 1133 | 1134 | public Long hset(byte[] key, byte[] field, byte[] value) { 1135 | return master.hset(key, field, value); 1136 | } 1137 | 1138 | public byte[] hget(byte[] key, byte[] field) { 1139 | return master.hget(key, field); 1140 | } 1141 | 1142 | public Long hsetnx(byte[] key, byte[] field, byte[] value) { 1143 | return master.hsetnx(key, field, value); 1144 | } 1145 | 1146 | public String hmset(byte[] key, Map hash) { 1147 | return master.hmset(key, hash); 1148 | } 1149 | 1150 | public List hmget(byte[] key, byte[]... fields) { 1151 | return master.hmget(key, fields); 1152 | } 1153 | 1154 | public Long hincrBy(byte[] key, byte[] field, long value) { 1155 | return master.hincrBy(key, field, value); 1156 | } 1157 | 1158 | public Double hincrByFloat(byte[] key, byte[] field, double value) { 1159 | return master.hincrByFloat(key, field, value); 1160 | } 1161 | 1162 | public Boolean hexists(byte[] key, byte[] field) { 1163 | return master.hexists(key, field); 1164 | } 1165 | 1166 | public Long hdel(byte[] key, byte[]... fields) { 1167 | return master.hdel(key, fields); 1168 | } 1169 | 1170 | public Long hlen(byte[] key) { 1171 | return master.hlen(key); 1172 | } 1173 | 1174 | public Set hkeys(byte[] key) { 1175 | return master.hkeys(key); 1176 | } 1177 | 1178 | public Collection hvals(byte[] key) { 1179 | return master.hvals(key); 1180 | } 1181 | 1182 | public Map hgetAll(byte[] key) { 1183 | return master.hgetAll(key); 1184 | } 1185 | 1186 | public Long rpush(byte[] key, byte[]... args) { 1187 | return master.rpush(key, args); 1188 | } 1189 | 1190 | public Long lpush(byte[] key, byte[]... args) { 1191 | return master.lpush(key, args); 1192 | } 1193 | 1194 | public Long llen(byte[] key) { 1195 | return master.llen(key); 1196 | } 1197 | 1198 | public List lrange(byte[] key, long start, long end) { 1199 | return master.lrange(key, start, end); 1200 | } 1201 | 1202 | public String ltrim(byte[] key, long start, long end) { 1203 | return master.ltrim(key, start, end); 1204 | } 1205 | 1206 | public byte[] lindex(byte[] key, long index) { 1207 | return master.lindex(key, index); 1208 | } 1209 | 1210 | public String lset(byte[] key, long index, byte[] value) { 1211 | return master.lset(key, index, value); 1212 | } 1213 | 1214 | public Long lrem(byte[] key, long count, byte[] value) { 1215 | return master.lrem(key, count, value); 1216 | } 1217 | 1218 | public byte[] lpop(byte[] key) { 1219 | return master.lpop(key); 1220 | } 1221 | 1222 | public byte[] rpop(byte[] key) { 1223 | return master.rpop(key); 1224 | } 1225 | 1226 | public Long sadd(byte[] key, byte[]... members) { 1227 | return master.sadd(key, members); 1228 | } 1229 | 1230 | public Set smembers(byte[] key) { 1231 | return master.smembers(key); 1232 | } 1233 | 1234 | public Long srem(byte[] key, byte[]... member) { 1235 | return master.srem(key, member); 1236 | } 1237 | 1238 | public byte[] spop(byte[] key) { 1239 | return master.spop(key); 1240 | } 1241 | 1242 | public Long scard(byte[] key) { 1243 | return master.scard(key); 1244 | } 1245 | 1246 | public Boolean sismember(byte[] key, byte[] member) { 1247 | return master.sismember(key, member); 1248 | } 1249 | 1250 | public byte[] srandmember(byte[] key) { 1251 | return master.srandmember(key); 1252 | } 1253 | 1254 | public List srandmember(byte[] key, int count) { 1255 | return master.srandmember(key, count); 1256 | } 1257 | 1258 | public Long strlen(byte[] key) { 1259 | return master.strlen(key); 1260 | } 1261 | 1262 | public Long zadd(byte[] key, double score, byte[] member) { 1263 | return master.zadd(key, score, member); 1264 | } 1265 | 1266 | public Long zadd(byte[] key, Map scoreMembers) { 1267 | return master.zadd(key, scoreMembers); 1268 | } 1269 | 1270 | public Set zrange(byte[] key, long start, long end) { 1271 | return master.zrange(key, start, end); 1272 | } 1273 | 1274 | public Long zrem(byte[] key, byte[]... members) { 1275 | return master.zrem(key, members); 1276 | } 1277 | 1278 | public Double zincrby(byte[] key, double score, byte[] member) { 1279 | return master.zincrby(key, score, member); 1280 | } 1281 | 1282 | public Long zrank(byte[] key, byte[] member) { 1283 | return master.zrank(key, member); 1284 | } 1285 | 1286 | public Long zrevrank(byte[] key, byte[] member) { 1287 | return master.zrevrank(key, member); 1288 | } 1289 | 1290 | public Set zrevrange(byte[] key, long start, long end) { 1291 | return master.zrevrange(key, start, end); 1292 | } 1293 | 1294 | public Set zrangeWithScores(byte[] key, long start, long end) { 1295 | return master.zrangeWithScores(key, start, end); 1296 | } 1297 | 1298 | public Set zrevrangeWithScores(byte[] key, long start, long end) { 1299 | return master.zrevrangeWithScores(key, start, end); 1300 | } 1301 | 1302 | public Long zcard(byte[] key) { 1303 | return master.zcard(key); 1304 | } 1305 | 1306 | public Double zscore(byte[] key, byte[] member) { 1307 | return master.zscore(key, member); 1308 | } 1309 | 1310 | public List sort(byte[] key) { 1311 | return master.sort(key); 1312 | } 1313 | 1314 | public List sort(byte[] key, SortingParams sortingParameters) { 1315 | return master.sort(key, sortingParameters); 1316 | } 1317 | 1318 | public Long zcount(byte[] key, double min, double max) { 1319 | return master.zcount(key, min, max); 1320 | } 1321 | 1322 | public Long zcount(byte[] key, byte[] min, byte[] max) { 1323 | return master.zcount(key, min, max); 1324 | } 1325 | 1326 | public Set zrangeByScore(byte[] key, double min, double max) { 1327 | return master.zrangeByScore(key, min, max); 1328 | } 1329 | 1330 | public Set zrangeByScore(byte[] key, byte[] min, byte[] max) { 1331 | return master.zrangeByScore(key, min, max); 1332 | } 1333 | 1334 | public Set zrevrangeByScore(byte[] key, double max, double min) { 1335 | return master.zrevrangeByScore(key, max, min); 1336 | } 1337 | 1338 | public Set zrangeByScore(byte[] key, double min, double max, 1339 | int offset, int count) { 1340 | return master.zrangeByScore(key, min, max, offset, count); 1341 | } 1342 | 1343 | public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min) { 1344 | return master.zrevrangeByScore(key, max, min); 1345 | } 1346 | 1347 | public Set zrangeByScore(byte[] key, byte[] min, byte[] max, 1348 | int offset, int count) { 1349 | return master.zrangeByScore(key, min, max, offset, count); 1350 | } 1351 | 1352 | public Set zrevrangeByScore(byte[] key, double max, double min, 1353 | int offset, int count) { 1354 | return master.zrevrangeByScore(key, max, min, offset, count); 1355 | } 1356 | 1357 | public Set zrangeByScoreWithScores(byte[] key, double min, double max) { 1358 | return master.zrangeByScoreWithScores(key, min, max); 1359 | } 1360 | 1361 | public Set zrevrangeByScoreWithScores(byte[] key, double max, 1362 | double min) { 1363 | return master.zrevrangeByScoreWithScores(key, max, min); 1364 | } 1365 | 1366 | public Set zrangeByScoreWithScores(byte[] key, double min, 1367 | double max, int offset, int count) { 1368 | return master.zrangeByScoreWithScores(key, min, max, offset, count); 1369 | } 1370 | 1371 | public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min, 1372 | int offset, int count) { 1373 | return master.zrevrangeByScore(key, max, min, offset, count); 1374 | } 1375 | 1376 | public Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max) { 1377 | return master.zrangeByScoreWithScores(key, min, max); 1378 | } 1379 | 1380 | public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, 1381 | byte[] min) { 1382 | return master.zrevrangeByScoreWithScores(key, max, min); 1383 | } 1384 | 1385 | public Set zrangeByScoreWithScores(byte[] key, byte[] min, 1386 | byte[] max, int offset, int count) { 1387 | return master.zrangeByScoreWithScores(key, min, max, offset, count); 1388 | } 1389 | 1390 | public Set zrevrangeByScoreWithScores(byte[] key, double max, 1391 | double min, int offset, int count) { 1392 | return master.zrevrangeByScoreWithScores(key, max, min, offset, count); 1393 | } 1394 | 1395 | public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, 1396 | byte[] min, int offset, int count) { 1397 | return master.zrevrangeByScoreWithScores(key, max, min, offset, count); 1398 | } 1399 | 1400 | public Long zremrangeByRank(byte[] key, long start, long end) { 1401 | return master.zremrangeByRank(key, start, end); 1402 | } 1403 | 1404 | public Long zremrangeByScore(byte[] key, double start, double end) { 1405 | return master.zremrangeByScore(key, start, end); 1406 | } 1407 | 1408 | public Long zremrangeByScore(byte[] key, byte[] start, byte[] end) { 1409 | return master.zremrangeByScore(key, start, end); 1410 | } 1411 | 1412 | public Long zlexcount(byte[] key, byte[] min, byte[] max) { 1413 | return master.zlexcount(key, min, max); 1414 | } 1415 | 1416 | public Set zrangeByLex(byte[] key, byte[] min, byte[] max) { 1417 | return master.zrangeByLex(key, min, max); 1418 | } 1419 | 1420 | public Set zrangeByLex(byte[] key, byte[] min, byte[] max, 1421 | int offset, int count) { 1422 | return master.zrangeByLex(key, min, max, offset, count); 1423 | } 1424 | 1425 | public Long zremrangeByLex(byte[] key, byte[] min, byte[] max) { 1426 | return master.zremrangeByLex(key, min, max); 1427 | } 1428 | 1429 | public Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, 1430 | byte[] value) { 1431 | return master.linsert(key, where, pivot, value); 1432 | } 1433 | 1434 | public Long lpushx(byte[] key, byte[]... arg) { 1435 | return master.lpushx(key, arg); 1436 | } 1437 | 1438 | public Long rpushx(byte[] key, byte[]... arg) { 1439 | return master.rpushx(key, arg); 1440 | } 1441 | 1442 | @Deprecated 1443 | public List blpop(byte[] arg) { 1444 | return master.blpop(arg); 1445 | } 1446 | 1447 | @Deprecated 1448 | public List brpop(byte[] arg) { 1449 | return master.brpop(arg); 1450 | } 1451 | 1452 | public Long del(byte[] key) { 1453 | return master.del(key); 1454 | } 1455 | 1456 | public byte[] echo(byte[] arg) { 1457 | return master.echo(arg); 1458 | } 1459 | 1460 | public Long move(byte[] key, int dbIndex) { 1461 | return master.move(key, dbIndex); 1462 | } 1463 | 1464 | public Long bitcount(byte[] key) { 1465 | return master.bitcount(key); 1466 | } 1467 | 1468 | public Long bitcount(byte[] key, long start, long end) { 1469 | return master.bitcount(key, start, end); 1470 | } 1471 | 1472 | public Long pfadd(byte[] key, byte[]... elements) { 1473 | return master.pfadd(key, elements); 1474 | } 1475 | 1476 | public long pfcount(byte[] key) { 1477 | return master.pfcount(key); 1478 | } 1479 | 1480 | public String ping() { 1481 | return master.ping(); 1482 | } 1483 | 1484 | public String quit() { 1485 | return master.quit(); 1486 | } 1487 | 1488 | public String flushDB() { 1489 | return master.flushDB(); 1490 | } 1491 | 1492 | public Long dbSize() { 1493 | return master.dbSize(); 1494 | } 1495 | 1496 | public String select(int index) { 1497 | return master.select(index); 1498 | } 1499 | 1500 | public String flushAll() { 1501 | return master.flushAll(); 1502 | } 1503 | 1504 | public String auth(String password) { 1505 | return master.auth(password); 1506 | } 1507 | 1508 | public String save() { 1509 | return master.save(); 1510 | } 1511 | 1512 | public String bgsave() { 1513 | return master.bgsave(); 1514 | } 1515 | 1516 | public String bgrewriteaof() { 1517 | return master.bgrewriteaof(); 1518 | } 1519 | 1520 | public Long lastsave() { 1521 | return master.lastsave(); 1522 | } 1523 | 1524 | public String shutdown() { 1525 | return master.shutdown(); 1526 | } 1527 | 1528 | public String info() { 1529 | return master.info(); 1530 | } 1531 | 1532 | public String info(String section) { 1533 | return master.info(section); 1534 | } 1535 | 1536 | public String slaveof(String host, int port) { 1537 | return master.slaveof(host, port); 1538 | } 1539 | 1540 | public String slaveofNoOne() { 1541 | return master.slaveofNoOne(); 1542 | } 1543 | 1544 | public Long getDB() { 1545 | return master.getDB(); 1546 | } 1547 | 1548 | public String debug(DebugParams params) { 1549 | return master.debug(params); 1550 | } 1551 | 1552 | public String configResetStat() { 1553 | return master.configResetStat(); 1554 | } 1555 | 1556 | public Long waitReplicas(int replicas, long timeout) { 1557 | return master.waitReplicas(replicas, timeout); 1558 | } 1559 | 1560 | public void close() { 1561 | if (dataSource != null) { 1562 | boolean broken = getClient().isBroken(); 1563 | if(!broken){ 1564 | for (Jedis jedis : getAllShards()) { 1565 | if (jedis.getClient().isBroken()) { 1566 | broken = true; 1567 | break; 1568 | } 1569 | } 1570 | } 1571 | if (broken) { 1572 | dataSource.returnBrokenResource(this); 1573 | } else { 1574 | dataSource.returnResource(this); 1575 | } 1576 | } else { 1577 | disconnect(); 1578 | } 1579 | } 1580 | 1581 | public void setDataSource(Pool shardedMasterSlaveJedisPool) { 1582 | this.dataSource = shardedMasterSlaveJedisPool; 1583 | } 1584 | 1585 | public void resetState() { 1586 | master.resetState(); 1587 | for (Jedis jedis : getAllShards()) { 1588 | jedis.resetState(); 1589 | } 1590 | } 1591 | 1592 | public void disconnect() { 1593 | if(master != null){ 1594 | try { 1595 | master.quit(); 1596 | } catch (JedisConnectionException e) { 1597 | // ignore the exception node, so that all other normal nodes can 1598 | // release all connections. 1599 | } 1600 | } 1601 | for (Jedis jedis : getAllShards()) { 1602 | try { 1603 | jedis.quit(); 1604 | } catch (JedisConnectionException e) { 1605 | // ignore the exception node, so that all other normal nodes can 1606 | // release all connections. 1607 | } 1608 | try { 1609 | jedis.disconnect(); 1610 | } catch (JedisConnectionException e) { 1611 | // ignore the exception node, so that all other normal nodes can 1612 | // release all connections. 1613 | } 1614 | } 1615 | } 1616 | 1617 | public String toString() { 1618 | StringBuilder sb = new StringBuilder(); 1619 | sb.append("MasterSlaveJedis {master=" + masterShard.getHost() + ":" + masterShard.getPort() + ", slaves="); 1620 | sb.append("["); 1621 | for(int i = 0, len = slaveShards.size(); i < len; i++){ 1622 | sb.append(slaveShards.get(i).getHost() + ":" + slaveShards.get(i).getPort()); 1623 | if(i != len - 1){ 1624 | sb.append(", "); 1625 | } 1626 | } 1627 | sb.append("]}"); 1628 | return sb.toString(); 1629 | } 1630 | 1631 | } 1632 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/MasterSlaveJedisSentinelPool.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.LinkedHashSet; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Set; 10 | import java.util.concurrent.atomic.AtomicBoolean; 11 | import java.util.regex.Pattern; 12 | 13 | import org.apache.commons.pool2.PooledObject; 14 | import org.apache.commons.pool2.PooledObjectFactory; 15 | import org.apache.commons.pool2.impl.DefaultPooledObject; 16 | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 17 | import org.slf4j.Logger; 18 | import org.slf4j.LoggerFactory; 19 | 20 | import redis.clients.jedis.HostAndPort; 21 | import redis.clients.jedis.Jedis; 22 | import redis.clients.jedis.JedisPubSub; 23 | import redis.clients.jedis.JedisShardInfo; 24 | import redis.clients.jedis.Protocol; 25 | import redis.clients.jedis.exceptions.JedisConnectionException; 26 | import redis.clients.util.Hashing; 27 | import redis.clients.util.Pool; 28 | 29 | public class MasterSlaveJedisSentinelPool extends Pool { 30 | 31 | private Logger logger = LoggerFactory.getLogger(MasterSlaveJedisSentinelPool.class); 32 | 33 | protected String masterName; 34 | 35 | protected Set sentinels; 36 | 37 | protected GenericObjectPoolConfig poolConfig; 38 | 39 | protected int timeout = Protocol.DEFAULT_TIMEOUT; 40 | 41 | protected String password; 42 | 43 | protected int database = Protocol.DEFAULT_DATABASE; 44 | 45 | protected volatile MasterSlaveHostAndPort currentHostMasterSlave; 46 | 47 | protected Set masterSlaveListeners = new HashSet(); 48 | 49 | private volatile MasterSlaveJedisFactory factory; 50 | 51 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig) { 52 | this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); 53 | } 54 | 55 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels) { 56 | this(masterName, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); 57 | } 58 | 59 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, String password) { 60 | this(masterName, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, password); 61 | } 62 | 63 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, int timeout, final String password) { 64 | this(masterName, sentinels, poolConfig, timeout, password, Protocol.DEFAULT_DATABASE); 65 | } 66 | 67 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, final int timeout) { 68 | this(masterName, sentinels, poolConfig, timeout, null, Protocol.DEFAULT_DATABASE); 69 | } 70 | 71 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, final String password) { 72 | this(masterName, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password); 73 | } 74 | 75 | /** 76 | * Master-Slave must be use the same config 77 | */ 78 | public MasterSlaveJedisSentinelPool(String masterName, Set sentinels, final GenericObjectPoolConfig poolConfig, int timeout, final String password, final int database) { 79 | this.masterName = masterName; 80 | this.sentinels = sentinels; 81 | this.poolConfig = poolConfig; 82 | this.timeout = timeout; 83 | this.password = password; 84 | this.database = database; 85 | 86 | initSentinelPool(); 87 | initSentinelLiseners(); 88 | } 89 | 90 | protected void initSentinelPool() { 91 | MasterSlaveHostAndPort masterSlaveHostAndPort = sentinelGetMasterSlaves(); 92 | initPool(masterSlaveHostAndPort); 93 | } 94 | 95 | protected MasterSlaveHostAndPort sentinelGetMasterSlaves() { 96 | HostAndPort master = null; 97 | Set slaves = new LinkedHashSet(); 98 | logger.info("Trying to find Master-Slaves from available sentinels..."); 99 | 100 | for (String sentinel : sentinels) { 101 | final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); 102 | logger.info("Connecting to sentinel " + hap); 103 | 104 | Jedis jedis = null; 105 | try { 106 | jedis = new Jedis(hap.getHost(), hap.getPort()); 107 | 108 | List masterAddr = jedis.sentinelGetMasterAddrByName(masterName); 109 | List> slaveAddrs = jedis.sentinelSlaves(masterName); 110 | 111 | if (masterAddr == null || masterAddr.size() != 2) { 112 | logger.warn("Can not get master addr, master name: {}. Sentinel: {}.", masterName, hap); 113 | continue; 114 | } 115 | 116 | if (slaveAddrs == null || slaveAddrs.isEmpty()) { 117 | logger.warn("Can not get slave addr, master name: {}. Sentinel: {}.", masterName, hap); 118 | continue; 119 | } 120 | 121 | master = toHostAndPort(masterAddr); 122 | 123 | for(Map slave : slaveAddrs){ 124 | if("slave".equals(slave.get("flags"))){ //is normal worked slave at now 125 | slaves.add(toHostAndPort(Arrays.asList(slave.get("ip"), slave.get("port")))); 126 | } 127 | } 128 | MasterSlaveHostAndPort masterSlaveHostAndPort = new MasterSlaveHostAndPort(masterName, master, slaves); 129 | logger.info("Found Master-Slaves : {}", masterSlaveHostAndPort); 130 | return masterSlaveHostAndPort; 131 | } catch (JedisConnectionException e) { 132 | logger.warn("Cannot connect to sentinel running @ {}. Trying next one.", hap); 133 | } finally { 134 | if (jedis != null) { 135 | jedis.close(); 136 | } 137 | } 138 | } 139 | return null; 140 | } 141 | 142 | protected void initSentinelLiseners() { 143 | if(currentHostMasterSlave != null){ 144 | logger.info("Starting Sentinel listeners..."); 145 | for (String sentinel : sentinels) { 146 | try { 147 | final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); 148 | MasterSlaveListener masterSlaveListener = new MasterSlaveListener(masterName, hap.getHost(), hap.getPort()); 149 | masterSlaveListeners.add(masterSlaveListener); 150 | masterSlaveListener.start(); 151 | } catch(Exception e) { 152 | logger.error("Starting Starting Sentinel listeners caught a exception: " + e.getMessage(), e); 153 | } 154 | } 155 | } 156 | } 157 | 158 | protected synchronized void initPool(MasterSlaveHostAndPort masterSlaveHostAndPort){ 159 | if(masterSlaveHostAndPort != null && !masterSlaveHostAndPort.equals(currentHostMasterSlave)){ 160 | currentHostMasterSlave = masterSlaveHostAndPort; 161 | 162 | JedisShardInfo masterShard = toJedisShardInfo(masterSlaveHostAndPort.getMaster(), masterSlaveHostAndPort.getMasterName()); 163 | List slaveShards = new ArrayList(); 164 | for(HostAndPort slave : masterSlaveHostAndPort.getSlaves()){ 165 | JedisShardInfo slaveShard = toJedisShardInfo(slave, null); 166 | slaveShards.add(slaveShard); 167 | } 168 | if(factory == null){ 169 | factory = new MasterSlaveJedisFactory(masterShard, slaveShards, Hashing.MURMUR_HASH, null); 170 | initPool(poolConfig, factory); 171 | }else{ 172 | factory.setMasterShard(masterShard); 173 | factory.setSlaveShards(slaveShards); 174 | // although we clear the pool, we still have to check the 175 | // returned object 176 | // in getResource, this call only clears idle instances, not 177 | // borrowed instances 178 | internalPool.clear(); 179 | } 180 | 181 | logger.info("Create Master-Slaves jedis pool for {}", currentHostMasterSlave); 182 | } 183 | } 184 | 185 | protected JedisShardInfo toJedisShardInfo(HostAndPort hostAndPort, String name) { 186 | JedisShardInfo shard = new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort(), timeout, name); 187 | shard.setPassword(password); 188 | return shard; 189 | } 190 | 191 | public MasterSlaveJedis getResource() { 192 | while (true) { 193 | MasterSlaveJedis masterSlaveJedis = super.getResource(); 194 | masterSlaveJedis.setDataSource(this); 195 | 196 | // get a reference because it can change concurrently 197 | final MasterSlaveHostAndPort reference = currentHostMasterSlave; 198 | final MasterSlaveHostAndPort connectionDesc = masterSlaveJedis.getConnectionDesc(); 199 | if (connectionDesc.equals(reference)) { 200 | // connected to the correct master 201 | return masterSlaveJedis; 202 | } else { 203 | returnBrokenResource(masterSlaveJedis); 204 | } 205 | } 206 | } 207 | 208 | public void returnBrokenResource(final MasterSlaveJedis resource) { 209 | if (resource != null) { 210 | returnBrokenResourceObject(resource); 211 | } 212 | } 213 | 214 | public void returnResource(final MasterSlaveJedis resource) { 215 | if (resource != null) { 216 | // get a reference because it can change concurrently 217 | final MasterSlaveHostAndPort reference = currentHostMasterSlave; 218 | final MasterSlaveHostAndPort connectionDesc = resource.getConnectionDesc(); 219 | if (connectionDesc.equals(reference)) { 220 | // connected to the correct master 221 | resource.resetState(); 222 | returnResourceObject(resource); 223 | } else { 224 | returnBrokenResource(resource); 225 | } 226 | } 227 | } 228 | 229 | public void destroy() { 230 | for (MasterSlaveListener m : masterSlaveListeners) { 231 | m.shutdown(); 232 | } 233 | super.destroy(); 234 | } 235 | 236 | public MasterSlaveHostAndPort getCurrentHostMasterSlave() { 237 | return currentHostMasterSlave; 238 | } 239 | 240 | protected HostAndPort toHostAndPort(List hostAndPort){ 241 | return new HostAndPort(hostAndPort.get(0), Integer.parseInt(hostAndPort.get(1))); 242 | } 243 | 244 | protected class MasterSlaveListener extends Thread { 245 | 246 | protected String masterName; 247 | protected String host; 248 | protected int port; 249 | protected long subscribeRetryWaitTimeMillis = 5000; 250 | protected Jedis sentinelJedis; 251 | protected AtomicBoolean running = new AtomicBoolean(false); 252 | 253 | protected MasterSlaveListener() { 254 | } 255 | 256 | public MasterSlaveListener(String masterName, String host, int port) { 257 | this.masterName = masterName; 258 | this.host = host; 259 | this.port = port; 260 | } 261 | 262 | public MasterSlaveListener(String masterName, String host, int port, long subscribeRetryWaitTimeMillis) { 263 | this(masterName, host, port); 264 | this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis; 265 | } 266 | 267 | public void run() { 268 | running.set(true); 269 | while (running.get()) { 270 | logger.debug(">>> Runing!"); 271 | sentinelJedis = new Jedis(host, port); 272 | try { 273 | sentinelJedis.subscribe(new JedisPubSub() { 274 | public void onMessage(String channel, String message) { 275 | logger.info("Sentinel {} published: {} {}", host + ":" + port, channel, message); 276 | if("+sdown".equals(channel)){ //has slave offline 277 | String[] messages = message.split(" "); 278 | if(messages.length == 8){ 279 | if("slave".equals(messages[0])){ 280 | if(masterName.equals(messages[5])){ 281 | String slaveIp = messages[2]; 282 | String slavePort = messages[3]; 283 | String masterIp = messages[6]; 284 | String masterPort = messages[7]; 285 | logger.error("Found unavailable redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, masterName, masterIp + ":" + masterPort); 286 | initSentinelPool(); 287 | }else{ 288 | logger.error("Ignoring message on +sdown for master name {}, but our master name is {}!", messages[5], masterName); 289 | } 290 | }else{ 291 | logger.error("Invalid message received on Sentinel {} on channel +sdown: {}", host + ":" + port, message); 292 | } 293 | } 294 | } 295 | if("-sdown".equals(channel)){ //has slave online 296 | String[] messages = message.split(" "); 297 | if(messages.length == 8){ 298 | if("slave".equals(messages[0])){ 299 | if(masterName.equals(messages[5])){ 300 | String slaveIp = messages[2]; 301 | String slavePort = messages[3]; 302 | String masterIp = messages[6]; 303 | String masterPort = messages[7]; 304 | logger.info("Found available redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, masterName, masterIp + ":" + masterPort); 305 | initSentinelPool(); 306 | }else{ 307 | logger.error("Ignoring message on -sdown for master name {}, but our master name is {}!", messages[5], masterName); 308 | } 309 | }else{ 310 | logger.error("Invalid message received on Sentinel {} on channel -sdown: {}", host + ":" + port, message); 311 | } 312 | } 313 | } 314 | if("+switch-master".equals(channel)){ //master has been switched 315 | String[] messages = message.split(" "); 316 | if(messages.length == 5){ 317 | if(masterName.equals(messages[0])){ 318 | String oldMasterIp = messages[1]; 319 | String oldMasterPort = messages[2]; 320 | String newMasterIp = messages[3]; 321 | String newMasterPort = messages[4]; 322 | logger.info("Switch master {} from [{}] to [{}]", masterName, oldMasterIp + ":" + oldMasterPort, newMasterIp + ":" + newMasterPort); 323 | initSentinelPool(); 324 | }else{ 325 | logger.error("Ignoring message on +switch-master for master name {}, but our master name is {}!", messages[5], masterName); 326 | } 327 | }else{ 328 | logger.error("Invalid message received on Sentinel {} on channel +switch-master: {}", host + ":" + port, message); 329 | } 330 | } 331 | if("+slave".equals(channel)){ //has new slave joined 332 | String[] messages = message.split(" "); 333 | if(messages.length == 8){ 334 | if("slave".equals(messages[0])){ 335 | if(masterName.equals(messages[5])){ 336 | String slaveIp = messages[2]; 337 | String slavePort = messages[3]; 338 | String masterIp = messages[6]; 339 | String masterPort = messages[7]; 340 | logger.error("Found available redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, masterName, masterIp + ":" + masterPort); 341 | initSentinelPool(); 342 | }else{ 343 | logger.error("Ignoring message on +slave for master name {}, but our master name is {}!", messages[5], masterName); 344 | } 345 | }else{ 346 | logger.error("Invalid message received on Sentinel {} on channel +slave: {}", host + ":" + port, message); 347 | } 348 | } 349 | } 350 | } 351 | }, "+switch-master", "+sdown", "-sdown", "+slave"); 352 | } catch (JedisConnectionException e) { 353 | if (running.get()) { 354 | logger.error("Lost connection to Sentinel at {}. Sleeping {}ms and retrying.", host + ":" + port, subscribeRetryWaitTimeMillis); 355 | try { 356 | Thread.sleep(subscribeRetryWaitTimeMillis); 357 | } catch (InterruptedException e1) { 358 | e1.printStackTrace(); 359 | } 360 | } else { 361 | logger.error("Listener stop running and unsubscribing from Sentinel at {}.", host + ":" + port); 362 | } 363 | } 364 | } 365 | } 366 | 367 | public void shutdown() { 368 | try { 369 | logger.info("Shutting down listener on {}.", host + ":" + port); 370 | running.set(false); 371 | // This isn't good, the Jedis object is not thread safe 372 | sentinelJedis.disconnect(); 373 | } catch (Exception e) { 374 | logger.error("Caught exception while shutting down: " + e.getMessage()); 375 | } 376 | } 377 | } 378 | 379 | /** 380 | * MasterSlaveJedis工厂类 381 | * 382 | * @author pengpeng 383 | * @date 2015年3月14日 上午10:09:00 384 | * @version 1.0 385 | */ 386 | protected static class MasterSlaveJedisFactory implements PooledObjectFactory { 387 | private JedisShardInfo masterShard; 388 | private List slaveShards; 389 | private Hashing algo; 390 | private Pattern keyTagPattern; 391 | 392 | public MasterSlaveJedisFactory(JedisShardInfo masterShard, List slaveShards, Hashing algo, Pattern keyTagPattern) { 393 | this.masterShard = masterShard; 394 | this.slaveShards = slaveShards; 395 | this.algo = algo; 396 | this.keyTagPattern = keyTagPattern; 397 | } 398 | 399 | public void setMasterShard(JedisShardInfo masterShard) { 400 | this.masterShard = masterShard; 401 | } 402 | 403 | public void setSlaveShards(List slaveShards) { 404 | this.slaveShards = slaveShards; 405 | } 406 | 407 | public PooledObject makeObject() throws Exception { 408 | MasterSlaveJedis masterSlaveJedis = new MasterSlaveJedis(masterShard, slaveShards, algo, keyTagPattern); 409 | return new DefaultPooledObject(masterSlaveJedis); 410 | } 411 | 412 | public void destroyObject(PooledObject pooledMasterSlaveJedis) 413 | throws Exception { 414 | pooledMasterSlaveJedis.getObject().disconnect(); 415 | } 416 | 417 | public boolean validateObject(PooledObject pooledMasterSlaveJedis) { 418 | try { 419 | MasterSlaveJedis masterSlaveJedis = pooledMasterSlaveJedis.getObject(); 420 | if(!"PONG".equals(masterSlaveJedis.ping())){ 421 | return false; 422 | } 423 | for (Jedis slaveJedis : masterSlaveJedis.getAllShards()) { 424 | if (!"PONG".equals(slaveJedis.ping())) { 425 | return false; 426 | } 427 | } 428 | return true; 429 | } catch (Exception ex) { 430 | return false; 431 | } 432 | } 433 | 434 | public void activateObject(PooledObject p) throws Exception { 435 | 436 | } 437 | 438 | public void passivateObject(PooledObject p) throws Exception { 439 | 440 | } 441 | } 442 | 443 | } 444 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/MasterSlaveJedisShardInfo.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.util.List; 4 | 5 | import redis.clients.jedis.JedisShardInfo; 6 | import redis.clients.util.Hashing; 7 | import redis.clients.util.ShardInfo; 8 | import redis.clients.util.Sharded; 9 | 10 | public class MasterSlaveJedisShardInfo extends ShardInfo { 11 | 12 | private final String masterName; 13 | 14 | private final JedisShardInfo masterShard; 15 | 16 | private final List slaveShards; 17 | 18 | private final String name; 19 | 20 | public MasterSlaveJedisShardInfo(String masterName, JedisShardInfo masterShard, List slaveShards) { 21 | this(masterName, masterShard, slaveShards, Sharded.DEFAULT_WEIGHT); 22 | } 23 | 24 | public MasterSlaveJedisShardInfo(String masterName, JedisShardInfo masterShard, List slaveShards, int weight) { 25 | this(masterName, masterShard, slaveShards, Sharded.DEFAULT_WEIGHT, null); 26 | } 27 | 28 | public MasterSlaveJedisShardInfo(String masterName, JedisShardInfo masterShard, List slaveShards, int weight, String name) { 29 | super(weight); 30 | this.masterName = masterName; 31 | this.masterShard = masterShard; 32 | this.slaveShards = slaveShards; 33 | this.name = name; 34 | } 35 | 36 | protected MasterSlaveJedis createResource() { 37 | return new MasterSlaveJedis(masterShard, slaveShards, Hashing.MURMUR_HASH, null); 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public String getMasterName() { 45 | return masterName; 46 | } 47 | 48 | public JedisShardInfo getMasterShard() { 49 | return masterShard; 50 | } 51 | 52 | public List getSlaveShards() { 53 | return slaveShards; 54 | } 55 | 56 | public String toString() { 57 | StringBuilder sb = new StringBuilder(); 58 | sb.append("{masterName=" + masterName + ", master=" + masterShard.getHost() + ":" + masterShard.getPort() + ", slaves="); 59 | sb.append("["); 60 | for(int i = 0, len = slaveShards.size(); i < len; i++){ 61 | sb.append(slaveShards.get(i).getHost() + ":" + slaveShards.get(i).getPort()); 62 | if(i != len - 1){ 63 | sb.append(", "); 64 | } 65 | } 66 | sb.append("]}"); 67 | return sb.toString(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/ShardedMasterSlaveJedis.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.io.Closeable; 4 | import java.util.Collection; 5 | import java.util.LinkedHashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Map.Entry; 9 | import java.util.Set; 10 | import java.util.regex.Pattern; 11 | 12 | import redis.clients.jedis.BinaryClient.LIST_POSITION; 13 | import redis.clients.jedis.BinaryJedisCommands; 14 | import redis.clients.jedis.HostAndPort; 15 | import redis.clients.jedis.JedisCommands; 16 | import redis.clients.jedis.JedisShardInfo; 17 | import redis.clients.jedis.ScanResult; 18 | import redis.clients.jedis.SortingParams; 19 | import redis.clients.jedis.Tuple; 20 | import redis.clients.util.Hashing; 21 | import redis.clients.util.Pool; 22 | import redis.clients.util.Sharded; 23 | 24 | public class ShardedMasterSlaveJedis extends 25 | Sharded implements 26 | JedisCommands, BinaryJedisCommands, Closeable { 27 | 28 | protected final Set connectionDesc; 29 | 30 | protected Pool dataSource = null; 31 | 32 | protected final List shards; 33 | 34 | public ShardedMasterSlaveJedis(List shards) { 35 | super(shards); 36 | this.shards = shards; 37 | this.connectionDesc = new LinkedHashSet(); 38 | for(MasterSlaveJedisShardInfo shard : shards){ 39 | Set slaves = new LinkedHashSet(); 40 | for(JedisShardInfo slaveShard : shard.getSlaveShards()){ 41 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 42 | } 43 | this.connectionDesc.add(new MasterSlaveHostAndPort(shard.getMasterName(), new HostAndPort(shard.getMasterShard().getHost(), shard.getMasterShard().getPort()), slaves)); 44 | } 45 | } 46 | 47 | public ShardedMasterSlaveJedis(List shards, 48 | Hashing algo) { 49 | super(shards, algo); 50 | this.shards = shards; 51 | this.connectionDesc = new LinkedHashSet(); 52 | for(MasterSlaveJedisShardInfo shard : shards){ 53 | Set slaves = new LinkedHashSet(); 54 | for(JedisShardInfo slaveShard : shard.getSlaveShards()){ 55 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 56 | } 57 | this.connectionDesc.add(new MasterSlaveHostAndPort(shard.getMasterName(), new HostAndPort(shard.getMasterShard().getHost(), shard.getMasterShard().getPort()), slaves)); 58 | } 59 | } 60 | 61 | public ShardedMasterSlaveJedis(List shards, 62 | Pattern keyTagPattern) { 63 | super(shards, keyTagPattern); 64 | this.shards = shards; 65 | this.connectionDesc = new LinkedHashSet(); 66 | for(MasterSlaveJedisShardInfo shard : shards){ 67 | Set slaves = new LinkedHashSet(); 68 | for(JedisShardInfo slaveShard : shard.getSlaveShards()){ 69 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 70 | } 71 | this.connectionDesc.add(new MasterSlaveHostAndPort(shard.getMasterName(), new HostAndPort(shard.getMasterShard().getHost(), shard.getMasterShard().getPort()), slaves)); 72 | } 73 | } 74 | 75 | public ShardedMasterSlaveJedis(List shards, 76 | Hashing algo, Pattern keyTagPattern) { 77 | super(shards, algo, keyTagPattern); 78 | this.shards = shards; 79 | this.connectionDesc = new LinkedHashSet(); 80 | for(MasterSlaveJedisShardInfo shard : shards){ 81 | Set slaves = new LinkedHashSet(); 82 | for(JedisShardInfo slaveShard : shard.getSlaveShards()){ 83 | slaves.add(new HostAndPort(slaveShard.getHost(), slaveShard.getPort())); 84 | } 85 | this.connectionDesc.add(new MasterSlaveHostAndPort(shard.getMasterName(), new HostAndPort(shard.getMasterShard().getHost(), shard.getMasterShard().getPort()), slaves)); 86 | } 87 | } 88 | 89 | public Set getConnectionDesc() { 90 | return connectionDesc; 91 | } 92 | 93 | public String set(String key, String value) { 94 | MasterSlaveJedis j = getShard(key); 95 | return j.set(key, value); 96 | } 97 | 98 | public String set(String key, String value, String nxxx, String expx, 99 | long time) { 100 | MasterSlaveJedis j = getShard(key); 101 | return j.set(key, value, nxxx, expx, time); 102 | } 103 | 104 | public String get(String key) { 105 | MasterSlaveJedis j = getShard(key); 106 | return j.get(key); 107 | } 108 | 109 | public String echo(String string) { 110 | MasterSlaveJedis j = getShard(string); 111 | return j.echo(string); 112 | } 113 | 114 | public Boolean exists(String key) { 115 | MasterSlaveJedis j = getShard(key); 116 | return j.exists(key); 117 | } 118 | 119 | public String type(String key) { 120 | MasterSlaveJedis j = getShard(key); 121 | return j.type(key); 122 | } 123 | 124 | public Long expire(String key, int seconds) { 125 | MasterSlaveJedis j = getShard(key); 126 | return j.expire(key, seconds); 127 | } 128 | 129 | public Long expireAt(String key, long unixTime) { 130 | MasterSlaveJedis j = getShard(key); 131 | return j.expireAt(key, unixTime); 132 | } 133 | 134 | public Long ttl(String key) { 135 | MasterSlaveJedis j = getShard(key); 136 | return j.ttl(key); 137 | } 138 | 139 | public Boolean setbit(String key, long offset, boolean value) { 140 | MasterSlaveJedis j = getShard(key); 141 | return j.setbit(key, offset, value); 142 | } 143 | 144 | public Boolean setbit(String key, long offset, String value) { 145 | MasterSlaveJedis j = getShard(key); 146 | return j.setbit(key, offset, value); 147 | } 148 | 149 | public Boolean getbit(String key, long offset) { 150 | MasterSlaveJedis j = getShard(key); 151 | return j.getbit(key, offset); 152 | } 153 | 154 | public Long setrange(String key, long offset, String value) { 155 | MasterSlaveJedis j = getShard(key); 156 | return j.setrange(key, offset, value); 157 | } 158 | 159 | public String getrange(String key, long startOffset, long endOffset) { 160 | MasterSlaveJedis j = getShard(key); 161 | return j.getrange(key, startOffset, endOffset); 162 | } 163 | 164 | public String getSet(String key, String value) { 165 | MasterSlaveJedis j = getShard(key); 166 | return j.getSet(key, value); 167 | } 168 | 169 | public Long setnx(String key, String value) { 170 | MasterSlaveJedis j = getShard(key); 171 | return j.setnx(key, value); 172 | } 173 | 174 | public String setex(String key, int seconds, String value) { 175 | MasterSlaveJedis j = getShard(key); 176 | return j.setex(key, seconds, value); 177 | } 178 | 179 | @Deprecated 180 | public List blpop(String arg) { 181 | MasterSlaveJedis j = getShard(arg); 182 | return j.blpop(arg); 183 | } 184 | 185 | public List blpop(int timeout, String key) { 186 | MasterSlaveJedis j = getShard(key); 187 | return j.blpop(timeout, key); 188 | } 189 | 190 | @Deprecated 191 | public List brpop(String arg) { 192 | MasterSlaveJedis j = getShard(arg); 193 | return j.brpop(arg); 194 | } 195 | 196 | public List brpop(int timeout, String key) { 197 | MasterSlaveJedis j = getShard(key); 198 | return j.brpop(timeout, key); 199 | } 200 | 201 | public Long decrBy(String key, long integer) { 202 | MasterSlaveJedis j = getShard(key); 203 | return j.decrBy(key, integer); 204 | } 205 | 206 | public Long decr(String key) { 207 | MasterSlaveJedis j = getShard(key); 208 | return j.decr(key); 209 | } 210 | 211 | public Long incrBy(String key, long integer) { 212 | MasterSlaveJedis j = getShard(key); 213 | return j.incrBy(key, integer); 214 | } 215 | 216 | public Double incrByFloat(String key, double integer) { 217 | MasterSlaveJedis j = getShard(key); 218 | return j.incrByFloat(key, integer); 219 | } 220 | 221 | public Long incr(String key) { 222 | MasterSlaveJedis j = getShard(key); 223 | return j.incr(key); 224 | } 225 | 226 | public Long append(String key, String value) { 227 | MasterSlaveJedis j = getShard(key); 228 | return j.append(key, value); 229 | } 230 | 231 | public String substr(String key, int start, int end) { 232 | MasterSlaveJedis j = getShard(key); 233 | return j.substr(key, start, end); 234 | } 235 | 236 | public Long hset(String key, String field, String value) { 237 | MasterSlaveJedis j = getShard(key); 238 | return j.hset(key, field, value); 239 | } 240 | 241 | public String hget(String key, String field) { 242 | MasterSlaveJedis j = getShard(key); 243 | return j.hget(key, field); 244 | } 245 | 246 | public Long hsetnx(String key, String field, String value) { 247 | MasterSlaveJedis j = getShard(key); 248 | return j.hsetnx(key, field, value); 249 | } 250 | 251 | public String hmset(String key, Map hash) { 252 | MasterSlaveJedis j = getShard(key); 253 | return j.hmset(key, hash); 254 | } 255 | 256 | public List hmget(String key, String... fields) { 257 | MasterSlaveJedis j = getShard(key); 258 | return j.hmget(key, fields); 259 | } 260 | 261 | public Long hincrBy(String key, String field, long value) { 262 | MasterSlaveJedis j = getShard(key); 263 | return j.hincrBy(key, field, value); 264 | } 265 | 266 | public Double hincrByFloat(String key, String field, double value) { 267 | MasterSlaveJedis j = getShard(key); 268 | return j.hincrByFloat(key, field, value); 269 | } 270 | 271 | public Boolean hexists(String key, String field) { 272 | MasterSlaveJedis j = getShard(key); 273 | return j.hexists(key, field); 274 | } 275 | 276 | public Long del(String key) { 277 | MasterSlaveJedis j = getShard(key); 278 | return j.del(key); 279 | } 280 | 281 | public Long hdel(String key, String... fields) { 282 | MasterSlaveJedis j = getShard(key); 283 | return j.hdel(key, fields); 284 | } 285 | 286 | public Long hlen(String key) { 287 | MasterSlaveJedis j = getShard(key); 288 | return j.hlen(key); 289 | } 290 | 291 | public Set hkeys(String key) { 292 | MasterSlaveJedis j = getShard(key); 293 | return j.hkeys(key); 294 | } 295 | 296 | public List hvals(String key) { 297 | MasterSlaveJedis j = getShard(key); 298 | return j.hvals(key); 299 | } 300 | 301 | public Map hgetAll(String key) { 302 | MasterSlaveJedis j = getShard(key); 303 | return j.hgetAll(key); 304 | } 305 | 306 | public Long rpush(String key, String... strings) { 307 | MasterSlaveJedis j = getShard(key); 308 | return j.rpush(key, strings); 309 | } 310 | 311 | public Long lpush(String key, String... strings) { 312 | MasterSlaveJedis j = getShard(key); 313 | return j.lpush(key, strings); 314 | } 315 | 316 | public Long lpushx(String key, String... string) { 317 | MasterSlaveJedis j = getShard(key); 318 | return j.lpushx(key, string); 319 | } 320 | 321 | public Long strlen(final String key) { 322 | MasterSlaveJedis j = getShard(key); 323 | return j.strlen(key); 324 | } 325 | 326 | public Long move(String key, int dbIndex) { 327 | MasterSlaveJedis j = getShard(key); 328 | return j.move(key, dbIndex); 329 | } 330 | 331 | public Long rpushx(String key, String... string) { 332 | MasterSlaveJedis j = getShard(key); 333 | return j.rpushx(key, string); 334 | } 335 | 336 | public Long persist(final String key) { 337 | MasterSlaveJedis j = getShard(key); 338 | return j.persist(key); 339 | } 340 | 341 | public Long llen(String key) { 342 | MasterSlaveJedis j = getShard(key); 343 | return j.llen(key); 344 | } 345 | 346 | public List lrange(String key, long start, long end) { 347 | MasterSlaveJedis j = getShard(key); 348 | return j.lrange(key, start, end); 349 | } 350 | 351 | public String ltrim(String key, long start, long end) { 352 | MasterSlaveJedis j = getShard(key); 353 | return j.ltrim(key, start, end); 354 | } 355 | 356 | public String lindex(String key, long index) { 357 | MasterSlaveJedis j = getShard(key); 358 | return j.lindex(key, index); 359 | } 360 | 361 | public String lset(String key, long index, String value) { 362 | MasterSlaveJedis j = getShard(key); 363 | return j.lset(key, index, value); 364 | } 365 | 366 | public Long lrem(String key, long count, String value) { 367 | MasterSlaveJedis j = getShard(key); 368 | return j.lrem(key, count, value); 369 | } 370 | 371 | public String lpop(String key) { 372 | MasterSlaveJedis j = getShard(key); 373 | return j.lpop(key); 374 | } 375 | 376 | public String rpop(String key) { 377 | MasterSlaveJedis j = getShard(key); 378 | return j.rpop(key); 379 | } 380 | 381 | public Long sadd(String key, String... members) { 382 | MasterSlaveJedis j = getShard(key); 383 | return j.sadd(key, members); 384 | } 385 | 386 | public Set smembers(String key) { 387 | MasterSlaveJedis j = getShard(key); 388 | return j.smembers(key); 389 | } 390 | 391 | public Long srem(String key, String... members) { 392 | MasterSlaveJedis j = getShard(key); 393 | return j.srem(key, members); 394 | } 395 | 396 | public String spop(String key) { 397 | MasterSlaveJedis j = getShard(key); 398 | return j.spop(key); 399 | } 400 | 401 | public Long scard(String key) { 402 | MasterSlaveJedis j = getShard(key); 403 | return j.scard(key); 404 | } 405 | 406 | public Boolean sismember(String key, String member) { 407 | MasterSlaveJedis j = getShard(key); 408 | return j.sismember(key, member); 409 | } 410 | 411 | public String srandmember(String key) { 412 | MasterSlaveJedis j = getShard(key); 413 | return j.srandmember(key); 414 | } 415 | 416 | public List srandmember(String key, int count) { 417 | MasterSlaveJedis j = getShard(key); 418 | return j.srandmember(key, count); 419 | } 420 | 421 | public Long zadd(String key, double score, String member) { 422 | MasterSlaveJedis j = getShard(key); 423 | return j.zadd(key, score, member); 424 | } 425 | 426 | public Long zadd(String key, Map scoreMembers) { 427 | MasterSlaveJedis j = getShard(key); 428 | return j.zadd(key, scoreMembers); 429 | } 430 | 431 | public Set zrange(String key, long start, long end) { 432 | MasterSlaveJedis j = getShard(key); 433 | return j.zrange(key, start, end); 434 | } 435 | 436 | public Long zrem(String key, String... members) { 437 | MasterSlaveJedis j = getShard(key); 438 | return j.zrem(key, members); 439 | } 440 | 441 | public Double zincrby(String key, double score, String member) { 442 | MasterSlaveJedis j = getShard(key); 443 | return j.zincrby(key, score, member); 444 | } 445 | 446 | public Long zrank(String key, String member) { 447 | MasterSlaveJedis j = getShard(key); 448 | return j.zrank(key, member); 449 | } 450 | 451 | public Long zrevrank(String key, String member) { 452 | MasterSlaveJedis j = getShard(key); 453 | return j.zrevrank(key, member); 454 | } 455 | 456 | public Set zrevrange(String key, long start, long end) { 457 | MasterSlaveJedis j = getShard(key); 458 | return j.zrevrange(key, start, end); 459 | } 460 | 461 | public Set zrangeWithScores(String key, long start, long end) { 462 | MasterSlaveJedis j = getShard(key); 463 | return j.zrangeWithScores(key, start, end); 464 | } 465 | 466 | public Set zrevrangeWithScores(String key, long start, long end) { 467 | MasterSlaveJedis j = getShard(key); 468 | return j.zrevrangeWithScores(key, start, end); 469 | } 470 | 471 | public Long zcard(String key) { 472 | MasterSlaveJedis j = getShard(key); 473 | return j.zcard(key); 474 | } 475 | 476 | public Double zscore(String key, String member) { 477 | MasterSlaveJedis j = getShard(key); 478 | return j.zscore(key, member); 479 | } 480 | 481 | public List sort(String key) { 482 | MasterSlaveJedis j = getShard(key); 483 | return j.sort(key); 484 | } 485 | 486 | public List sort(String key, SortingParams sortingParameters) { 487 | MasterSlaveJedis j = getShard(key); 488 | return j.sort(key, sortingParameters); 489 | } 490 | 491 | public Long zcount(String key, double min, double max) { 492 | MasterSlaveJedis j = getShard(key); 493 | return j.zcount(key, min, max); 494 | } 495 | 496 | public Long zcount(String key, String min, String max) { 497 | MasterSlaveJedis j = getShard(key); 498 | return j.zcount(key, min, max); 499 | } 500 | 501 | public Set zrangeByScore(String key, double min, double max) { 502 | MasterSlaveJedis j = getShard(key); 503 | return j.zrangeByScore(key, min, max); 504 | } 505 | 506 | public Set zrevrangeByScore(String key, double max, double min) { 507 | MasterSlaveJedis j = getShard(key); 508 | return j.zrevrangeByScore(key, max, min); 509 | } 510 | 511 | public Set zrangeByScore(String key, double min, double max, 512 | int offset, int count) { 513 | MasterSlaveJedis j = getShard(key); 514 | return j.zrangeByScore(key, min, max, offset, count); 515 | } 516 | 517 | public Set zrevrangeByScore(String key, double max, double min, 518 | int offset, int count) { 519 | MasterSlaveJedis j = getShard(key); 520 | return j.zrevrangeByScore(key, max, min, offset, count); 521 | } 522 | 523 | public Set zrangeByScoreWithScores(String key, double min, double max) { 524 | MasterSlaveJedis j = getShard(key); 525 | return j.zrangeByScoreWithScores(key, min, max); 526 | } 527 | 528 | public Set zrevrangeByScoreWithScores(String key, double max, 529 | double min) { 530 | MasterSlaveJedis j = getShard(key); 531 | return j.zrevrangeByScoreWithScores(key, max, min); 532 | } 533 | 534 | public Set zrangeByScoreWithScores(String key, double min, 535 | double max, int offset, int count) { 536 | MasterSlaveJedis j = getShard(key); 537 | return j.zrangeByScoreWithScores(key, min, max, offset, count); 538 | } 539 | 540 | public Set zrevrangeByScoreWithScores(String key, double max, 541 | double min, int offset, int count) { 542 | MasterSlaveJedis j = getShard(key); 543 | return j.zrevrangeByScoreWithScores(key, max, min, offset, count); 544 | } 545 | 546 | public Set zrangeByScore(String key, String min, String max) { 547 | MasterSlaveJedis j = getShard(key); 548 | return j.zrangeByScore(key, min, max); 549 | } 550 | 551 | public Set zrevrangeByScore(String key, String max, String min) { 552 | MasterSlaveJedis j = getShard(key); 553 | return j.zrevrangeByScore(key, max, min); 554 | } 555 | 556 | public Set zrangeByScore(String key, String min, String max, 557 | int offset, int count) { 558 | MasterSlaveJedis j = getShard(key); 559 | return j.zrangeByScore(key, min, max, offset, count); 560 | } 561 | 562 | public Set zrevrangeByScore(String key, String max, String min, 563 | int offset, int count) { 564 | MasterSlaveJedis j = getShard(key); 565 | return j.zrevrangeByScore(key, max, min, offset, count); 566 | } 567 | 568 | public Set zrangeByScoreWithScores(String key, String min, String max) { 569 | MasterSlaveJedis j = getShard(key); 570 | return j.zrangeByScoreWithScores(key, min, max); 571 | } 572 | 573 | public Set zrevrangeByScoreWithScores(String key, String max, 574 | String min) { 575 | MasterSlaveJedis j = getShard(key); 576 | return j.zrevrangeByScoreWithScores(key, max, min); 577 | } 578 | 579 | public Set zrangeByScoreWithScores(String key, String min, 580 | String max, int offset, int count) { 581 | MasterSlaveJedis j = getShard(key); 582 | return j.zrangeByScoreWithScores(key, min, max, offset, count); 583 | } 584 | 585 | public Set zrevrangeByScoreWithScores(String key, String max, 586 | String min, int offset, int count) { 587 | MasterSlaveJedis j = getShard(key); 588 | return j.zrevrangeByScoreWithScores(key, max, min, offset, count); 589 | } 590 | 591 | public Long zremrangeByRank(String key, long start, long end) { 592 | MasterSlaveJedis j = getShard(key); 593 | return j.zremrangeByRank(key, start, end); 594 | } 595 | 596 | public Long zremrangeByScore(String key, double start, double end) { 597 | MasterSlaveJedis j = getShard(key); 598 | return j.zremrangeByScore(key, start, end); 599 | } 600 | 601 | public Long zremrangeByScore(String key, String start, String end) { 602 | MasterSlaveJedis j = getShard(key); 603 | return j.zremrangeByScore(key, start, end); 604 | } 605 | 606 | public Long zlexcount(final String key, final String min, final String max) { 607 | return getShard(key).zlexcount(key, min, max); 608 | } 609 | 610 | public Set zrangeByLex(final String key, final String min, 611 | final String max) { 612 | return getShard(key).zrangeByLex(key, min, max); 613 | } 614 | 615 | public Set zrangeByLex(final String key, final String min, 616 | final String max, final int offset, final int count) { 617 | return getShard(key).zrangeByLex(key, min, max, offset, count); 618 | } 619 | 620 | public Long zremrangeByLex(final String key, final String min, 621 | final String max) { 622 | return getShard(key).zremrangeByLex(key, min, max); 623 | } 624 | 625 | public Long linsert(String key, LIST_POSITION where, String pivot, 626 | String value) { 627 | MasterSlaveJedis j = getShard(key); 628 | return j.linsert(key, where, pivot, value); 629 | } 630 | 631 | public Long bitcount(final String key) { 632 | MasterSlaveJedis j = getShard(key); 633 | return j.bitcount(key); 634 | } 635 | 636 | public Long bitcount(final String key, long start, long end) { 637 | MasterSlaveJedis j = getShard(key); 638 | return j.bitcount(key, start, end); 639 | } 640 | 641 | @Deprecated 642 | /** 643 | * This method is deprecated due to bug (scan cursor should be unsigned long) 644 | * And will be removed on next major release 645 | * @see https://github.com/xetorthio/jedis/issues/531 646 | */ 647 | public ScanResult> hscan(String key, int cursor) { 648 | MasterSlaveJedis j = getShard(key); 649 | return j.hscan(key, cursor); 650 | } 651 | 652 | @Deprecated 653 | /** 654 | * This method is deprecated due to bug (scan cursor should be unsigned long) 655 | * And will be removed on next major release 656 | * @see https://github.com/xetorthio/jedis/issues/531 657 | */ 658 | public ScanResult sscan(String key, int cursor) { 659 | MasterSlaveJedis j = getShard(key); 660 | return j.sscan(key, cursor); 661 | } 662 | 663 | @Deprecated 664 | /** 665 | * This method is deprecated due to bug (scan cursor should be unsigned long) 666 | * And will be removed on next major release 667 | * @see https://github.com/xetorthio/jedis/issues/531 668 | */ 669 | public ScanResult zscan(String key, int cursor) { 670 | MasterSlaveJedis j = getShard(key); 671 | return j.zscan(key, cursor); 672 | } 673 | 674 | public ScanResult> hscan(String key, 675 | final String cursor) { 676 | MasterSlaveJedis j = getShard(key); 677 | return j.hscan(key, cursor); 678 | } 679 | 680 | public ScanResult sscan(String key, final String cursor) { 681 | MasterSlaveJedis j = getShard(key); 682 | return j.sscan(key, cursor); 683 | } 684 | 685 | public ScanResult zscan(String key, final String cursor) { 686 | MasterSlaveJedis j = getShard(key); 687 | return j.zscan(key, cursor); 688 | } 689 | 690 | public Long pfadd(String key, String... elements) { 691 | MasterSlaveJedis j = getShard(key); 692 | return j.pfadd(key, elements); 693 | } 694 | 695 | public long pfcount(String key) { 696 | MasterSlaveJedis j = getShard(key); 697 | return j.pfcount(key); 698 | } 699 | 700 | public String set(byte[] key, byte[] value) { 701 | MasterSlaveJedis j = getShard(key); 702 | return j.set(key, value); 703 | } 704 | 705 | public byte[] get(byte[] key) { 706 | MasterSlaveJedis j = getShard(key); 707 | return j.get(key); 708 | } 709 | 710 | public Boolean exists(byte[] key) { 711 | MasterSlaveJedis j = getShard(key); 712 | return j.exists(key); 713 | } 714 | 715 | public String type(byte[] key) { 716 | MasterSlaveJedis j = getShard(key); 717 | return j.type(key); 718 | } 719 | 720 | public Long expire(byte[] key, int seconds) { 721 | MasterSlaveJedis j = getShard(key); 722 | return j.expire(key, seconds); 723 | } 724 | 725 | public Long expireAt(byte[] key, long unixTime) { 726 | MasterSlaveJedis j = getShard(key); 727 | return j.expireAt(key, unixTime); 728 | } 729 | 730 | public Long ttl(byte[] key) { 731 | MasterSlaveJedis j = getShard(key); 732 | return j.ttl(key); 733 | } 734 | 735 | public byte[] getSet(byte[] key, byte[] value) { 736 | MasterSlaveJedis j = getShard(key); 737 | return j.getSet(key, value); 738 | } 739 | 740 | public Long setnx(byte[] key, byte[] value) { 741 | MasterSlaveJedis j = getShard(key); 742 | return j.setnx(key, value); 743 | } 744 | 745 | public String setex(byte[] key, int seconds, byte[] value) { 746 | MasterSlaveJedis j = getShard(key); 747 | return j.setex(key, seconds, value); 748 | } 749 | 750 | public Long decrBy(byte[] key, long integer) { 751 | MasterSlaveJedis j = getShard(key); 752 | return j.decrBy(key, integer); 753 | } 754 | 755 | public Long decr(byte[] key) { 756 | MasterSlaveJedis j = getShard(key); 757 | return j.decr(key); 758 | } 759 | 760 | public Long del(byte[] key) { 761 | MasterSlaveJedis j = getShard(key); 762 | return j.del(key); 763 | } 764 | 765 | public Long incrBy(byte[] key, long integer) { 766 | MasterSlaveJedis j = getShard(key); 767 | return j.incrBy(key, integer); 768 | } 769 | 770 | public Double incrByFloat(byte[] key, double integer) { 771 | MasterSlaveJedis j = getShard(key); 772 | return j.incrByFloat(key, integer); 773 | } 774 | 775 | public Long incr(byte[] key) { 776 | MasterSlaveJedis j = getShard(key); 777 | return j.incr(key); 778 | } 779 | 780 | public Long append(byte[] key, byte[] value) { 781 | MasterSlaveJedis j = getShard(key); 782 | return j.append(key, value); 783 | } 784 | 785 | public byte[] substr(byte[] key, int start, int end) { 786 | MasterSlaveJedis j = getShard(key); 787 | return j.substr(key, start, end); 788 | } 789 | 790 | public Long hset(byte[] key, byte[] field, byte[] value) { 791 | MasterSlaveJedis j = getShard(key); 792 | return j.hset(key, field, value); 793 | } 794 | 795 | public byte[] hget(byte[] key, byte[] field) { 796 | MasterSlaveJedis j = getShard(key); 797 | return j.hget(key, field); 798 | } 799 | 800 | public Long hsetnx(byte[] key, byte[] field, byte[] value) { 801 | MasterSlaveJedis j = getShard(key); 802 | return j.hsetnx(key, field, value); 803 | } 804 | 805 | public String hmset(byte[] key, Map hash) { 806 | MasterSlaveJedis j = getShard(key); 807 | return j.hmset(key, hash); 808 | } 809 | 810 | public List hmget(byte[] key, byte[]... fields) { 811 | MasterSlaveJedis j = getShard(key); 812 | return j.hmget(key, fields); 813 | } 814 | 815 | public Long hincrBy(byte[] key, byte[] field, long value) { 816 | MasterSlaveJedis j = getShard(key); 817 | return j.hincrBy(key, field, value); 818 | } 819 | 820 | public Double hincrByFloat(byte[] key, byte[] field, double value) { 821 | MasterSlaveJedis j = getShard(key); 822 | return j.hincrByFloat(key, field, value); 823 | } 824 | 825 | public Boolean hexists(byte[] key, byte[] field) { 826 | MasterSlaveJedis j = getShard(key); 827 | return j.hexists(key, field); 828 | } 829 | 830 | public Long hdel(byte[] key, byte[]... fields) { 831 | MasterSlaveJedis j = getShard(key); 832 | return j.hdel(key, fields); 833 | } 834 | 835 | public Long hlen(byte[] key) { 836 | MasterSlaveJedis j = getShard(key); 837 | return j.hlen(key); 838 | } 839 | 840 | public Set hkeys(byte[] key) { 841 | MasterSlaveJedis j = getShard(key); 842 | return j.hkeys(key); 843 | } 844 | 845 | public Collection hvals(byte[] key) { 846 | MasterSlaveJedis j = getShard(key); 847 | return j.hvals(key); 848 | } 849 | 850 | public Map hgetAll(byte[] key) { 851 | MasterSlaveJedis j = getShard(key); 852 | return j.hgetAll(key); 853 | } 854 | 855 | public Long rpush(byte[] key, byte[]... strings) { 856 | MasterSlaveJedis j = getShard(key); 857 | return j.rpush(key, strings); 858 | } 859 | 860 | public Long lpush(byte[] key, byte[]... strings) { 861 | MasterSlaveJedis j = getShard(key); 862 | return j.lpush(key, strings); 863 | } 864 | 865 | public Long strlen(final byte[] key) { 866 | MasterSlaveJedis j = getShard(key); 867 | return j.strlen(key); 868 | } 869 | 870 | public Long lpushx(byte[] key, byte[]... string) { 871 | MasterSlaveJedis j = getShard(key); 872 | return j.lpushx(key, string); 873 | } 874 | 875 | public Long persist(final byte[] key) { 876 | MasterSlaveJedis j = getShard(key); 877 | return j.persist(key); 878 | } 879 | 880 | public Long rpushx(byte[] key, byte[]... string) { 881 | MasterSlaveJedis j = getShard(key); 882 | return j.rpushx(key, string); 883 | } 884 | 885 | public Long llen(byte[] key) { 886 | MasterSlaveJedis j = getShard(key); 887 | return j.llen(key); 888 | } 889 | 890 | public List lrange(byte[] key, long start, long end) { 891 | MasterSlaveJedis j = getShard(key); 892 | return j.lrange(key, start, end); 893 | } 894 | 895 | public String ltrim(byte[] key, long start, long end) { 896 | MasterSlaveJedis j = getShard(key); 897 | return j.ltrim(key, start, end); 898 | } 899 | 900 | public byte[] lindex(byte[] key, long index) { 901 | MasterSlaveJedis j = getShard(key); 902 | return j.lindex(key, index); 903 | } 904 | 905 | public String lset(byte[] key, long index, byte[] value) { 906 | MasterSlaveJedis j = getShard(key); 907 | return j.lset(key, index, value); 908 | } 909 | 910 | public Long lrem(byte[] key, long count, byte[] value) { 911 | MasterSlaveJedis j = getShard(key); 912 | return j.lrem(key, count, value); 913 | } 914 | 915 | public byte[] lpop(byte[] key) { 916 | MasterSlaveJedis j = getShard(key); 917 | return j.lpop(key); 918 | } 919 | 920 | public byte[] rpop(byte[] key) { 921 | MasterSlaveJedis j = getShard(key); 922 | return j.rpop(key); 923 | } 924 | 925 | public Long sadd(byte[] key, byte[]... members) { 926 | MasterSlaveJedis j = getShard(key); 927 | return j.sadd(key, members); 928 | } 929 | 930 | public Set smembers(byte[] key) { 931 | MasterSlaveJedis j = getShard(key); 932 | return j.smembers(key); 933 | } 934 | 935 | public Long srem(byte[] key, byte[]... members) { 936 | MasterSlaveJedis j = getShard(key); 937 | return j.srem(key, members); 938 | } 939 | 940 | public byte[] spop(byte[] key) { 941 | MasterSlaveJedis j = getShard(key); 942 | return j.spop(key); 943 | } 944 | 945 | public Long scard(byte[] key) { 946 | MasterSlaveJedis j = getShard(key); 947 | return j.scard(key); 948 | } 949 | 950 | public Boolean sismember(byte[] key, byte[] member) { 951 | MasterSlaveJedis j = getShard(key); 952 | return j.sismember(key, member); 953 | } 954 | 955 | public byte[] srandmember(byte[] key) { 956 | MasterSlaveJedis j = getShard(key); 957 | return j.srandmember(key); 958 | } 959 | 960 | public List srandmember(byte[] key, int count) { 961 | MasterSlaveJedis j = getShard(key); 962 | return j.srandmember(key, count); 963 | } 964 | 965 | public Long zadd(byte[] key, double score, byte[] member) { 966 | MasterSlaveJedis j = getShard(key); 967 | return j.zadd(key, score, member); 968 | } 969 | 970 | public Long zadd(byte[] key, Map scoreMembers) { 971 | MasterSlaveJedis j = getShard(key); 972 | return j.zadd(key, scoreMembers); 973 | } 974 | 975 | public Set zrange(byte[] key, long start, long end) { 976 | MasterSlaveJedis j = getShard(key); 977 | return j.zrange(key, start, end); 978 | } 979 | 980 | public Long zrem(byte[] key, byte[]... members) { 981 | MasterSlaveJedis j = getShard(key); 982 | return j.zrem(key, members); 983 | } 984 | 985 | public Double zincrby(byte[] key, double score, byte[] member) { 986 | MasterSlaveJedis j = getShard(key); 987 | return j.zincrby(key, score, member); 988 | } 989 | 990 | public Long zrank(byte[] key, byte[] member) { 991 | MasterSlaveJedis j = getShard(key); 992 | return j.zrank(key, member); 993 | } 994 | 995 | public Long zrevrank(byte[] key, byte[] member) { 996 | MasterSlaveJedis j = getShard(key); 997 | return j.zrevrank(key, member); 998 | } 999 | 1000 | public Set zrevrange(byte[] key, long start, long end) { 1001 | MasterSlaveJedis j = getShard(key); 1002 | return j.zrevrange(key, start, end); 1003 | } 1004 | 1005 | public Set zrangeWithScores(byte[] key, long start, long end) { 1006 | MasterSlaveJedis j = getShard(key); 1007 | return j.zrangeWithScores(key, start, end); 1008 | } 1009 | 1010 | public Set zrevrangeWithScores(byte[] key, long start, long end) { 1011 | MasterSlaveJedis j = getShard(key); 1012 | return j.zrevrangeWithScores(key, start, end); 1013 | } 1014 | 1015 | public Long zcard(byte[] key) { 1016 | MasterSlaveJedis j = getShard(key); 1017 | return j.zcard(key); 1018 | } 1019 | 1020 | public Double zscore(byte[] key, byte[] member) { 1021 | MasterSlaveJedis j = getShard(key); 1022 | return j.zscore(key, member); 1023 | } 1024 | 1025 | public List sort(byte[] key) { 1026 | MasterSlaveJedis j = getShard(key); 1027 | return j.sort(key); 1028 | } 1029 | 1030 | public List sort(byte[] key, SortingParams sortingParameters) { 1031 | MasterSlaveJedis j = getShard(key); 1032 | return j.sort(key, sortingParameters); 1033 | } 1034 | 1035 | public Long zcount(byte[] key, double min, double max) { 1036 | MasterSlaveJedis j = getShard(key); 1037 | return j.zcount(key, min, max); 1038 | } 1039 | 1040 | public Long zcount(byte[] key, byte[] min, byte[] max) { 1041 | MasterSlaveJedis j = getShard(key); 1042 | return j.zcount(key, min, max); 1043 | } 1044 | 1045 | public Set zrangeByScore(byte[] key, double min, double max) { 1046 | MasterSlaveJedis j = getShard(key); 1047 | return j.zrangeByScore(key, min, max); 1048 | } 1049 | 1050 | public Set zrangeByScore(byte[] key, double min, double max, 1051 | int offset, int count) { 1052 | MasterSlaveJedis j = getShard(key); 1053 | return j.zrangeByScore(key, min, max, offset, count); 1054 | } 1055 | 1056 | public Set zrangeByScoreWithScores(byte[] key, double min, double max) { 1057 | MasterSlaveJedis j = getShard(key); 1058 | return j.zrangeByScoreWithScores(key, min, max); 1059 | } 1060 | 1061 | public Set zrangeByScoreWithScores(byte[] key, double min, 1062 | double max, int offset, int count) { 1063 | MasterSlaveJedis j = getShard(key); 1064 | return j.zrangeByScoreWithScores(key, min, max, offset, count); 1065 | } 1066 | 1067 | public Set zrangeByScore(byte[] key, byte[] min, byte[] max) { 1068 | MasterSlaveJedis j = getShard(key); 1069 | return j.zrangeByScore(key, min, max); 1070 | } 1071 | 1072 | public Set zrangeByScoreWithScores(byte[] key, byte[] min, byte[] max) { 1073 | MasterSlaveJedis j = getShard(key); 1074 | return j.zrangeByScoreWithScores(key, min, max); 1075 | } 1076 | 1077 | public Set zrangeByScoreWithScores(byte[] key, byte[] min, 1078 | byte[] max, int offset, int count) { 1079 | MasterSlaveJedis j = getShard(key); 1080 | return j.zrangeByScoreWithScores(key, min, max, offset, count); 1081 | } 1082 | 1083 | public Set zrangeByScore(byte[] key, byte[] min, byte[] max, 1084 | int offset, int count) { 1085 | MasterSlaveJedis j = getShard(key); 1086 | return j.zrangeByScore(key, min, max, offset, count); 1087 | } 1088 | 1089 | public Set zrevrangeByScore(byte[] key, double max, double min) { 1090 | MasterSlaveJedis j = getShard(key); 1091 | return j.zrevrangeByScore(key, max, min); 1092 | } 1093 | 1094 | public Set zrevrangeByScore(byte[] key, double max, double min, 1095 | int offset, int count) { 1096 | MasterSlaveJedis j = getShard(key); 1097 | return j.zrevrangeByScore(key, max, min, offset, count); 1098 | } 1099 | 1100 | public Set zrevrangeByScoreWithScores(byte[] key, double max, 1101 | double min) { 1102 | MasterSlaveJedis j = getShard(key); 1103 | return j.zrevrangeByScoreWithScores(key, max, min); 1104 | } 1105 | 1106 | public Set zrevrangeByScoreWithScores(byte[] key, double max, 1107 | double min, int offset, int count) { 1108 | MasterSlaveJedis j = getShard(key); 1109 | return j.zrevrangeByScoreWithScores(key, max, min, offset, count); 1110 | } 1111 | 1112 | public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min) { 1113 | MasterSlaveJedis j = getShard(key); 1114 | return j.zrevrangeByScore(key, max, min); 1115 | } 1116 | 1117 | public Set zrevrangeByScore(byte[] key, byte[] max, byte[] min, 1118 | int offset, int count) { 1119 | MasterSlaveJedis j = getShard(key); 1120 | return j.zrevrangeByScore(key, max, min, offset, count); 1121 | } 1122 | 1123 | public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, 1124 | byte[] min) { 1125 | MasterSlaveJedis j = getShard(key); 1126 | return j.zrevrangeByScoreWithScores(key, max, min); 1127 | } 1128 | 1129 | public Set zrevrangeByScoreWithScores(byte[] key, byte[] max, 1130 | byte[] min, int offset, int count) { 1131 | MasterSlaveJedis j = getShard(key); 1132 | return j.zrevrangeByScoreWithScores(key, max, min, offset, count); 1133 | } 1134 | 1135 | public Long zremrangeByRank(byte[] key, long start, long end) { 1136 | MasterSlaveJedis j = getShard(key); 1137 | return j.zremrangeByRank(key, start, end); 1138 | } 1139 | 1140 | public Long zremrangeByScore(byte[] key, double start, double end) { 1141 | MasterSlaveJedis j = getShard(key); 1142 | return j.zremrangeByScore(key, start, end); 1143 | } 1144 | 1145 | public Long zremrangeByScore(byte[] key, byte[] start, byte[] end) { 1146 | MasterSlaveJedis j = getShard(key); 1147 | return j.zremrangeByScore(key, start, end); 1148 | } 1149 | 1150 | public Long zlexcount(final byte[] key, final byte[] min, final byte[] max) { 1151 | MasterSlaveJedis j = getShard(key); 1152 | return j.zlexcount(key, min, max); 1153 | } 1154 | 1155 | public Set zrangeByLex(final byte[] key, final byte[] min, 1156 | final byte[] max) { 1157 | MasterSlaveJedis j = getShard(key); 1158 | return j.zrangeByLex(key, min, max); 1159 | } 1160 | 1161 | public Set zrangeByLex(final byte[] key, final byte[] min, 1162 | final byte[] max, final int offset, final int count) { 1163 | MasterSlaveJedis j = getShard(key); 1164 | return j.zrangeByLex(key, min, max, offset, count); 1165 | } 1166 | 1167 | public Long zremrangeByLex(final byte[] key, final byte[] min, 1168 | final byte[] max) { 1169 | MasterSlaveJedis j = getShard(key); 1170 | return j.zremrangeByLex(key, min, max); 1171 | } 1172 | 1173 | public Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, 1174 | byte[] value) { 1175 | MasterSlaveJedis j = getShard(key); 1176 | return j.linsert(key, where, pivot, value); 1177 | } 1178 | 1179 | public Long objectRefcount(byte[] key) { 1180 | MasterSlaveJedis j = getShard(key); 1181 | return j.objectRefcount(key); 1182 | } 1183 | 1184 | public byte[] objectEncoding(byte[] key) { 1185 | MasterSlaveJedis j = getShard(key); 1186 | return j.objectEncoding(key); 1187 | } 1188 | 1189 | public Long objectIdletime(byte[] key) { 1190 | MasterSlaveJedis j = getShard(key); 1191 | return j.objectIdletime(key); 1192 | } 1193 | 1194 | public Boolean setbit(byte[] key, long offset, boolean value) { 1195 | MasterSlaveJedis j = getShard(key); 1196 | return j.setbit(key, offset, value); 1197 | } 1198 | 1199 | public Boolean setbit(byte[] key, long offset, byte[] value) { 1200 | MasterSlaveJedis j = getShard(key); 1201 | return j.setbit(key, offset, value); 1202 | } 1203 | 1204 | public Boolean getbit(byte[] key, long offset) { 1205 | MasterSlaveJedis j = getShard(key); 1206 | return j.getbit(key, offset); 1207 | } 1208 | 1209 | public Long setrange(byte[] key, long offset, byte[] value) { 1210 | MasterSlaveJedis j = getShard(key); 1211 | return j.setrange(key, offset, value); 1212 | } 1213 | 1214 | public byte[] getrange(byte[] key, long startOffset, long endOffset) { 1215 | MasterSlaveJedis j = getShard(key); 1216 | return j.getrange(key, startOffset, endOffset); 1217 | } 1218 | 1219 | public Long move(byte[] key, int dbIndex) { 1220 | MasterSlaveJedis j = getShard(key); 1221 | return j.move(key, dbIndex); 1222 | } 1223 | 1224 | public byte[] echo(byte[] arg) { 1225 | MasterSlaveJedis j = getShard(arg); 1226 | return j.echo(arg); 1227 | } 1228 | 1229 | @Deprecated 1230 | public List brpop(byte[] arg) { 1231 | MasterSlaveJedis j = getShard(arg); 1232 | return j.brpop(arg); 1233 | } 1234 | 1235 | @Deprecated 1236 | public List blpop(byte[] arg) { 1237 | MasterSlaveJedis j = getShard(arg); 1238 | return j.blpop(arg); 1239 | } 1240 | 1241 | public Long bitcount(byte[] key) { 1242 | MasterSlaveJedis j = getShard(key); 1243 | return j.bitcount(key); 1244 | } 1245 | 1246 | public Long bitcount(byte[] key, long start, long end) { 1247 | MasterSlaveJedis j = getShard(key); 1248 | return j.bitcount(key, start, end); 1249 | } 1250 | 1251 | public Long pfadd(final byte[] key, final byte[]... elements) { 1252 | MasterSlaveJedis j = getShard(key); 1253 | return j.pfadd(key, elements); 1254 | } 1255 | 1256 | public long pfcount(final byte[] key) { 1257 | MasterSlaveJedis j = getShard(key); 1258 | return j.pfcount(key); 1259 | } 1260 | 1261 | public void close() { 1262 | if (dataSource != null) { 1263 | boolean broken = false; 1264 | 1265 | for (MasterSlaveJedis jedis : getAllShards()) { 1266 | if (jedis.getClient().isBroken()) { 1267 | broken = true; 1268 | break; 1269 | } 1270 | } 1271 | 1272 | if (broken) { 1273 | dataSource.returnBrokenResource(this); 1274 | } else { 1275 | dataSource.returnResource(this); 1276 | } 1277 | 1278 | } else { 1279 | disconnect(); 1280 | } 1281 | } 1282 | 1283 | public void setDataSource(Pool shardedJedisPool) { 1284 | this.dataSource = shardedJedisPool; 1285 | } 1286 | 1287 | public void resetState() { 1288 | for (MasterSlaveJedis jedis : getAllShards()) { 1289 | jedis.resetState(); 1290 | } 1291 | } 1292 | 1293 | public void disconnect() { 1294 | for (MasterSlaveJedis jedis : getAllShards()) { 1295 | jedis.disconnect(); 1296 | } 1297 | } 1298 | 1299 | public boolean ping() { 1300 | boolean b = true; 1301 | try { 1302 | for (MasterSlaveJedis jedis : getAllShards()) { 1303 | b = b && "PONG".equals(jedis.ping()); 1304 | if(!b){ 1305 | break; 1306 | } 1307 | } 1308 | } catch (Exception e) { 1309 | b = false; 1310 | } 1311 | return b; 1312 | } 1313 | 1314 | public String toString() { 1315 | StringBuilder sb = new StringBuilder(); 1316 | sb.append("ShardedMasterSlaveJedis@" + Integer.toHexString(hashCode()) + " "); 1317 | sb.append("["); 1318 | for(int i = 0, len = shards.size(); i < len; i++){ 1319 | sb.append(shards.get(i)); 1320 | if(i != len - 1){ 1321 | sb.append(", "); 1322 | } 1323 | } 1324 | sb.append("]}"); 1325 | return sb.toString(); 1326 | } 1327 | 1328 | } 1329 | -------------------------------------------------------------------------------- /src/main/java/com/penglecode/common/redis/jedis/ms/ShardedMasterSlaveJedisSentinelPool.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.ms; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.LinkedHashSet; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Set; 10 | import java.util.concurrent.atomic.AtomicBoolean; 11 | import java.util.regex.Pattern; 12 | 13 | import org.apache.commons.pool2.PooledObject; 14 | import org.apache.commons.pool2.PooledObjectFactory; 15 | import org.apache.commons.pool2.impl.DefaultPooledObject; 16 | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 17 | import org.slf4j.Logger; 18 | import org.slf4j.LoggerFactory; 19 | 20 | import redis.clients.jedis.HostAndPort; 21 | import redis.clients.jedis.Jedis; 22 | import redis.clients.jedis.JedisPubSub; 23 | import redis.clients.jedis.JedisShardInfo; 24 | import redis.clients.jedis.Protocol; 25 | import redis.clients.jedis.exceptions.JedisConnectionException; 26 | import redis.clients.util.Hashing; 27 | import redis.clients.util.Pool; 28 | 29 | public class ShardedMasterSlaveJedisSentinelPool extends Pool { 30 | 31 | private Logger logger = LoggerFactory.getLogger(ShardedMasterSlaveJedisSentinelPool.class); 32 | 33 | protected Set masterNames; 34 | 35 | protected Set sentinels; 36 | 37 | protected GenericObjectPoolConfig poolConfig; 38 | 39 | protected int timeout = Protocol.DEFAULT_TIMEOUT; 40 | 41 | protected String password; 42 | 43 | protected int database = Protocol.DEFAULT_DATABASE; 44 | 45 | protected volatile Set currentHostMasterSlaves; 46 | 47 | protected Set shardedMasterSlaveListeners = new HashSet(); 48 | 49 | private volatile ShardedMasterSlaveJedisFactory factory; 50 | 51 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, final GenericObjectPoolConfig poolConfig) { 52 | this(masterNames, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); 53 | } 54 | 55 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels) { 56 | this(masterNames, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE); 57 | } 58 | 59 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, String password) { 60 | this(masterNames, sentinels, new GenericObjectPoolConfig(), Protocol.DEFAULT_TIMEOUT, password); 61 | } 62 | 63 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, final GenericObjectPoolConfig poolConfig, int timeout, final String password) { 64 | this(masterNames, sentinels, poolConfig, timeout, password, Protocol.DEFAULT_DATABASE); 65 | } 66 | 67 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, final GenericObjectPoolConfig poolConfig, final int timeout) { 68 | this(masterNames, sentinels, poolConfig, timeout, null, Protocol.DEFAULT_DATABASE); 69 | } 70 | 71 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, final GenericObjectPoolConfig poolConfig, final String password) { 72 | this(masterNames, sentinels, poolConfig, Protocol.DEFAULT_TIMEOUT, password); 73 | } 74 | 75 | /** 76 | * Master-Slave must be use the same config 77 | */ 78 | public ShardedMasterSlaveJedisSentinelPool(Set masterNames, Set sentinels, final GenericObjectPoolConfig poolConfig, int timeout, final String password, final int database) { 79 | if(!(masterNames instanceof LinkedHashSet)){ 80 | throw new IllegalArgumentException("Parameter 'masterNames' must be typeof java.util.LinkedHashSet."); 81 | } 82 | this.masterNames = masterNames; 83 | this.sentinels = sentinels; 84 | this.poolConfig = poolConfig; 85 | this.timeout = timeout; 86 | this.password = password; 87 | this.database = database; 88 | 89 | initSentinelPool(); 90 | initSentinelLiseners(); 91 | } 92 | 93 | protected void initSentinelPool() { 94 | Set masterSlaveHostAndPorts = sentinelGetMasterSlaves(); 95 | initPool(masterSlaveHostAndPorts); 96 | } 97 | 98 | protected Set sentinelGetMasterSlaves() { 99 | Set masterSlaveHostAndPorts = new LinkedHashSet(); 100 | logger.info("Trying to find Sharded-Master-Slaves from available sentinels..."); 101 | 102 | for (String sentinel : sentinels) { 103 | final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); 104 | logger.info("Connecting to sentinel " + hap); 105 | 106 | Jedis jedis = null; 107 | try { 108 | jedis = new Jedis(hap.getHost(), hap.getPort()); 109 | for(String masterName : masterNames){ 110 | List masterAddr = jedis.sentinelGetMasterAddrByName(masterName); 111 | List> slaveAddrs = jedis.sentinelSlaves(masterName); 112 | 113 | if (masterAddr == null || masterAddr.size() != 2) { 114 | logger.warn("Can not get master addr, master name: {}. Sentinel: {}.", masterName, hap); 115 | continue; 116 | } 117 | 118 | if (slaveAddrs == null || slaveAddrs.isEmpty()) { 119 | logger.warn("Can not get slave addr, master name: {}. Sentinel: {}.", masterName, hap); 120 | continue; 121 | } 122 | 123 | HostAndPort master = toHostAndPort(masterAddr); 124 | Set slaves = new LinkedHashSet(); 125 | for(Map slave : slaveAddrs){ 126 | if("slave".equals(slave.get("flags"))){ //is normal worked slave at now 127 | slaves.add(toHostAndPort(Arrays.asList(slave.get("ip"), slave.get("port")))); 128 | } 129 | } 130 | MasterSlaveHostAndPort masterSlaveHostAndPort = new MasterSlaveHostAndPort(masterName, master, slaves); 131 | logger.info("Found sharded master-slaves : {}", masterSlaveHostAndPort); 132 | masterSlaveHostAndPorts.add(masterSlaveHostAndPort); 133 | } 134 | return masterSlaveHostAndPorts; 135 | } catch (JedisConnectionException e) { 136 | logger.warn("Cannot connect to sentinel running @ {}. Trying next one.", hap); 137 | } finally { 138 | if (jedis != null) { 139 | jedis.close(); 140 | } 141 | } 142 | } 143 | return null; 144 | } 145 | 146 | protected void initSentinelLiseners() { 147 | if(currentHostMasterSlaves != null){ 148 | logger.info("Starting Sentinel listeners..."); 149 | for (String sentinel : sentinels) { 150 | try { 151 | final HostAndPort hap = toHostAndPort(Arrays.asList(sentinel.split(":"))); 152 | ShardedMasterSlaveListener shardedMasterSlaveListener = new ShardedMasterSlaveListener(masterNames, hap.getHost(), hap.getPort()); 153 | shardedMasterSlaveListeners.add(shardedMasterSlaveListener); 154 | shardedMasterSlaveListener.start(); 155 | } catch(Exception e) { 156 | logger.error("Starting Sentinel listeners caught a exception: " + e.getMessage(), e); 157 | } 158 | } 159 | } 160 | } 161 | 162 | protected synchronized void initPool(Set masterSlaveHostAndPorts){ 163 | if(masterSlaveHostAndPorts != null && !masterSlaveHostAndPorts.equals(currentHostMasterSlaves)){ 164 | currentHostMasterSlaves = masterSlaveHostAndPorts; 165 | 166 | List shards = new ArrayList(); 167 | for(MasterSlaveHostAndPort masterSlaveHostAndPort : masterSlaveHostAndPorts){ 168 | JedisShardInfo masterShard = toJedisShardInfo(masterSlaveHostAndPort.getMaster(), masterSlaveHostAndPort.getMasterName()); 169 | List slaveShards = new ArrayList(); 170 | for(HostAndPort slave : masterSlaveHostAndPort.getSlaves()){ 171 | JedisShardInfo slaveShard = toJedisShardInfo(slave, masterSlaveHostAndPort.getMasterName()); 172 | slaveShards.add(slaveShard); 173 | } 174 | shards.add(new MasterSlaveJedisShardInfo(masterSlaveHostAndPort.getMasterName(), masterShard, slaveShards)); 175 | } 176 | 177 | if(factory == null){ 178 | factory = new ShardedMasterSlaveJedisFactory(shards, Hashing.MURMUR_HASH, null); 179 | initPool(poolConfig, factory); 180 | }else{ 181 | factory.setShards(shards); 182 | // although we clear the pool, we still have to check the 183 | // returned object 184 | // in getResource, this call only clears idle instances, not 185 | // borrowed instances 186 | internalPool.clear(); 187 | } 188 | 189 | logger.info("Create Sharded-Master-Slaves jedis pool for {}", currentHostMasterSlaves); 190 | } 191 | } 192 | 193 | protected JedisShardInfo toJedisShardInfo(HostAndPort hostAndPort, String masterName) { 194 | JedisShardInfo shard = new JedisShardInfo(hostAndPort.getHost(), hostAndPort.getPort(), timeout, masterName); 195 | shard.setPassword(password); 196 | return shard; 197 | } 198 | 199 | public ShardedMasterSlaveJedis getResource() { 200 | while (true) { 201 | ShardedMasterSlaveJedis shardedMasterSlaveJedis = super.getResource(); 202 | shardedMasterSlaveJedis.setDataSource(this); 203 | 204 | // get a reference because it can change concurrently 205 | final Set reference = currentHostMasterSlaves; 206 | final Set connectionDesc = shardedMasterSlaveJedis.getConnectionDesc(); 207 | if (connectionDesc.equals(reference)) { 208 | // connected to the correct master 209 | return shardedMasterSlaveJedis; 210 | } else { 211 | returnBrokenResource(shardedMasterSlaveJedis); 212 | } 213 | } 214 | } 215 | 216 | public void returnBrokenResource(final ShardedMasterSlaveJedis resource) { 217 | if (resource != null) { 218 | returnBrokenResourceObject(resource); 219 | } 220 | } 221 | 222 | public void returnResource(final ShardedMasterSlaveJedis resource) { 223 | if (resource != null) { 224 | // get a reference because it can change concurrently 225 | final Set reference = currentHostMasterSlaves; 226 | final Set connectionDesc = resource.getConnectionDesc(); 227 | if (connectionDesc.equals(reference)) { 228 | resource.resetState(); 229 | returnResourceObject(resource); 230 | } else { 231 | returnBrokenResource(resource); 232 | } 233 | } 234 | } 235 | 236 | public void destroy() { 237 | for (ShardedMasterSlaveListener m : shardedMasterSlaveListeners) { 238 | m.shutdown(); 239 | } 240 | super.destroy(); 241 | } 242 | 243 | public Set getCurrentHostMasterSlaves() { 244 | return currentHostMasterSlaves; 245 | } 246 | 247 | protected HostAndPort toHostAndPort(List hostAndPort){ 248 | return new HostAndPort(hostAndPort.get(0), Integer.parseInt(hostAndPort.get(1))); 249 | } 250 | 251 | protected class ShardedMasterSlaveListener extends Thread { 252 | 253 | protected Set masterNames; 254 | protected String host; 255 | protected int port; 256 | protected long subscribeRetryWaitTimeMillis = 5000; 257 | protected Jedis sentinelJedis; 258 | protected AtomicBoolean running = new AtomicBoolean(false); 259 | 260 | protected ShardedMasterSlaveListener() { 261 | } 262 | 263 | public ShardedMasterSlaveListener(Set masterNames, String host, int port) { 264 | this.masterNames = masterNames; 265 | this.host = host; 266 | this.port = port; 267 | } 268 | 269 | public ShardedMasterSlaveListener(Set masterNames, String host, int port, long subscribeRetryWaitTimeMillis) { 270 | this(masterNames, host, port); 271 | this.subscribeRetryWaitTimeMillis = subscribeRetryWaitTimeMillis; 272 | } 273 | 274 | public void run() { 275 | running.set(true); 276 | while (running.get()) { 277 | logger.debug(">>> Runing!"); 278 | sentinelJedis = new Jedis(host, port); 279 | try { 280 | sentinelJedis.subscribe(new JedisPubSub() { 281 | public void onMessage(String channel, String message) { 282 | logger.info("Sentinel {} published: {} {}", host + ":" + port, channel, message); 283 | if("+sdown".equals(channel)){ //has slave offline 284 | String[] messages = message.split(" "); 285 | if(messages.length == 8){ 286 | if("slave".equals(messages[0])){ 287 | if(masterNames.contains(messages[5])){ 288 | String slaveIp = messages[2]; 289 | String slavePort = messages[3]; 290 | String masterIp = messages[6]; 291 | String masterPort = messages[7]; 292 | logger.error("Found unavailable redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, messages[5], masterIp + ":" + masterPort); 293 | initSentinelPool(); 294 | }else{ 295 | logger.error("Ignoring message on +sdown for master name {}, but our master name is {}!", messages[5], masterNames); 296 | } 297 | }else{ 298 | logger.error("Invalid message received on Sentinel {} on channel +sdown: {}", host + ":" + port, message); 299 | } 300 | } 301 | } 302 | if("-sdown".equals(channel)){ //has slave online 303 | String[] messages = message.split(" "); 304 | if(messages.length == 8){ 305 | if("slave".equals(messages[0])){ 306 | if(masterNames.contains(messages[5])){ 307 | String slaveIp = messages[2]; 308 | String slavePort = messages[3]; 309 | String masterIp = messages[6]; 310 | String masterPort = messages[7]; 311 | logger.info("Found available redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, messages[5], masterIp + ":" + masterPort); 312 | initSentinelPool(); 313 | }else{ 314 | logger.error("Ignoring message on -sdown for master name {}, but our master name is {}!", messages[5], masterNames); 315 | } 316 | }else{ 317 | logger.error("Invalid message received on Sentinel {} on channel -sdown: {}", host + ":" + port, message); 318 | } 319 | } 320 | } 321 | if("+switch-master".equals(channel)){ //master has been switched 322 | String[] messages = message.split(" "); 323 | if(messages.length == 5){ 324 | if(masterNames.contains(messages[0])){ 325 | String oldMasterIp = messages[1]; 326 | String oldMasterPort = messages[2]; 327 | String newMasterIp = messages[3]; 328 | String newMasterPort = messages[4]; 329 | logger.info("Switch master {} from [{}] to [{}]", messages[0], oldMasterIp + ":" + oldMasterPort, newMasterIp + ":" + newMasterPort); 330 | initSentinelPool(); 331 | }else{ 332 | logger.error("Ignoring message on +switch-master for master name {}, but our master name is {}!", messages[5], masterNames); 333 | } 334 | }else{ 335 | logger.error("Invalid message received on Sentinel {} on channel +switch-master: {}", host + ":" + port, message); 336 | } 337 | } 338 | if("+slave".equals(channel)){ //has new slave joined 339 | String[] messages = message.split(" "); 340 | if(messages.length == 8){ 341 | if("slave".equals(messages[0])){ 342 | if(masterNames.contains(messages[5])){ 343 | String slaveIp = messages[2]; 344 | String slavePort = messages[3]; 345 | String masterIp = messages[6]; 346 | String masterPort = messages[7]; 347 | logger.error("Found available redis slave[{}] for master[{}@{}]", slaveIp + ":" + slavePort, messages[5], masterIp + ":" + masterPort); 348 | initSentinelPool(); 349 | }else{ 350 | logger.error("Ignoring message on +slave for master name {}, but our master name is {}!", messages[5], masterNames); 351 | } 352 | }else{ 353 | logger.error("Invalid message received on Sentinel {} on channel +slave: {}", host + ":" + port, message); 354 | } 355 | } 356 | } 357 | } 358 | }, "+switch-master", "+sdown", "-sdown", "+slave"); 359 | } catch (JedisConnectionException e) { 360 | if (running.get()) { 361 | logger.error("Lost connection to Sentinel at {}. Sleeping {}ms and retrying.", host + ":" + port, subscribeRetryWaitTimeMillis); 362 | try { 363 | Thread.sleep(subscribeRetryWaitTimeMillis); 364 | } catch (InterruptedException e1) { 365 | e1.printStackTrace(); 366 | } 367 | } else { 368 | logger.error("Listener stop running and unsubscribing from Sentinel at {}.", host + ":" + port); 369 | } 370 | } 371 | } 372 | } 373 | 374 | public void shutdown() { 375 | try { 376 | logger.info("Shutting down listener on {}.", host + ":" + port); 377 | running.set(false); 378 | // This isn't good, the Jedis object is not thread safe 379 | sentinelJedis.disconnect(); 380 | } catch (Exception e) { 381 | logger.error("Caught exception while shutting down: " + e.getMessage()); 382 | } 383 | } 384 | } 385 | 386 | /** 387 | * ShardedMasterSlaveJedis工厂类 388 | * 389 | * @author pengpeng 390 | * @date 2015年3月14日 上午10:09:00 391 | * @version 1.0 392 | */ 393 | protected class ShardedMasterSlaveJedisFactory implements PooledObjectFactory { 394 | private List shards; 395 | private Hashing algo; 396 | private Pattern keyTagPattern; 397 | 398 | public ShardedMasterSlaveJedisFactory(List shards, Hashing algo, Pattern keyTagPattern) { 399 | this.shards = shards; 400 | this.algo = algo; 401 | this.keyTagPattern = keyTagPattern; 402 | } 403 | 404 | public void setShards(List shards) { 405 | this.shards = shards; 406 | } 407 | 408 | public PooledObject makeObject() throws Exception { 409 | ShardedMasterSlaveJedis shardedMasterSlaveJedis = new ShardedMasterSlaveJedis(shards, algo, keyTagPattern); 410 | return new DefaultPooledObject(shardedMasterSlaveJedis); 411 | } 412 | 413 | public void destroyObject(PooledObject pooledShardedMasterSlaveJedis) 414 | throws Exception { 415 | pooledShardedMasterSlaveJedis.getObject().disconnect(); 416 | } 417 | 418 | public boolean validateObject(PooledObject pooledObject) { 419 | try { 420 | ShardedMasterSlaveJedis pooledShardedMasterSlaveJedis = pooledObject.getObject(); 421 | if(pooledShardedMasterSlaveJedis.ping()){ 422 | return true; 423 | } 424 | return false; 425 | } catch (Exception ex) { 426 | return false; 427 | } 428 | } 429 | 430 | public void activateObject(PooledObject p) throws Exception { 431 | 432 | } 433 | 434 | public void passivateObject(PooledObject p) throws Exception { 435 | 436 | } 437 | } 438 | 439 | } 440 | -------------------------------------------------------------------------------- /src/test/java/com/penglecode/common/redis/jedis/test/AbstractJedisExample.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.test; 2 | 3 | import org.junit.Before; 4 | 5 | import com.penglecode.common.redis.jedis.JedisTemplate; 6 | 7 | import redis.clients.jedis.JedisPoolConfig; 8 | import redis.clients.util.Pool; 9 | 10 | public abstract class AbstractJedisExample { 11 | 12 | private JedisPoolConfig jedisPoolConfig; 13 | 14 | private Pool jedisPool; 15 | 16 | private JedisTemplate jedisTemplate; 17 | 18 | @Before 19 | public final void setUp(){ 20 | jedisPoolConfig = createJedisPoolConfig(); 21 | jedisPool = createJedisPool(jedisPoolConfig); 22 | jedisTemplate = new JedisTemplate(jedisPool); 23 | } 24 | 25 | public JedisPoolConfig createJedisPoolConfig(){ 26 | JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 27 | jedisPoolConfig.setMaxTotal(8); 28 | jedisPoolConfig.setMaxIdle(8); 29 | jedisPoolConfig.setMinIdle(0); 30 | jedisPoolConfig.setMaxWaitMillis(15000); 31 | return jedisPoolConfig; 32 | } 33 | 34 | public abstract Pool createJedisPool(JedisPoolConfig jedisPoolConfig); 35 | 36 | public JedisPoolConfig getJedisPoolConfig() { 37 | return jedisPoolConfig; 38 | } 39 | 40 | public Pool getJedisPool() { 41 | return jedisPool; 42 | } 43 | 44 | public JedisTemplate getJedisTemplate() { 45 | return jedisTemplate; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/com/penglecode/common/redis/jedis/test/example/JedisSentinelExample.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.test.example; 2 | 3 | import java.util.Arrays; 4 | import java.util.Date; 5 | import java.util.LinkedHashSet; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | 10 | import org.junit.Test; 11 | 12 | import redis.clients.jedis.HostAndPort; 13 | import redis.clients.jedis.Jedis; 14 | import redis.clients.jedis.JedisPoolConfig; 15 | import redis.clients.jedis.JedisPubSub; 16 | import redis.clients.jedis.JedisSentinelPool; 17 | import redis.clients.jedis.exceptions.JedisConnectionException; 18 | import redis.clients.util.Pool; 19 | 20 | import com.penglecode.common.redis.jedis.test.AbstractJedisExample; 21 | 22 | public class JedisSentinelExample extends AbstractJedisExample { 23 | 24 | public Pool createJedisPool(JedisPoolConfig jedisPoolConfig) { 25 | Set sentinels = new LinkedHashSet(); 26 | sentinels.add("192.168.137.101:63791"); 27 | sentinels.add("192.168.137.101:63792"); 28 | return new JedisSentinelPool("master-1", sentinels, jedisPoolConfig); 29 | } 30 | 31 | @Test 32 | public void getResource() throws Exception { 33 | Jedis master = null; 34 | try { 35 | master = this.getJedisPool().getResource(); 36 | System.out.println(String.format(">>> jedis = %s", master)); 37 | System.out.println(master.getClient().getHost() + ":" + master.getClient().getPort()); 38 | //System.out.println(jedis.info()); //看以看出此处的jedis实例指向的是master 39 | 40 | Date now = new Date(); 41 | master.set("current_time", String.format("%tF %tT", now, now)); 42 | master.incr("COUNT"); 43 | } finally { 44 | if(master != null){ 45 | this.getJedisPool().returnResource(master); 46 | } 47 | } 48 | } 49 | 50 | @Test 51 | public void sentinelMasters() throws Exception { 52 | Jedis sentinel = null; 53 | try { 54 | sentinel = new Jedis("192.168.137.101", 63791); 55 | System.out.println(String.format(">>> sentinel = %s", sentinel)); 56 | List> masters = sentinel.sentinelMasters(); 57 | for(Map master : masters){ 58 | System.out.println("----------------------------------------------"); 59 | for(Map.Entry entry : master.entrySet()){ 60 | System.out.println(entry.getKey() + " : " + entry.getValue()); 61 | } 62 | } 63 | } finally { 64 | if(sentinel != null){ 65 | sentinel.close(); 66 | } 67 | } 68 | } 69 | 70 | @Test 71 | public void sentinelSlaves() throws Exception { 72 | Jedis sentinel = null; 73 | try { 74 | sentinel = new Jedis("192.168.137.101", 63791); 75 | System.out.println(String.format(">>> sentinel = %s", sentinel)); 76 | List> slaves = sentinel.sentinelSlaves("master-1"); 77 | for(Map slave : slaves){ 78 | System.out.println("----------------------------------------------"); 79 | for(Map.Entry entry : slave.entrySet()){ 80 | System.out.println(entry.getKey() + " : " + entry.getValue()); 81 | } 82 | } 83 | } finally { 84 | if(sentinel != null){ 85 | sentinel.close(); 86 | } 87 | } 88 | } 89 | 90 | @Test 91 | public void sentinelPubSubListener() throws Exception { 92 | final String masterName = "master-1"; 93 | final String host = "192.168.137.101"; 94 | final int port = 63791; 95 | final Jedis sentinelJedis = new Jedis(host, port); 96 | System.out.println(String.format(">>> sentinel = %s", sentinelJedis)); 97 | 98 | try { 99 | sentinelJedis.subscribe(new JedisPubSub() { 100 | public void onMessage(String channel, String message) { 101 | System.out.println("Sentinel " + host + ":" + port + " published: " + message); 102 | if("+sdown".equals(channel)){ 103 | System.err.println("+sdown " + message); 104 | String[] messages = message.split(" "); 105 | System.out.println(Arrays.toString(messages)); 106 | if(messages.length == 8 && "slave".equals(messages[0])){ 107 | if(masterName.equals(messages[5])){ 108 | String slaveIp = messages[2]; 109 | String slavePort = messages[3]; 110 | String masterIp = messages[6]; 111 | String masterPort = messages[7]; 112 | System.err.println("Found unavailable redis slave[" + slaveIp + ":" + slavePort + "] for master[" + masterName + "@" + masterIp + ":" + masterPort + "]"); 113 | }else{ 114 | System.err.println("Ignoring message on +sdown for master name " + messages[5] + ", but our master name is " + masterName); 115 | } 116 | }else{ 117 | System.err.println("Invalid message received on Sentinel " + host + ":" + port + " on channel +sdown: " + message); 118 | } 119 | } 120 | if("-sdown".equals(channel)){ 121 | System.err.println("-sdown " + message); 122 | String[] messages = message.split(" "); 123 | System.out.println(Arrays.toString(messages)); 124 | if(messages.length == 8 && "slave".equals(messages[0])){ 125 | if(masterName.equals(messages[5])){ 126 | String slaveIp = messages[2]; 127 | String slavePort = messages[3]; 128 | String masterIp = messages[6]; 129 | String masterPort = messages[7]; 130 | System.err.println("Found available redis slave[" + slaveIp + ":" + slavePort + "] for master[" + masterName + "@" + masterIp + ":" + masterPort + "]"); 131 | }else{ 132 | System.err.println("Ignoring message on +sdown for master name " + messages[5] + ", but our master name is " + masterName); 133 | } 134 | }else{ 135 | System.err.println("Invalid message received on Sentinel " + host + ":" + port + " on channel -sdown: " + message); 136 | } 137 | } 138 | if("+switch-master".equals(channel)){ 139 | System.err.println("+switch-master " + message); 140 | String[] messages = message.split(" "); 141 | System.out.println(Arrays.toString(messages)); 142 | if(messages.length == 5){ 143 | if(masterName.equals(messages[0])){ 144 | String oldMasterIp = messages[1]; 145 | String oldMasterPort = messages[2]; 146 | String newMasterIp = messages[3]; 147 | String newMasterPort = messages[4]; 148 | System.err.println("Switch master " + masterName + " from [" + oldMasterIp + ":" + oldMasterPort + "] to [" + newMasterIp + ":" + newMasterPort + "]"); 149 | }else{ 150 | System.err.println("Ignoring message on +switch-master for master name " + messages[5] + ", but our master name is " + masterName); 151 | } 152 | }else{ 153 | System.err.println("Invalid message received on Sentinel " + host + ":" + port + " on channel +switch-master: " + message); 154 | } 155 | } 156 | } 157 | }, "+switch-master", "+sdown", "-sdown"); 158 | } catch(JedisConnectionException e) { 159 | System.err.println(e); 160 | } 161 | Thread.sleep(3600); 162 | //sentinelJedis.close(); 163 | } 164 | 165 | protected HostAndPort toHostAndPort(List hostAndPort){ 166 | return new HostAndPort(hostAndPort.get(0), Integer.parseInt(hostAndPort.get(1))); 167 | } 168 | 169 | } 170 | -------------------------------------------------------------------------------- /src/test/java/com/penglecode/common/redis/jedis/test/example/MasterSlaveJedisSentinelPoolExample.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.test.example; 2 | 3 | import java.util.Date; 4 | import java.util.LinkedHashSet; 5 | import java.util.Set; 6 | import java.util.concurrent.TimeUnit; 7 | import java.util.concurrent.locks.LockSupport; 8 | 9 | import org.junit.Test; 10 | 11 | import redis.clients.jedis.Jedis; 12 | import redis.clients.jedis.JedisPoolConfig; 13 | import redis.clients.util.Pool; 14 | 15 | import com.penglecode.common.redis.jedis.ms.MasterSlaveJedis; 16 | import com.penglecode.common.redis.jedis.ms.MasterSlaveJedisSentinelPool; 17 | import com.penglecode.common.redis.jedis.test.AbstractJedisExample; 18 | 19 | public class MasterSlaveJedisSentinelPoolExample extends AbstractJedisExample { 20 | 21 | public Pool createJedisPool(JedisPoolConfig jedisPoolConfig) { 22 | Set sentinels = new LinkedHashSet(); 23 | sentinels.add("192.168.137.101:63791"); 24 | sentinels.add("192.168.137.101:63792"); 25 | return new MasterSlaveJedisSentinelPool("master-1", sentinels, jedisPoolConfig); 26 | } 27 | 28 | @Test 29 | public void getRourceAndReturnResource(){ 30 | Pool pool = this.getJedisPool(); 31 | MasterSlaveJedis masterSlaveJedis = pool.getResource(); 32 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis); 33 | pool.returnResource(masterSlaveJedis); 34 | 35 | System.out.println("--------------------------------------"); 36 | for(int i = 0; i < 10; i++){ 37 | masterSlaveJedis = pool.getResource(); 38 | Jedis slaveJedis = masterSlaveJedis.opsForSlave(String.valueOf(i)); 39 | System.out.println(">>> slaveJedis = " + slaveJedis.getClient().getHost() + ":" + slaveJedis.getClient().getPort()); 40 | pool.returnResource(masterSlaveJedis); 41 | } 42 | System.out.println("--------------------------------------"); 43 | 44 | System.out.println(">>> pool = " + pool); 45 | pool.destroy(); 46 | System.out.println(">>> pool = " + pool); 47 | System.out.println(pool.getResource());// 如果pool已经被销毁,调用pool.getResource()会抛出"Can not get a resource from pool"异常 48 | } 49 | 50 | @Test 51 | public void masterSetSlaveGet(){ 52 | Date now = new Date(); 53 | String nowTime = String.format("%tF %tT", now, now); 54 | Pool pool = this.getJedisPool(); 55 | MasterSlaveJedis masterSlaveJedis = pool.getResource(); 56 | 57 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis.getClient().getHost() + ":" + masterSlaveJedis.getClient().getPort()); 58 | System.out.println(">>> nowTime = " + nowTime); 59 | 60 | masterSlaveJedis.set("current_time", nowTime); 61 | 62 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200)); 63 | 64 | System.out.println("--------------------------------------"); 65 | Jedis slaveJedis = masterSlaveJedis.opsForSlave(); 66 | System.out.println(">>> slaveJedis = " + slaveJedis.getClient().getHost() + ":" + slaveJedis.getClient().getPort()); 67 | System.out.println(">>> nowTime = " + slaveJedis.get("current_time")); 68 | 69 | //slaveJedis.set("current_time", nowTime + ".000"); // slave节点默认是只读的,如果在只读的slave节点上进行写操作会抛出异常 70 | pool.returnResource(masterSlaveJedis); 71 | 72 | System.out.println("--------------------------------------"); 73 | pool.destroy(); 74 | } 75 | 76 | @Test 77 | public void masterSlaveFailover() throws Exception { 78 | Pool pool = this.getJedisPool(); 79 | 80 | MasterSlaveJedis masterSlaveJedis = pool.getResource(); 81 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis.getClient().getHost() + ":" + masterSlaveJedis.getClient().getPort()); 82 | pool.returnResource(masterSlaveJedis); 83 | 84 | System.out.println("--------------------------------------"); 85 | 86 | masterSlaveJedis = pool.getResource(); 87 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis.getClient().getHost() + ":" + masterSlaveJedis.getClient().getPort()); 88 | pool.returnResource(masterSlaveJedis); 89 | 90 | Thread.sleep(120000L * 10); 91 | 92 | masterSlaveJedis = pool.getResource(); 93 | System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis.getClient().getHost() + ":" + masterSlaveJedis.getClient().getPort()); 94 | pool.returnResource(masterSlaveJedis); 95 | 96 | System.out.println("--------------------------------------"); 97 | pool.destroy(); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/test/java/com/penglecode/common/redis/jedis/test/example/ShardedMasterSlaveJedisSentinelPoolExample.java: -------------------------------------------------------------------------------- 1 | package com.penglecode.common.redis.jedis.test.example; 2 | 3 | import java.util.LinkedHashSet; 4 | import java.util.Set; 5 | import java.util.concurrent.TimeUnit; 6 | import java.util.concurrent.locks.LockSupport; 7 | 8 | import org.junit.Test; 9 | 10 | import redis.clients.jedis.JedisPoolConfig; 11 | import redis.clients.util.Pool; 12 | 13 | import com.penglecode.common.redis.jedis.ms.ShardedMasterSlaveJedis; 14 | import com.penglecode.common.redis.jedis.ms.ShardedMasterSlaveJedisSentinelPool; 15 | import com.penglecode.common.redis.jedis.test.AbstractJedisExample; 16 | 17 | public class ShardedMasterSlaveJedisSentinelPoolExample extends AbstractJedisExample { 18 | 19 | public Pool createJedisPool(JedisPoolConfig jedisPoolConfig) { 20 | Set masterNames = new LinkedHashSet(); 21 | masterNames.add("master-1"); 22 | masterNames.add("master-2"); 23 | Set sentinels = new LinkedHashSet(); 24 | sentinels.add("192.168.137.101:63791"); 25 | sentinels.add("192.168.137.101:63792"); 26 | return new ShardedMasterSlaveJedisSentinelPool(masterNames, sentinels, jedisPoolConfig); 27 | } 28 | 29 | @Test 30 | public void getRourceAndReturnResource(){ 31 | Pool pool = this.getJedisPool(); 32 | ShardedMasterSlaveJedis shardedMasterSlaveJedis = pool.getResource(); 33 | System.out.println(">>> shardedMasterSlaveJedis = " + shardedMasterSlaveJedis); 34 | pool.returnResource(shardedMasterSlaveJedis); 35 | 36 | System.out.println("--------------------------------------"); 37 | for(int i = 0; i < 10; i++){ 38 | shardedMasterSlaveJedis = pool.getResource(); 39 | System.out.println(">>> shardedMasterSlaveJedis = " + shardedMasterSlaveJedis); 40 | String key = "shard-" + i; 41 | shardedMasterSlaveJedis.set(key, System.currentTimeMillis() + ""); 42 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200)); 43 | System.out.println(key + " = " + shardedMasterSlaveJedis.getShard(key).opsForSlave().get(key)); 44 | pool.returnResource(shardedMasterSlaveJedis); 45 | } 46 | System.out.println("--------------------------------------"); 47 | 48 | System.out.println(">>> pool = " + pool); 49 | pool.destroy(); 50 | System.out.println(">>> pool = " + pool); 51 | System.out.println(pool.getResource());// 如果pool已经被销毁,调用pool.getResource()会抛出"Can not get a resource from pool"异常 52 | } 53 | 54 | 55 | @Test 56 | public void masterSlaveFailover() throws Exception { 57 | Pool pool = this.getJedisPool(); 58 | 59 | ShardedMasterSlaveJedis shardedMasterSlaveJedis = pool.getResource(); 60 | System.out.println(">>> shardedMasterSlaveJedis = " + shardedMasterSlaveJedis); 61 | pool.returnResource(shardedMasterSlaveJedis); 62 | 63 | System.out.println("--------------------------------------"); 64 | 65 | shardedMasterSlaveJedis = pool.getResource(); 66 | System.out.println(">>> shardedMasterSlaveJedis = " + shardedMasterSlaveJedis); 67 | pool.returnResource(shardedMasterSlaveJedis); 68 | 69 | Thread.sleep(10L * 10); 70 | 71 | shardedMasterSlaveJedis = pool.getResource(); 72 | System.out.println(">>> shardedMasterSlaveJedis = " + shardedMasterSlaveJedis); 73 | pool.returnResource(shardedMasterSlaveJedis); 74 | 75 | System.out.println("--------------------------------------"); 76 | pool.destroy(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | tutorialsRedisLogger 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | %date [%thread] %-5level %logger#%line - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | ${logback.logs.path}${logback.project.name}_log_%d{yyyy-MM-dd}.log 27 | 28 | 30 29 | 30 | 31 | %date [%thread] %-5level %logger#%line - %msg%n 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | --------------------------------------------------------------------------------