开发过程中首次使用了redis,也是一点一点边学边开发,期间遇到了一些坑所以做下记录防止以后忘记。
首先代码里只用到了单机版的redis,但因为后续数据量的问题改成了集群版,这里记录集群版的基本配置,如果有不对的地方欢迎评论留言指正哈~

文章目录

  • 一、application.properties配置文件内容
  • 二、RedisConfig配置文件内容
  • 三、集群中不能使用pipeline操作的解决方法
  • PS(额外记录)

一、application.properties配置文件内容

首先在配置文件里写上配置redis集群的服务器和端口,这里用192.168.0.1、0.2、0.3三个服务器做集群。

spring.redis.cluster.nodes=192.168.0.1:7001,192.168.0.2:7002,192.168.0.3:7003logging.config=classpath:logback-spring.xml
logging.level.com.test.service=debug...

二、RedisConfig配置文件内容

这里是redis的配置类信息


@Configuration
public class RedisConfig {@Value("${spring.redis.cluster.nodes}")  private String nodes;  @Bean  public RedisTemplate<String, Object> redisTemplateObject() throws Exception {  RedisTemplate<String, Object> redisTemplateObject = new RedisTemplate<String, Object>();  redisTemplateObject.setConnectionFactory(redisConnectionFactory());  setSerializer(redisTemplateObject);  redisTemplateObject.afterPropertiesSet();  return redisTemplateObject;  } @Bean  public RedisConnectionFactory redisConnectionFactory(){  JedisPoolConfig poolConfig=new JedisPoolConfig();  //poolConfig.setMaxIdle(maxIdl);  //poolConfig.setMinIdle(minIdl);  poolConfig.setTestOnBorrow(true);  poolConfig.setTestOnReturn(true);  poolConfig.setTestWhileIdle(true);  poolConfig.setNumTestsPerEvictionRun(10);  poolConfig.setTimeBetweenEvictionRunsMillis(60000);  //JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);  JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisConfig(), poolConfig);  //jedisConnectionFactory.setHostName(hostName);   //jedisConnectionFactory.setPort(port);  //jedisConnectionFactory.setDatabase(database);  jedisConnectionFactory.setTimeout(100000);return jedisConnectionFactory;  } private void setSerializer(RedisTemplate<String, Object> template) {  Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(  Object.class);  ObjectMapper om = new ObjectMapper();  om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  jackson2JsonRedisSerializer.setObjectMapper(om);  template.setKeySerializer(template.getStringSerializer());  //template.setValueSerializer(jackson2JsonRedisSerializer);  //template.setHashValueSerializer(jackson2JsonRedisSerializer);  //在使用String的数据结构的时候使用这个来更改序列化方式  RedisSerializer<String> stringSerializer = new StringRedisSerializer(); //template.setKeySerializer(stringSerializer ); template.setValueSerializer(stringSerializer ); //template.setHashKeySerializer(stringSerializer ); template.setHashValueSerializer(stringSerializer ); } @Beanpublic RedisClusterConfiguration jedisConfig(){RedisClusterConfiguration config = new RedisClusterConfiguration();String[] nodeArr = nodes.split(",");List<RedisNode> nodeList = new ArrayList<>(nodeArr.length);String[] tem;for (String node : nodeArr){tem = node.split(":");nodeList.add(new RedisNode(tem[0], Integer.valueOf(tem[1])));}config.setClusterNodes(nodeList);return config;}@Beanpublic JedisCluster getJedisCluster(){String[] nodeArr = nodes.split(",");Set<HostAndPort> nodeSet = new HashSet<>();String[] tem;for (String node : nodeArr){tem = node.split(":");nodeSet.add(new HostAndPort(tem[0], Integer.valueOf(tem[1])));}return new JedisCluster(nodeSet);}}

三、集群中不能使用pipeline操作的解决方法

把redis单机版改为集群后,原本单机版代码里使用pipeline进行批量操作入库的代码就不能使用了。会提示提示org.springframework.data.redis.connection.jedis
.JedisClusterConnection
错误。

上网查了之后知道是pipeline不能在集群模式下使用的原因,但是不使用批量操作的话循环入库的速度又会受到影响。所以只能重写pipeline的方法,实现原来管道的操作。使用的是该链接里的重写类JedisClusterPipeline,直接拿过来用就可以。
https://blog.csdn.net/EndTheme_Xin/article/details/84623063

下面是单机版和集群版批量操作的代码对比

原来单机版的批量入库代码:

List<Object> list = redisTemplate.executePipelined(new RedisCallback<Object>() {@Overridepublic Object doInRedis(RedisConnection redisConnection) throws DataAccessException {redisConnection.openPipeline();for (String server : ipList){String key = server.split("|")[0];String value = server.split("|")[1];redisConnection.hSet(dateStr.getBytes(), key.getBytes(), value.getBytes());//dateStr是日期。redisConnection.expire(dateStr.getBytes(), expireTime);//expireTime是过期时间(秒)。}}
}, redisTemplate.getValueSerializer());

改成集群后的批量入库代码:

JedisClusterPipeline jcp = JedisClusterPipeline.pipelined(jedisCluster);//jedisCluster是配置类里声明的Bean
jcp.refreshCluster();
List<Object> batchResult = null;
try {for (String server : ipList){String key = ...;String value = ...;jcp.setex(key, expireTime.intValue(), value);//expireTime是过期时间(秒)}jcp.sync();batchResult = jcp.syncAndReturnAll();
} finally {jcp.close();j
}

PS(额外记录)

在一个类中引用properties文件里值的写法

@PropertySource(value = {"classpath:/config.properties","file:${spring.profiles.path}/config.properties"}, ignoreResourceNotFound = true)
public class Test{@Value("${log_path}")private String logPath;......@Scheduled(cron = "${cron_expression}")public void task(){......}
}

【SpringBoot】SpringBoot + Redis集群配置(项目记录)相关推荐

  1. springBoot整合redis集群配置

    最近发现这篇博客阅读量比较大,今天特意抽空创建了一个可运行的开源项目. 项目的代码和之前的博客内容相比,做了些优化,请大家参考项目源码. 开源项目源码: springboot-redis-cluste ...

  2. SpringBoot多redis集群配置

    在一个SpringBoot应用中配置多个redis集群 文章目录 1) 禁用自动配置 2)配置application.properties 3)增加java配置类 4)使用 1) 禁用自动配置 禁用 ...

  3. 基于springboot的redis集群配置

    使用yml配置文件形式配置redis连接, 也可直接使用RedisTemplate默认配置,就可不用创建以下代码内容,只需编写yml文件即可. 至此就可以使用 RedisTemplate的自带API进 ...

  4. springboot集成redis集群实现集群拓扑动态刷新

    一个redis-cluster的三主三从集群,在其中一个master节点挂了之后,springboot集成redis集群配置信息没有及时刷新,出现读取操作报错.下面聊聊如何实现springboot集成 ...

  5. springBoot整合redis单节点、redis哨兵、redis集群配置及redisClient区别

    springBoot整合redis单节点.redis哨兵.redis集群配置,redisClient jedis lettuce 区别? 1.springboot 整合redis单机模式: sprin ...

  6. springboot整合redis集群master宕机后连接超时

    前提: #        本文是在确保redis集群配置正确的情况下,连接超时的解决方案. 项目登录认证使用的是sa-token(这个不重要,主要说的是springboot和redis集群),最近应甲 ...

  7. springboot连接redis集群

    开启redis服务和客户端 查看下当前redis的进程 [root@localhost ~]# ps -ef | grep redis 启动redis服务 [root@localhost ~]# cd ...

  8. java集群解析文件_java相关:springboot整合redis集群过程解析

    java相关:springboot整合redis集群过程解析 发布于 2020-4-13| 复制链接 摘记: 简介 在springboot使用搭建好的redis集群添加redis和连接池依赖 ```x ...

  9. springboot篇】二十二. springboot整合Redis集群

    springboot整合Redis集群 **中国加油,武汉加油!** 案例准备 1. 新建Springboot项目springboot-redis-sentinel 2. 编写yml 3. Test ...

最新文章

  1. [iOS Animation]-CALayer 图层几何学
  2. C++中流的基本概念
  3. JavaScript 设计模式之观察者模式与发布订阅模式
  4. linux下重启mysql php nginx
  5. 7-1 公路村村通 (30 分)
  6. Git Bash中npm配置,加速下载等作用
  7. Python之进程+线程+协程(进程的本质 与 threading线程模块)
  8. Win10家庭版安装docker(WSL与WSL2两种安装方式)
  9. 索引sql server_维护SQL Server索引
  10. #CSP 201312-4 有趣的数
  11. Python 爬取 201865 条《隐秘的角落》弹幕,发现看剧不如爬山?
  12. 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!...
  13. 微信开发者工具构建npm
  14. 格式化日期为xxxx-xx-xx hh:mm:ss
  15. Oracle RAC集群增加新共享硬盘并使用AFD的式增加新ASM磁盘组
  16. 京东数据化运营(四)— 客单价篇
  17. 人生感悟|写在四月底
  18. 解决Windows XP 系统下,用户登录一直循环提示激活
  19. Android异步和同步的区别
  20. 连接MySQL错误:Can#39;t connect to MySQL server (10060)

热门文章

  1. 运维(11) : centos挂载nas
  2. Linux Skype 4.3 下载地址
  3. 上海闪酷成为京东商城第一批独立软件开发商(ISV)
  4. 推特“换帅”:新掌门人80后,33岁 CTO,37岁CEO
  5. Django项目实现微博登录
  6. EasyExcel多sheet页导出详细代码记录
  7. C# DotNetty (2) EchoClient
  8. vue技术点(3)—vuex、插槽、自定义指令、vue动画、keep-alive使用、mixin
  9. sourceMap: devtool 模式以及SourceMapDevToolPlugin的使用
  10. 【linux】i386与AMD64的区别