springboot redis cluster集群的配置方法
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,本篇内容主要讲解"springboot redis cluster集群的配置方法",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot red
千家信息网最后更新 2025年12月03日springboot redis cluster集群的配置方法
本篇内容主要讲解"springboot redis cluster集群的配置方法",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot redis cluster集群的配置方法"吧!
上代码:
import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.MapPropertySource; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import com.gta.yyyf.commons.utils.SpringContextHolder;import redis.clients.jedis.JedisPoolConfig;import java.util.HashMap; import java.util.Map;@Configuration @EnableCaching public class RedisClusterConfig extends CachingConfigurerSupport { private JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow, boolean testOnReturn) { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(maxIdle); jedisPoolConfig.setMaxTotal(maxTotal); jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); jedisPoolConfig.setTestOnBorrow(testOnBorrow); jedisPoolConfig.setTestOnReturn(testOnReturn); return jedisPoolConfig; } private RedisClusterConfiguration redisCluterConfig(String clusterNodes) { Map source = new HashMap<>(); source.put("spring.redis.cluster.nodes", clusterNodes); return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); } @Bean(name = "redisConnectionFactory") public JedisConnectionFactory redisConnectionFactory(@Value("${spring.redis.cluster.maxIdle}") int maxIdle, @Value("${spring.redis.cluster.maxTotal}") int maxTotal, @Value("${spring.redis.cluster.maxWaitMillis}") long maxWaitMillis, @Value("${spring.redis.cluster.testOnBorrow}") boolean testOnBorrow, @Value("${spring.redis.cluster.testOnReturn}") boolean testOnReturn, @Value("${spring.redis.cluster.nodes}") String clusterNodes, @Value("${spring.redis.cluster.password}") String password) { JedisConnectionFactory connectionFactory = new JedisConnectionFactory( redisCluterConfig(clusterNodes), poolConfig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow, testOnReturn)); connectionFactory.setPassword(password); return connectionFactory; } @Bean(name = "stringRedisCluterTemplate") public StringRedisTemplate stringRedisTemplate() { StringRedisTemplate template = new StringRedisTemplate(); JedisConnectionFactory jedisConnectionFactory = SpringContextHolder.getBean("redisConnectionFactory"); template.setConnectionFactory(jedisConnectionFactory); return template; } @Bean("redisCluterTemplate") public RedisTemplate redisTemplate(JedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); StringRedisSerializer keySerializer = new StringRedisSerializer(); GenericJackson2JsonRedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer(); template.setKeySerializer(keySerializer); template.setHashKeySerializer(keySerializer); template.setValueSerializer(valueSerializer); template.setHashValueSerializer(valueSerializer); template.afterPropertiesSet(); return template; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager manager = new RedisCacheManager(redisTemplate); manager.setDefaultExpiration(10); return manager; }}配置文件:spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381 spring.redis.cluster.timeout=2000 spring.redis.cluster.max-redirects=100 spring.redis.cluster.password=123456 spring.redis.cluster.maxIdle=200 spring.redis.cluster.maxTotal=1000 spring.redis.cluster.maxWaitMillis=2000 spring.redis.cluster.testOnBorrow=true spring.redis.cluster.testOnReturn=truespring.redis.cluster.connectionTimeout=60000 spring.redis.cluster.soTimeout=2000 spring.redis.cluster.maxAttempts=5 到此,相信大家对"springboot redis cluster集群的配置方法"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
方法
配置
集群
内容
学习
实用
更深
上代
兴趣
实用性
实际
操作简单
文件
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
甲骨文数据库标准版
自动生成数据库测试数据库
仙梦奇缘怎么找之前的服务器
网络技术降低成本
怀旧服转服务器东西都在吗
服务器管理界面怎么登录密码
潍坊直播软件开发企业
零基础自学软件开发需要多久
开展网络安全自查整个活动
数据库课程设计安工大
蜗牛软件开发靠谱吗
网络安全渗透测试视频教程
跨境数据流动与网络安全论坛
西宁展厅互动软件开发公司
软件开发10年感想
网络技术及数字技术
服务器安全证书过期还能用吗
传统网络安全
如何使用宏录入数据库
kafka数据库
平安人寿软件开发招聘
网络安全科普文
数据库是数组吗
特朗普谈网络安全局
数据库椒做什么用的
罗湖区光纤网络技术开发动态
数据库 排他锁
用友数据库66中提取逻辑页失败
越来越红的网络安全
数据库电脑名称更改