本篇內(nèi)容主要講解“springboot redis cluster集群的配置方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“springboot redis cluster集群的配置方法”吧!
創(chuàng)新互聯(lián)專注于濟(jì)水街道網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供濟(jì)水街道營(yíng)銷型網(wǎng)站建設(shè),濟(jì)水街道網(wǎng)站制作、濟(jì)水街道網(wǎng)頁(yè)設(shè)計(jì)、濟(jì)水街道網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造濟(jì)水街道網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供濟(jì)水街道網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
上代碼:
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) { Mapsource = 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=true spring.redis.cluster.connectionTimeout=60000 spring.redis.cluster.soTimeout=2000 spring.redis.cluster.maxAttempts=5
到此,相信大家對(duì)“springboot redis cluster集群的配置方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!