一、JedisCluster方式(推荐)
jedisCluster专门用来连接redis集群
jedisCluster单例存在的
1、yml配置
spring:
redis:
password:
clusterNodes: xxx.xx.xx:8001, xxx.xx.xx:8002
expireSeconds: 120
commandTimeout: 10000 #redis操作的超时时间
pool:
maxActive: 5000 #最大连接数
maxIdle: 30 #最大空闲连接数
minIdle: 5 #最小空闲连接数
maxWait: 3000 #获取连接最大等待时间 ms #default -1
2、新增类RedisProperties
package com.example.redis_cluster.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisProperties {
private int expireSeconds;
private String clusterNodes;
private int commandTimeout;
}
3、JedisCluster方式连接集群的配置
package com.example.redis_cluster.config;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.HostAndPort;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class JedisClusterConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisCluster getJedisCluster() {
String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes,redisProperties.getCommandTimeout(),1000,1,new GenericObjectPoolConfig());
}
}
4、工具类RedisUtil
package com.example.redis_cluster.config;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
@Component
public class RedisUtil {
@Autowired
private JedisCluster jedisCluster;
/**
* 设置缓存
* @param key 缓存key
* @param value 缓存value
*/
public void set(String key, String value) {
jedisCluster.set(key, value);
}
/**
* 设置缓存对象
* @param key 缓存key
* @param obj 缓存value
*/
public <T> void setObject(String key, T obj , int expireTime) {
jedisCluster.setex(key, expireTime, JSON.toJSONString(obj));
}
/**
* 获取指定key的缓存
* @param key---JSON.parseObject(value, User.class);
*/
public String getObject(String key) {
return jedisCluster.get(key);
}
/**
* 判断当前key值 是否存在
*
* @param key
*/
public boolean hasKey(String key) {
return jedisCluster.exists(key);
}
/**
* 设置缓存,并且自己指定过期时间
* @param key
* @param value
* @param expireTime 过期时间
*/
public void setWithExpireTime( String key, String value, int expireTime) {
jedisCluster.setex(key, expireTime, value);
}
/**
* 获取指定key的缓存
* @param key
*/
public String get(String key) {
String value = jedisCluster.get(key);
return value;
}
/**
* 删除指定key的缓存
* @param key
*/
public void delete(String key) {
jedisCluster.del(key);
}
}
5、工具类使用
二、使用jedis连接池
1、yml配置
spring:
redis:
database: 0
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 10000
cluster:
nodes: xxx.xx.xx:8001, xxx.xx.xx:8002
command-timeout: 5000
expire-seconds: 120
2、连接池注入配置信息
package com.example.redis_cluster.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory factory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
3、工具类使用
@Resource(name="redisTemplate")
private RedisTemplate<String,String> redisTemplate;
@RequestMapping("/redisTest")
public String redisTest(){
return redisTemplate.opsForValue().get("name");
}
@RequestMapping("/getKey")
public String findRedis(String key) {
String value = redisTemplate.opsForValue().get(key);
return value;
}