Jedis three ways is connected Redis

1, stand-alone mode

String addr = Private "192.168.1.1";
Private String Port = "6236";
Private Key String = "Key";
Private new new Jedis Jedis jedis = (addr, Port); // Jedis Redis data acquired in jedis, jedis.set ( "a", "b "); // change to a key value jedis.hmset (key, the hash);
System.out.println (jedis.get (key));
2, Split mode

GenericObjectPoolConfig config=new GenericObjectPoolConfig();
config.setMaxIdle(32);
config.setMinIdle(12);
config.setTestOnBorrow(true);
config.setTestOnReturn(rtrue);
config.setTestWhileIdle(true);
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
for (int i = 0; i < shareds.size(); i++) {
shards.add(new JedisShardInfo("192.168.0.100", 6379, 200));
}
// 构造池
ShardedJedisPool shardedJedisPool= new ShardedJedisPool(config, shards);
ShardedJedis jedis=shardedJedisPool.getResource();
jedis.set("a","b");
jedis.hmset(key, hash);
3、集群模式(since 3.0)

String [] ADDRs = conf.getString ( " redisIP", "10.244.84.33") split ( ",");. // conf is arranged to read
String [] PORTs = conf.getString ( " redisPort", "6379 ") .split (", ");
for (int I = 0; I <length; I ++) {
HostAndPort hostAndPort = new new HostAndPort (addrs [I], the Integer.parseInt (the PORTs [I]));
haps.add ( hostAndPort);
}
JedisCluster myJedisCluster = new new JedisCluster (HAPS, TIMEOUT);
     the Map <String, String> = new new Gather the HashMap <> (); // Redis data is <key, value> form, may be other types of data
Gather = myJedisCluster.hgetAll (Key);
4. JedisUtil

From then jedis 3.0.1, jedis with a call jedis.close after completion () to return the connection, which is called internally still return resource and returnBrokenResource

public class RedisUtils {
private JedisPool jedisPool;
static {

String configurationFileName = "xxx.properties";
Configuration conf = Configuration.getConfiguration(configurationFileName);
if (conf == null) {
System.out.println("reading " + configurationFileName + " is failed.");
System.exit(-1);
}
String[] ADDRs = conf.getString("redisIP", "10.100.56.33").split(",");
String[] PORTs = conf.getString("redisPort", "6379").split(",");

if (ADDRs.length == 0 || PORTs.length == 0) {
System.out.println("definition redisIP is not found in " + configurationFileName);
System.exit(-1);
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(MAX_IDLE);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDRs[0], PORT, TIMEOUT);
}

public synchronized static Jedis getJedis() {
if (jedisPool != null) {
//获取资源
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
}

public static void returnJedis(Jedis jedis) {
if (jedis != null) {
//释放资源
jedis.close(http://www.my516.com);

}
}
}
 
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11161445.html