Redis-分布式
package test.jedis; import java.util.HashSet; import java.util.Set; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisSentinelPool; /** * 分布式redis 操作 * 没有多台机器,在同一台机器上多个redis 实例来模拟多台机器测试 * * */ public class SentinelPoolTest { @Test public void testJedis() throws InterruptedException { Set<String> sentinels = new HashSet<String>(); sentinels.add("127.0.0.1:6377"); sentinels.add("127.0.0.1:6378"); sentinels.add("127.0.0.1:6379"); JedisSentinelPool sentinelPool = new JedisSentinelPool("master_test",sentinels); Jedis jedis = sentinelPool.getResource(); System.out.println("current Host:" + sentinelPool.getCurrentHostMaster()); String key = "current key"; String cacheData = jedis.get(key); if (cacheData == null) { jedis.del(key); } jedis.set(key, "write data");// 写入 System.out.println(jedis.get(key));// 读取 System.out.println("current Host:" + sentinelPool.getCurrentHostMaster());// down掉master,观察slave是否被提升为master jedis.set(key, "bbb");// 测试新master的写入 System.out.println(jedis.get(key));// 观察读取是否正常 sentinelPool.returnResource(jedis); jedis.disconnect(); } }