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:9170redis.cluster.timeout=5000redis.cluster.maxIdle=10redis.cluster.minIdle=2redis.cluster.maxActive=100redis.cluster.maxWaitMillis=5000springboot+rediscluster的更多相关文章
- springBoot集成redisCluster
本文主要内容:springBoot简介,在SpringBoot中如何集成Redis,可配置Redis集群. 关于SpringBoot 你想要的,这里都有:https://spring.io/proje ...
- 第三章 springboot + jedisCluster
如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1) 第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...
- Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...
- 【第三章】 springboot + jedisCluster
如果使用的是redis2.x,在项目中使用客户端分片(Shard)机制.(具体使用方式:第九章 企业项目开发--分布式缓存Redis(1) 第十章 企业项目开发--分布式缓存Redis(2)) 如果 ...
- SpringBoot之整合Redis
一.SpringBoot整合单机版Redis 1.在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework ...
- Docker实战之Redis-Cluster集群
概述 接上一篇Docker实战之MySQL主从复制, 这里是Docker实战系列的第二篇,主要进行Redis-Cluster集群环境的快速搭建.Redis作为基于键值对的NoSQL数据库,具有高性能. ...
- 这 5 个开源的能挣钱的 SpringBoot 项目,真TMD香!
不得不佩服 Spring Boot 的生态如此强大,今天我给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统,小伙伴们再也不用从头到尾撸一个项目了,简直就是接私活,挣钱的利器啊. SmartA ...
- SpringBoot配置文件 application.properties,yaml配置
SpringBoot配置文件 application.properties,yaml配置 1.Spring Boot 的配置文件 application.properties 1.1 位置问题 1.2 ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
随机推荐
- Lua的内存管理
[前言] 在历史长河中,各种各样的新语言,总是伴随着我们编程人员:有的时候,工作的需要,我们不得不去学习这些很炫的,很新的语言.学习任何一门语言(我这里只说学习),都无非就是学习那么几个大模块,基本语 ...
- Filebeat工作原理
在这篇文章中,您可以了解Filebeat的关键构建模块以及它们如何一起工作.了解这些概念将有助于您针对特定用例对Filebeat进行配置做出明智的决定.Filebeat由两个主要组件组成: prosp ...
- HiveQl 基本查询
1 基本的Select 操作 SELECT [ALL | DISTINCT] select_expr, select_expr, ...FROM table_reference[WHERE where ...
- hdu4352 数位dp+状态压缩+一个tip
按照nlogn求lis的方法,把lis的状态压缩了,每次新加一个数就把它右边第一个数的位置置为0,然后把这个数加进去 一个需要注意的地方,如果前面都是0,那么状态s中代表0的位置不可以是1,因为这种情 ...
- table切换jquery插件 jQuery插件写法模板 流程
通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jQuery UI的部件工厂方式创建 通过$.extend()来扩展jQuery $. ...
- 树dp:边覆盖,点覆盖
#493. 求树的最小支配集 问题描述 对于一棵n个结点的无根树,求它的最小支配集. 最小支配集:指从所有顶点中取尽量少的点组成一个集合,使得剩下的所有点都与取出来的点有边相连.顶点个数最小的支配集被 ...
- bootstrap-datetimepicker的中文显示问题
bootstrap-datetimepicker的本地化显示依赖于moment插件.也就是说moment插件提供了多语言的内容支持,而bootstrap-datetimepicker没有语言内容. 为 ...
- mysql根据分组和条件查询以后如何统计记录的条数
1.子查询,查询出的数据随便起一个别名,然后根据分组和条件查询出的数据,作为一个具有一列的一个表,然后外面的查询查询这个数据表的这一列的总数,即可. SELECT COUNT( * ) FROM ( ...
- git本地项目上传至码云gitee
如果你的本机是安装成功第一次使用,先配置一下一些基本的信息 $ git config--global user.name "Your Name" $ git config --gl ...
- Python实现字符串反转的几种方法
面试遇到的一个特无聊的问题--- 要求:在Python环境下用尽可能多的方法反转字符串,例如将s = "abcdef"反转成 "fedcba" 第一种:使用字符 ...