@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. Ansible------角色

    什么是角色 角色是一种解决问题的思想,也是一种规范. 目录 角色的目录结构如下: files: 存放由copy或script模块等调用的文件. templates: template模块查找所需要模板 ...

  2. 往服务器部署thinkphp5代码时要注意 pathinfo的问题

    往服务器部署thinkphp5代码时要注意 pathinfo的问题 如果nginx没有做任何设置 要使用?s=/的方式访问地址 只需要修改3个地方就可以了,亲测成功,看代码有注解 location ~ ...

  3. 可持久化并(xian)查(duan)集(shu)

    随便地点开了这道可持久化并查集,发现了真相...这和并查集有 PI 关系哦.除了find_father(而且还不能路径压缩),全都是线段树0.0 题目链接: luogu.org 题目没什么描述,就是三 ...

  4. how2heap学习笔记

    github源代码地址 这里只分析glibc2.25堆分配的特性,为了方便调试编译时使用 gcc -g -no-pie <input_file_name> -o <output_fi ...

  5. redis/memcache监控管理工具——treeNMS

    TreeNMS可以帮助您搭建起一套用于redis的监控管理系统,也支持Memcached,让您可以通过web的方式对数据库进行管理,有了它您就可以展示NOSQL数据库.编辑修改内容,另外还配备了sql ...

  6. 服务器启动socket服务报错 java.net.BindException:Cannot assign requested address

    错误信息:  2017-06-13 11:18:00,865 [GateServer.java:82][ERROR]:启动服务出错了java.net.BindException: Cannot ass ...

  7. OpenCV-Python:形态学操作

    常用的形态学操作:腐蚀.膨胀.开运算和闭运算 一.什么叫形态学操作 形态学操作就是改变物体的形状,比如腐蚀就是"变瘦",膨胀就是"变胖" 形态学操作一般作用于二 ...

  8. SpringBoot之hello world!

    哈哈哈,还是在元旦这一天对你下手了.麻溜的给自己充电,在这个寒冬,不断听到裁员的消息或者新闻,可对于我这个外包和外派的人来说,好像并没有受到什么影响.可能是人手不够可能是项目很忙.对于明年的三月金四月 ...

  9. Classification

    kNN1 # -*- coding: utf-8 -*- """ kNN : 최근접 이웃 """ import numpy as np # ...

  10. STL复习之 map & vector --- disney HDU 2142

    题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...