【spring-boot - 整合Reids集群】
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot与Redis整合依赖-->
<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>
2.配置application.properties
server.port=8080
server.servlet.encoding.charset=UTF-8
# 应用名称
spring.application.name=redis-sample
spring.jackson.date-format='yyyy-MM-dd HH:mm:ss'
spring.jackson.time-zone=GMT+8
# non_null - 字段值非空才返回该字段,否则返回体中不包含该字段
# always - 始终包含该属性,与该属性的值无关
spring.jackson.default-property-inclusion=always
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# ===============================redis cluster nodes config=================================
# 最大的要重定向的次数
spring.redis.cluster.max-redirects=3
spring.redis.cluster.nodes=192.168.0.111:6379,192.168.0.112:6379,192.168.0.113:6379,192.168.0.114:6379,192.168.0.115:6379,192.168.0.116:6379
# 连接密码
spring.redis.password=123456
# 连接池配置,这里使用的lettuce
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
# lettuce 支持集群拓扑动态感知刷新,自适应拓扑刷新是否使用所有可用的更新,默认关闭:false,改为:true
spring.redis.lettuce.cluster.refresh.adaptive=true
# 定时刷新,单位: 毫秒ms
spring.redis.lettuce.cluster.refresh.period=2000
3.出于序列化考虑,编写配置类:RedisConfig,指定RedisTemplate对应的key-value序列化方式
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//设置key序列化方式: string
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置value的序列化方式json,使用 GenericJackson2JsonRedisSerializer 替换默认序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
4.使用
@Autowired
private RedisTemplate redisTemplate;
// 写入
redisTemplate.opsForValue().set(key, value);
// 获取
Object obj = redisTemplate.opsForValue().get(key);
spring-boot - 整合Reids集群
于 2023-04-15 00:37:53 首次发布
文章介绍了如何在SpringBoot项目中整合Redis集群,包括添加相关依赖、配置application.properties文件以设定Redis集群节点、连接池参数以及密码,同时提供了自定义RedisTemplate以处理序列化的方式。此外,还展示了如何在代码中使用RedisTemplate进行数据的读写操作。
10万+

被折叠的 条评论
为什么被折叠?



