springboot+redistemplate 集群配置
redisConfig 配置类
package com.zhouzhou.springredistemplatedemo.controller;import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
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 javax.annotation.Resource;
import java.time.Duration;@Configuration
public class RedisConfig {@Resourceprivate RedisProperties redisProperties;/*** 对象 redistemplate** @param factory* @return*/@Bean(name = "redisTemplate")public RedisTemplate<String, Student> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Student> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(factory); // 设置工厂redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 设置 value 为jackson序列化redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // 设置 hashvalue 为jackson序列化// redisTemplate.afterPropertiesSet(); //非spring注入,需要加上这一句return redisTemplate;}/*** 字符串 redistemplate** @param factory* @return*/@Bean(name = "stringRedisTemplate")public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);redisTemplate.setConnectionFactory(factory); // 设置工厂redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 设置 value 为jackson序列化redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); // 设置 hashvalue 为jackson序列化// redisTemplate.afterPropertiesSet(); //非spring注入,需要加上这一句return redisTemplate;}/*** 集群才需要配置,单机配置会报错* @return*/@Beanpublic RedisConnectionFactory redisConnectionFactory() {RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());redisClusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));//开始自适应集群拓扑刷新ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder().enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT,ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS,ClusterTopologyRefreshOptions.RefreshTrigger.ASK_REDIRECT).adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)).build();ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder().validateClusterNodeMembership(false).topologyRefreshOptions(clusterTopologyRefreshOptions).build();LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder().clientOptions(clusterClientOptions).build();return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration);}
}
springboot+redistemplate 集群配置相关推荐
- 【SpringBoot】SpringBoot + Redis集群配置(项目记录)
开发过程中首次使用了redis,也是一点一点边学边开发,期间遇到了一些坑所以做下记录防止以后忘记. 首先代码里只用到了单机版的redis,但因为后续数据量的问题改成了集群版,这里记录集群版的基本配置, ...
- springBoot整合redis集群配置
最近发现这篇博客阅读量比较大,今天特意抽空创建了一个可运行的开源项目. 项目的代码和之前的博客内容相比,做了些优化,请大家参考项目源码. 开源项目源码: springboot-redis-cluste ...
- springBoot整合redis单节点、redis哨兵、redis集群配置及redisClient区别
springBoot整合redis单节点.redis哨兵.redis集群配置,redisClient jedis lettuce 区别? 1.springboot 整合redis单机模式: sprin ...
- 基于redis的cas集群配置(转)
1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...
- Redis整合springboot实现集群模式
整体结构 Redis.config package com.cc.springredis.config;import com.cc.springredis.RedisUtil; import org. ...
- windows版本下的 redis 集群配置
windows下的redis配置 https://www.cnblogs.com/thirteen-zxh/p/9187875.html ( 集群后篇) https://www.cnblogs.com ...
- 分布式与微服务系列(三)、SpringBoot+Zookeeper集群+Nginx反向代理+Dubbo分布式托管(提供者、消费者)
SpringBoot+Zookeeper集群+Nginx反向代理+Dubbo分布式托管(提供者.消费者) 一.软件架构和微服务需求 1.1.微服务需求 1.2.框架选择 1.3.集群分布(下面为此图实 ...
- SpringCloud实现Redis集群配置
在配置文件中添加以下 spring:redis:cluster:# 各 Redis 节点信息nodes: 密码# 执行命令超时时间command-timeout: 15000# 重试次数max-att ...
- Hadoop集群配置(最全面总结)
Hadoop集群配置(最全面总结) 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为DataNode也作为Ta ...
最新文章
- 【FFmpeg】如何通过字符串到对应的封装器,以flv为例
- 树莓派开发4-串口通讯wiringpi库
- np.array_split可以不均等划分 np.split为均等划分
- 反思代码优化点:充分使用错误处理机制
- 华为Mate 30 Pro最新渲染图曝光:六摄造型 越看越顺眼了?
- 三分钟学会.NET微服务之Polly
- 实现CentOS 中的单窗口打开文件夹
- 关于QQ非会员发图限制
- 不小心发现谷歌 Firebase 消息服务的漏洞,获奖3万+美元
- C++回声服务器_6-多进程pipe版本服务器
- 代码下移快捷键_收藏细看!最全面的通达信快捷键一览
- Android UI学习之RadioButton和RadioGroup
- 用计算机函数,信息技术应用 用计算机画函数图象教案设计(一等奖)
- Pygame下载教程
- 搭建结构光三维扫描仪(基于TI资料,PointGrey相机+DLP投影仪搭建成的)
- html整体字体微软雅黑,网页布局中对全局字体的最佳控制_html/css_WEB-ITnose
- Android DataBinding 详解
- php phalcon 中文手册,基础教程 · Phalcon 3.4中文手册 · 看云
- 基于MATLAB的filter的使用,低通、带通和高通滤波器设计
- 黑白照片如何上色?AI智能一键上色
热门文章
- 谷歌IO大会全面硬钢微软+OpenAI
- 工具软件界的奇葩公司-Ashampoo
- 谷歌收购摩托罗拉掀开移动互联网大战帷幕
- Javascript属性:addEvent()用法浅析
- iphone换android系统更新不了,苹果支持安卓以旧换新,却被批心不诚,你会用安卓换苹果吗?...
- 翼码张波O2O分享9:O2O的其他商务行为
- pythonbb平台答案_微生物BB平台作业和答案
- AjaxUpLoad.js文件上传插件的使用
- 前端时间格式2020-11-18T22:42:48.000+00:00 转化成正常格式
- 计算机组成原理算术运算实验报告,《计算机组成原理》实验报告---8位算术逻辑运算实验.doc...