二十三、Redis Sentinel客户端
程序员文章站
2022-06-06 09:50:19
...
Redis Sentinel客户端
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
package com.yang;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
public class RedisSentinelFailOver {
public static void main(String[] args) {
String masterName="mymaster";
Set<String> sentinels=new HashSet<String>();
sentinels.add("192.168.1.107:26379");
sentinels.add("192.168.1.107:26380");
sentinels.add("192.168.1.107:26381");
JedisSentinelPool pools=new JedisSentinelPool(masterName, sentinels);
int counter=0;
while(true) {
counter++;
Jedis jedis=null;
try {
jedis=pools.getResource();
int index=new Random().nextInt(10000);
String key="k-"+index;
String value="v-"+index;
jedis.set(key, value);
TimeUnit.MICROSECONDS.sleep(10);
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}finally {
if(jedis!=null) {
jedis.close();
}
}
}
}
}