前言
一、 springboot 版本
<spring-boot.version>2.3.5.RELEASE</spring-boot.version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
二、引入 redis 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
三、增加配置文件
spring:
redis:
cluster:
nodes: 192.168.162.235:6379,192.168.162.235:6380,192.168.162.235:6381,192.168.162.235:6382,192.168.162.235:6383,192.168.162.235:6384
max-redirects: 3
pool:
#最大空闲连接
max-idle: 8
#最小空闲连接
min-idle: 0
#最大连接数,-1表示是没有限制
max-active: 8
#最大阻塞等待时间,-1表示没有限制
max-wait: -1
#连接超时时间(毫秒)
timeout: 60000
commandTimeout: 5000
password: 123456
connectionTimeout: 60000```
四、增加配置类
1、 RedissonConfig
@Configuration
public class RedissonConfig {
@Value("${spring.redis.cluster.nodes}")
private String[] nodes;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.connectionTimeout}")
private int connectionTimeout;
@Bean(destroyMethod = "shutdown")
public RedissonClient redissonClient() {
Config config = new Config();
ClusterServersConfig clusterServersConfig = config.useClusterServers();
clusterServersConfig.addNodeAddress(Stream.of(nodes).map((node) -> "redis://" + node)
.toArray(String[]::new))
.setConnectTimeout(connectionTimeout);
if (StringUtils.isNotBlank(password)) {
clusterServersConfig.setPassword(password);
}
return Redisson.create(config);
}
}
2、RedisConfig
@Configuration
public class RedisConfig {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jacksonSeial.setObjectMapper(om);
template.setValueSerializer(jacksonSeial);
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
五、增加操作类,主要操作 string
Setter
@Service
public class StringRedisTemplateClient {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Deprecated
public Boolean expire(String key, Integer seconds) {
Boolean succeed=stringRedisTemplate.expire(key, Duration.ofSeconds(seconds));
return succeed;
}
public Boolean expire(String key, long timeout, TimeUnit unit) {
Boolean succeed=stringRedisTemplate.expire(key, timeout,unit);
return succeed;
}
public String get(String key) {
String value=stringRedisTemplate.opsForValue().get(key);
return value;
}
public void set(String key, String value, long timeout, TimeUnit unit) {
stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
}
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
public void set(String key, Object value) {
String jsonStr = JSONObject.toJSONString(value);
set(key,jsonStr);
}
public void setex(String key, String value, Integer seconds) {
stringRedisTemplate.opsForValue().set(key, value, Duration.ofSeconds(seconds));
}
public void setex(String key, String value, Long seconds) {
stringRedisTemplate.opsForValue().set(key, value, Duration.ofSeconds(seconds));
}
public void setex(String key, Object value, Integer seconds) {
String jsonStr = JSONObject.toJSONString(value);
setex(key,jsonStr,seconds);
}
public long ttl(String key) {
Long expiresIn = stringRedisTemplate.opsForValue().getOperations().getExpire(key, TimeUnit.SECONDS);
return expiresIn;
}
public <T> T get(String key, Class<T> clazz) {
String jsonStr = get(key);
return JSONObject.parseObject(jsonStr, clazz);
}
public <T> List<T> getList(String key, Class<T> clazz) {
String jsonStr = get(key);
return JSONObject.parseArray(jsonStr, clazz);
}
public boolean exist(String key) {
String value = get(key);
return StringUtils.isNotBlank(value);
}
public Boolean delete(String key) {
Boolean succeed=stringRedisTemplate.delete(key);
return succeed;
}
public Long incr(String key) {
Long increment = stringRedisTemplate.opsForValue().increment(key);
return increment;
}
}
总结