spring-boot使用lettuce redis客户端的方法
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要介绍"spring-boot使用lettuce redis客户端的方法",在日常操作中,相信很多人在spring-boot使用lettuce redis客户端的方法问题上存在疑惑,小编查阅
千家信息网最后更新 2026年11月01日spring-boot使用lettuce redis客户端的方法
这篇文章主要介绍"spring-boot使用lettuce redis客户端的方法",在日常操作中,相信很多人在spring-boot使用lettuce redis客户端的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"spring-boot使用lettuce redis客户端的方法"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
config类:
package net.loyin.cloud.upms.config;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.*;import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;import java.util.HashSet;import java.util.Set;@Configuration@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisConfig { @Value("${spring.redis.database:0}") private int database; @Value("${spring.redis.host:localhost}") private String host; @Value("${spring.redis.nodes:}") private String clusterNodes; @Value("${spring.redis.password:}") private String password; @Value("${spring.redis.port:6379}") private int port; @Value("${spring.redis.timeout:1000}") private long timeout; @Value("${spring.redis.lettuce.pool.max-idle:8}") private int maxIdle; @Value("${spring.redis.lettuce.pool.min-idle:8}") private int minIdle; @Value("${spring.redis.lettuce.pool.max-active:10}") private int maxActive; @Value("${spring.redis.lettuce.pool.max-wait:-1}") private long maxWait; /** * 配置自定义redisTemplate * * @return */ @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 /*Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper);*/ //使用StringRedisSerializer来序列化和反序列化redis的key值 StringRedisSerializer stringRedisSerializer=new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setValueSerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } @Bean LettuceConnectionFactory redisConnectionFactory() { RedisConfiguration redisConfiguration = null; if (clusterNodes != null&&"".equals(clusterNodes)==false) { // 集群版配置 RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(); String[] serverArray = clusterNodes.split(","); Set nodes = new HashSet(); for (String ipPort : serverArray) { String[] ipAndPort = ipPort.split(":"); nodes.add(new RedisNode(ipAndPort[0].trim(), Integer.valueOf(ipAndPort[1]))); } redisClusterConfiguration.setPassword(RedisPassword.of(password)); redisClusterConfiguration.setClusterNodes(nodes);// redisClusterConfiguration.setMaxRedirects(maxRedirects); redisConfiguration = redisClusterConfiguration; } else { // 单机版配置 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setDatabase(database); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); if (password != null && "".equals(password) == false) { redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); } redisConfiguration = redisStandaloneConfiguration; } GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxIdle(maxIdle); poolConfig.setMinIdle(minIdle); poolConfig.setMaxTotal(maxActive); poolConfig.setMaxWaitMillis(maxWait); LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofMillis(timeout)) .poolConfig(poolConfig) .build(); LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConfiguration, clientConfig); return factory; }} yml文件:
spring: redis: database: 0 # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0 host: 192.168.86.130 port: 6379 lettuce: # netty 线程安全的reids客户端 pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8 max-idle: 8 # 连接池中的最大空闲连接 默认 8 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 min-idle: 0 # 连接池中的最小空闲连接 默认 0 timeout: 10000 # 连接超时(ms)
如上配置,使用了连接池。
pom.xml如下:
4.0.0 net.loyin.study redis 1.0-SNAPSHOT aliyun-repos https://maven.aliyun.com/repository/public false 1.8 2.1.7.RELEASE org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE org.projectlombok lombok 1.18.8 compile org.springframework.boot spring-boot-starter-web ${spring.boot.version} org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-data-redis ${spring.boot.version} org.springframework.boot spring-boot-starter-test test org.apache.commons commons-pool2 org.apache.maven.plugins maven-deploy-plugin ${maven-deploy-plugin.version} true org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version}
到此,关于"spring-boot使用lettuce redis客户端的方法"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
客户
方法
配置
端的
序列
学习
最大
更多
空闲
负值
帮助
限制
实用
最小
安全
接下来
下有
单机
单机版
如上
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网和科技技术
互联网金融监管科技数据
互联网最新的科技有限公司
科技类互联网
互联网公司 科技公司
互联网科技小额贷款
深圳艾佳互联网科技
互联网科技类展会
盛创互联网科技有限公司
佩信高科技互联网
山东吉时兔互联网科技有限公司
互联网科技与管理
互联网科技大奖都有哪些公司
互联网科技研发水平
中国互联网公司排名锤子科技
佛山市翊博互联网络科技有限公司
领航科技大厦互联网公司
本届互联网大会上的黑科技
容联科技互联网电商
互联网公司属于科技吗
疫情对互联网科技公司影响
互联网科技缩写
阿里互联网科技公司
杭州壁岐互联网科技有限公司
搜狐科技互联网
山东有哪些互联网科技公司
广东工业互联网大赛深圳学泰科技
广州互联网高科技取餐柜
互联网科技机房
米泉互联网科技专业怎么样