五、java操作redis
系列导航
--demo主方法
package com.redis;
import com.JedisCluster;
import java.util.HashSet;
/**
* 描述:redis集群样例。
* <p>项目名称: redis集群</p>
* <p>文件名称: DemonMain.java</p>
*/
public class DemonMain {
static HashSet set = new HashSet();
private static JedisClusterFactory jedisClusterFactory = new JedisClusterFactory();
private static JedisCluster jedisCluster = jedisClusterFactory.getJedisCluster();
/**
* 描述:redis测试是的主函数。
*
* @param args 主函数的参数
*/
public static void main(String[] args) {
RedisService rs = new RedisService();
//普通的操作
jedisCluster.set("key", "0");
String value = jedisCluster.get("key");
}
}
//redis操作不同的数据集可以参考https://www.cnblogs.com/edisonfeng/p/3571870.html 写的比较全面
//本例主要提供一个连接redis集群的公共配置类
----连接配置类
package com.redis;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
/**
* 描述:redis集群样例。
* <p>项目名称: redis集群</p>
*/
public class JedisClusterFactory {
private static final String MODULE = JedisClusterFactory.class.getName();
JedisPoolConfig poolConfig;
private JedisCluster jedisCluster;
private int connectionTimeout = 1000;
private int soTimeout = 2000;
private int maxRedirections = 30;
private Set<String> jedisClusterNodes;
private static JedisClusterFactory instance = null;
/**
* 描述:redis的构造函数(配置信息)。
*/
public JedisClusterFactory() {
try {
Set<HostAndPort> haps = new HashSet<HostAndPort>();
String hostIp = "127.0.0.1";
haps.add(new HostAndPort(hostIp, 7001));
haps.add(new HostAndPort(hostIp, 7002));
haps.add(new HostAndPort(hostIp, 7003));
haps.add(new HostAndPort(hostIp, 7004));
haps.add(new HostAndPort(hostIp, 7005));
haps.add(new HostAndPort(hostIp, 7006));
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
poolConfig.setMaxTotal(20);
// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
poolConfig.setMaxIdle(50);
poolConfig.setMinIdle(20);
// 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
poolConfig.setMaxWaitMillis(1000 * 100);
// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
poolConfig.setTestOnBorrow(true);
//密码
String password = "redispass";
jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, password, poolConfig);
//如下是不加密码的写法
//jedisCluster = new JedisCluster(haps, connectionTimeout, soTimeout, maxRedirections, poolConfig);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 描述:redis的构造函数(配置信息)。
*
* @return JedisClusterFactory redis集群的连接信息
*/
public static synchronized JedisClusterFactory getInstance() {
if (instance == null) {
instance = new JedisClusterFactory();
}
return instance;
}
public JedisPoolConfig getPoolConfig() {
return poolConfig;
}
public void setPoolConfig(JedisPoolConfig poolConfig) {
this.poolConfig = poolConfig;
}
public JedisCluster getJedisCluster() {
return jedisCluster;
}
public void setJedisCluster(JedisCluster jedisCluster) {
this.jedisCluster = jedisCluster;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public int getSoTimeout() {
return soTimeout;
}
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
public int getMaxRedirections() {
return maxRedirections;
}
public void setMaxRedirections(int maxRedirections) {
this.maxRedirections = maxRedirections;
}
public Set<String> getJedisClusterNodes() {
return jedisClusterNodes;
}
public void setJedisClusterNodes(Set<String> jedisClusterNodes) {
this.jedisClusterNodes = jedisClusterNodes;
}
}