1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package org.helium.redis.test; import org.junit.Test; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisSentinelPool; import sun.tools.tree.SynchronizedStatement;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.HashSet; import java.util.Set;
public class RedisSentinelTest { static RedisSentinelTest test = new RedisSentinelTest(); public static void main(String[] args) throws Exception { test1(); } public static void test1() throws IOException { File file = new File("/Users/wuhao/redis/redis-log"); if (file.exists()){ file.delete(); } file.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(file); JedisPoolConfig poolConfig = new JedisPoolConfig(); String masterName = "mymaster"; Set<String> sentinels = new HashSet<String>(); sentinels.add("10.10.220.149:17021"); sentinels.add("10.10.220.149:17022"); sentinels.add("10.10.220.149:17023"); JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, poolConfig);
for (int i = 0; i < 20; i++) {
Thread run = new Thread(new Runnable() { @Override public void run() { while (true){ Jedis redisJ = null; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } try { redisJ = jedisSentinelPool.getResource(); redisJ.set("a", "333"); String value = redisJ.get("a"); HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster(); String host = new Date() + "OK-"+ currentHostMaster.toString() + "\n"; fileOutputStream.write(host.getBytes()); } catch (Exception e){ e.printStackTrace(); try { fileOutputStream.write( ("ERROR-" + e.getMessage()).getBytes()); fileOutputStream.write("\n".getBytes()); } catch (IOException e1) { e1.printStackTrace(); } } finally { if (redisJ != null){ redisJ.close(); } }
} } }); run.start();; } try { Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); }
}
}
|
v1.5.2