单机测试:
1 public class RedisClient{
2 private Jedis jedis;
3 private JedisPool jedisPool;
4 private ShardedJedis shardedJedis;
5 private ShardedJedisPool shardedJedisPool;
6 public RedisClient() {
7
8 }
9 /**
10 * 单例链接redis
11 */
12 @Test
13 public void testJedisSingle() {
14 Jedis jedis = new Jedis("localhost",6379);
15 jedis.set("name", "bar");
16 String name= jedis.get("name");
17 System.out.println(name);
18 jedis.close();
19 }
20 /**
21 * 连接池链接redis
22 */
23 @Test
24 public void testPool() {
25 JedisPoolConfig config = new JedisPoolConfig();
26 config.setMaxTotal(30);//最大连接数
27 config.setMaxIdle(15);//最大空闲连接数
28 JedisPool pool = new JedisPool(config,"localhost",6379);
29 Jedis jedis = null;
30
31 try {
32 jedis = pool.getResource();
33 jedis.set("name1", "wangwu");
34 String name1 = jedis.get("name1");
35 System.out.println(name1);
36 } catch (Exception e) {
37 e.printStackTrace();
38 }finally {
39 jedis.close();
40 }
41 }
42
43
44 }
bar
wangwu
集群测试:
1 public class RedisCluster {
2 @Test
3 public void testCluster() {
4
5 JedisPoolConfig config = new JedisPoolConfig();
6 config.setMaxTotal(30);
7 config.setMaxIdle(15);
8
9 Set<HostAndPort> jedisClusterNode = new HashSet();
10 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7000));
11 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7001));
12 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7002));
13 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7003));
14 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7004));
15 jedisClusterNode.add(new HostAndPort("127.0.0.1", 7005));
16
17 JedisCluster jc_in = new JedisCluster(jedisClusterNode,config);
18 JedisCluster jc_out = new JedisCluster(jedisClusterNode);
19
20 jc_in.set("123", "123");
21 String vlaue = jc_out.get("123");
22 System.out.println("value:"+vlaue);
23 }
24
25 }
value:123