1、pom文件引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2、RedisConnect类
/**
* @author Han, WeChat:autumn6980
* @date 2021/4/10
*/
public class RedisConnect {
// Redis服务器节点信息
// ip替换成你的真实ip或者域名即可
private static final String[] nodeConfigs = {"ip1:6379", "ip2:6379"};
// Redis访问用户名
private static final String username = "";
// Redis访问密码
private static final String password = "";
private static final JedisCluster jedisCluster;
static {
Set<HostAndPort> nodes = new HashSet<>();
for (String node : nodeConfigs) {
String[] parts = StringUtils.split(node, ":");
Assert.state(parts.length == 2, "redis node should be defined as 'host:port', not '" + node + "'");
nodes.add(new HostAndPort(parts[0], Integer.parseInt(parts[1])));
}
// 没有配置连接池使用默认的连接池
// return new JedisCluster(nodes);
// 创建集群对象
if (StringUtils.isNotEmpty(password)) {
jedisCluster = new JedisCluster(nodes, 6000, 1500, 3, username, password, "default", jedisPoolConfig());
} else {
jedisCluster = new JedisCluster(nodes, 6000, jedisPoolConfig());
}
}
public static JedisCluster getConnection() {
return jedisCluster;
}
public static JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMinIdle(5);
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setMaxWaitMillis(24000);
return jedisPoolConfig;
}
}
3、RedisClientFactory类
/**
* @author Han, WeChat:autumn6980
* @date 2021/4/10
*/
public class RedisClientFactory {
public static JedisCluster defaultClient() {
return RedisConnect.getConnection();
}
}
4、RedisUtil类
这里只给了get和set的示例
/**
* @author Han, WeChat:autumn6980
* @date 2021/4/10
*/
public class RedisUtil {
private static final String OK = "OK";
private static JedisCluster redis() {
return RedisClientFactory.defaultClient();
}
public static boolean set(String key, String value) {
String result = redis().set(key, value);
return OK.equals(result);
}
public static boolean set(String key, String value, int secondsToExpire) {
SetParams setParams = new SetParams();
setParams.ex(secondsToExpire);
String result = redis().set(key, value, setParams);
return OK.equals(result);
}
public static boolean set(String key, Object value) {
String json = JSON.toJSONString(value);
return set(key, json);
}
public static boolean set(String key, Object value, int secondsToExpire) {
String json = JSON.toJSONString(value);
return set(key, json, secondsToExpire);
}
public static <T> T get(String key, TypeReference<T> type) {
String result = redis().get(key);
return JSON.parseObject(result, type);
}
public static String get(String key) {
return redis().get(key);
}
public static void main(String[] args) {
set("currentDatetime", new Date(), 60);
Date currentDatetime = get("currentDatetime", new TypeReference<Date>() {
});
System.out.println(currentDatetime);
}
}
http://www.hanyehong.com/2021/04/11/SpringBoot%E4%BD%BF%E7%94%A8Jedis%E8%AE%BF%E9%97%AERedis/