@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
 
 

springboot+rediscluster的更多相关文章

  1. springBoot集成redisCluster

    本文主要内容:springBoot简介,在SpringBoot中如何集成Redis,可配置Redis集群. 关于SpringBoot 你想要的,这里都有:https://spring.io/proje ...

  2. 第三章 springboot + jedisCluster

    如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1)  第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...

  3. Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置

    Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...

  4. 【第三章】 springboot + jedisCluster

    如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1)  第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...

  5. SpringBoot之整合Redis

    一.SpringBoot整合单机版Redis 1.在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework ...

  6. Docker实战之Redis-Cluster集群

    概述 接上一篇Docker实战之MySQL主从复制, 这里是Docker实战系列的第二篇,主要进行Redis-Cluster集群环境的快速搭建.Redis作为基于键值对的NoSQL数据库,具有高性能. ...

  7. 这 5 个开源的能挣钱的 SpringBoot 项目,真TMD香!

    不得不佩服 Spring Boot 的生态如此强大,今天我给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统,小伙伴们再也不用从头到尾撸一个项目了,简直就是接私活,挣钱的利器啊. SmartA ...

  8. SpringBoot配置文件 application.properties,yaml配置

    SpringBoot配置文件 application.properties,yaml配置 1.Spring Boot 的配置文件 application.properties 1.1 位置问题 1.2 ...

  9. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

随机推荐

  1. MapReduce输出文件名更改

    默认情况下生成的文件名是part-r-00000格式,想要自定义生成输出文件名可以使用org.apache.hadoop.mapreduce.lib.output.MultipleOutputs类用来 ...

  2. 什么是DAPP

    DAPP(Decentralized Application)去中心化的应用 DAPP可以是网站,也可以是手机app,只要主要逻辑和数据在区块链上就可以 在以太坊平台上,一个DAPP肯定基于一个或多个 ...

  3. scikit-learn 决策树 分类问题

    1.Demo from sklearn import tree import pydotplus import numpy as np #李航p59表数据 #年龄,有工作,有自己房子,信贷情况,类别 ...

  4. workqueue --最清晰的讲解【转】

    转自:https://www.cnblogs.com/zxc2man/p/6604290.html 带你入门: 1.INIT_WORK(struct work_struct *work, void ( ...

  5. 新闻API接口

    最近很多大学生为了完成作业,或者刚开始接触android的问我要新闻APP的源码,其实就是个很小的demo,以前自己也是学着别人敲得的代码,现在自己整理了一份体积很小,代码注释非常详细的新闻APP.提 ...

  6. Jquyer相册

    点击图片然后弹出相册列表,效果如下: html代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml&q ...

  7. PHP实现微信商户支付企业付款到零钱功能代码实例

    本文为大家分享了PHP实现微信商户支付企业付款到零钱的具体代码,供大家参考,具体内容如下 微信支付开发文档 一.开通条件 企业付款为企业提供付款至用户零钱的能力,支持通过API接口付款,或通过微信支付 ...

  8. .net core2.x - 关于工作单元(UnitOfWork) 模式

    概要:在搭建框架,顺手说下写下,关于unitofwork,可能你理解了,可能你还不理解,可能与不可能不是重点,重点是感兴趣就看看吧. 1.工作单元(unitofowork)是什么(后面简写uow)? ...

  9. 快速搭建MQTT服务器(MQTTnet和Apache Apollo)

    前言 MQTT协议是IBM开发的一个即时通讯协议,有可能成为物联网的重要组成部分,http://mqtt.org/. MQTT is a machine-to-machine (M2M)/" ...

  10. python TextMining

    01.Crawling url_request # -*- coding: utf-8 -*- """ Created on Sun Feb 17 11:08:44 20 ...