非集群环境下
package com.chiwei.redis; import java.util.ArrayList; import java.util.List; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import redis.clients.util.Pool; public class RedisSingleClient { //未分片客户端连接 private Jedis jedis; //未分片客户端连接池 private JedisPool jedisPool; //分片客户端连接 private ShardedJedis shardedJedis; //分片客户端连接池 private ShardedJedisPool shardedJedisPool; //jedispool配置 private JedisPoolConfig config; public void initJedisPoolConfig() { config = new JedisPoolConfig(); config.setMaxTotal(100); config.setMaxIdle(10); config.setMaxWaitMillis(1000L); config.setTestOnBorrow(true); config.setTestOnReturn(true); } public void initJedisPool() { initJedisPoolConfig(); jedisPool = new JedisPool(config,"192.168.11.176",7379); } public void initShardedJedisPool() { initJedisPoolConfig(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo("192.168.11.176",7379)); shards.add(new JedisShardInfo("192.168.11.177",7379)); shards.add(new JedisShardInfo("192.168.11.178",7379)); shardedJedisPool = new ShardedJedisPool(config,shards); } public Jedis getJedis() { jedis = this.jedisPool.getResource(); return jedis; } public ShardedJedis getShardedJedis() { shardedJedis = this.shardedJedisPool.getResource(); return shardedJedis; } @SuppressWarnings({ "unchecked", "rawtypes" }) public void returnResource(Pool pool, Object redis) { if (redis != null) { pool.returnResourceObject(redis); } } public void set(String key, String value) { try { jedis = getJedis(); shardedJedis = getShardedJedis(); jedis.set(key, value); shardedJedis.getShard("chiwei").set("chiwei", "lining"); } catch (Exception e) { e.printStackTrace(); jedisPool.returnBrokenResource(jedis); shardedJedisPool.returnBrokenResource(shardedJedis); } finally { returnResource(jedisPool, jedis); returnResource(shardedJedisPool, shardedJedis); } } public String get(String key) { String value = null; try { jedis = getJedis(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); // 释放资源 jedisPool.returnBrokenResource(jedis); } finally { returnResource(jedisPool, jedis); } return value; } public String getShardValue(String key) { String value = null; try { shardedJedis = getShardedJedis(); value = shardedJedis.getShard(key).get(key); } catch (Exception e) { e.printStackTrace(); // 释放资源 shardedJedisPool.returnBrokenResource(shardedJedis); } finally { returnResource(shardedJedisPool, shardedJedis); } return value; } @Test public void test() { initJedisPool(); initShardedJedisPool(); jedis = getJedis(); shardedJedis = getShardedJedis(); jedis.set("momo", "nono"); //shardedJedis.set("chiwei", "lining"); System.out.println(jedis.get("momo")); //System.out.println(shardedJedis.get("chiwei")); shardedJedis.getShard("chiwei").set("chiwei", "lining"); System.out.println(shardedJedis.getShard("chiwei").get("chiwei")); } }
分片Shard的目的就是将数据均匀分布到多台redis实例中
以上的测试实例是简单的key-value,下面试试复杂的key
List
shardedJedis.lpush("chiwei", "one"); shardedJedis.lpush("chiwei", "two"); shardedJedis.lpush("chiwei", "three"); shardedJedis.lpush("chiwei", "three"); System.out.println(shardedJedis.llen("chiwei")); String str = shardedJedis.lpop("chiwei"); System.out.println(str);
list添加的元素可重复
Set不能重复
shardedJedis.sadd("chiwei", "one"); shardedJedis.sadd("chiwei", "one");
其它各种类型,请读者自行尝试,http://www.redis.cn/commands.html
集群环境
package com.chiwei.redis; import java.util.HashSet; import java.util.Set; import org.junit.Test; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; public class RedisClusterClient { private String serverInfo = "172.30.129.145:7379,172.30.129.145:7380,172.30.129.146:7379,172.30.129.146:7380,172.30.129.147:7379,172.30.129.147:7380"; private Set<HostAndPort> getClusterInfo(String serverInfo) { Set<HostAndPort> set = new HashSet<HostAndPort>(); if(serverInfo==null||"".equals(serverInfo.length())) { throw new RuntimeException("The serverInfo can not be empty"); } String ipPort[] = serverInfo.split(","); int len = ipPort.length; for(int i=0;i<len;i++) { String server[] = ipPort[i].split(":"); set.add(new HostAndPort(server[0], Integer.parseInt(server[1]))); } return set; } @Test public void test() { Set<HostAndPort> jedisClusterNodes = getClusterInfo(serverInfo); // Jedis Cluster will attempt to discover cluster nodes automatically JedisCluster jc = new JedisCluster(jedisClusterNodes); jc.set("foo", "bar"); String value = jc.get("foo"); System.out.println(value); } }
通过JedisCluster对象来操作redis缓存。
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.0</version> </dependency>
时间: 2024-10-11 04:50:50