• springboot+rediscluster


    @EnableCaching
    @Configuration
    public class RedisConfiguration extends CachingConfigurerSupport {
    
        @Autowired
        private RedisClusterProperties redisClusterProperties;
    
        @Bean
        public RedisConnectionFactory connectionFactory() {
    
            String nodes = redisClusterProperties.getNodes();
            List<String> nodeList = new ArrayList<>();
            String[] split = nodes.split(",");
            for (String node : split) {
                nodeList.add(node);
            }
    
            GenericObjectPoolConfig config = new GenericObjectPoolConfig();
            config.setMaxTotal(redisClusterProperties.getMaxActive());
            config.setMaxIdle(redisClusterProperties.getMaxIdle());
            config.setMinIdle(redisClusterProperties.getMinIdle());
            config.setMaxWaitMillis(redisClusterProperties.getMaxWaitMillis());
    
            LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                    .poolConfig(config)
                    .commandTimeout(Duration.ofMillis(redisClusterProperties.getTimeout()))
                    .build();
    
            RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(nodeList);
            clusterConfig.setMaxRedirects(3);
    
            return new LettuceConnectionFactory(
                    clusterConfig,clientConfig);
    
        }
    
        @Bean
        public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
    
            return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory),
                    this.getRedisCacheConfigurationWithTtl(3600),
                    this.getRedisCacheConfigurationMap()
            );
        }
    
        @Bean
        @Override
        public KeyGenerator keyGenerator() {
            return new KeyGenerator() {
                @Override
                public Object generate(Object target, Method method, Object... params) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(target.getClass().getName());
                    sb.append(method.getName());
                    for (Object obj : params) {
                        sb.append(obj.toString());
                    }
                    return sb.toString();
                }
            };
        }
    
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {
    
            // 配置redisTemplate
            RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(connectionFactory);
    
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.QuoteFieldNames);
            FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
            fastJsonRedisSerializer.setFastJsonConfig(config);
    
            RedisSerializer<?> stringSerializer = new StringRedisSerializer();
            //GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
            redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);
            redisTemplate.setKeySerializer(stringSerializer);
            redisTemplate.setValueSerializer(fastJsonRedisSerializer);
            redisTemplate.setHashKeySerializer(stringSerializer);
            redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
            return redisTemplate;
        }
    
        private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
    
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.QuoteFieldNames);
            FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
            fastJsonRedisSerializer.setFastJsonConfig(config);
    
            //GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
    
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
            redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                    RedisSerializationContext
                            .SerializationPair
                            .fromSerializer(fastJsonRedisSerializer)
            ).entryTtl(Duration.ofSeconds(seconds));
    
            return redisCacheConfiguration;
        }
    
        private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
            Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
            //redisCacheConfigurationMap.put("city:shard", this.getRedisCacheConfigurationWithTtl(43200));
            //redisCacheConfigurationMap.put("city:route:conf", this.getRedisCacheConfigurationWithTtl(43200));
            //redisCacheConfigurationMap.put("city:node", this.getRedisCacheConfigurationWithTtl(43200));
    
            return redisCacheConfigurationMap;
        }
    
    }
    redis.cluster.nodes=10.202.114.65:9168,10.202.114.65:9169,10.202.114.65:9170
    
    
    redis.cluster.timeout=5000
    
    
    redis.cluster.maxIdle=10
    
    
    redis.cluster.minIdle=2
    
    
    redis.cluster.maxActive=100
    
    
    redis.cluster.maxWaitMillis=5000
     
     
  • 相关阅读:
    java怎样将一组对象传入Oracle存储过程
    android webview内容压线问题解决方法
    BS和CS对比
    【OpenCV-Python】Python Extension Packages for Windows
    hdu4462 Scaring the Birds
    tomcat安全配置之证书密码加密存储
    UVA 10714 Ants 蚂蚁 贪心+模拟 水题
    一个不喜欢读书的Javaer的读书单
    二叉树可视化--Graphviz
    [置顶] mmog游戏开发之业务篇
  • 原文地址:https://www.cnblogs.com/gendway/p/10698440.html
Copyright © 2020-2023  润新知