温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot redis cluster集群的配置方法

发布时间:2021-09-10 10:18:44 来源:亿速云 阅读:285 作者:chen 栏目:大数据

本篇内容主要讲解“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<String, Object> 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<String, Object> redisTemplate(JedisConnectionFactory factory) {         RedisTemplate<String, Object> 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<String, Object> 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=true
spring.redis.cluster.connectionTimeout=60000 spring.redis.cluster.soTimeout=2000 spring.redis.cluster.maxAttempts=5

到此,相信大家对“springboot redis cluster集群的配置方法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI