正式环境都是用集群版redis,开发用的单机版,领导要求通过配置文件来确定是单机还是集群,由于单机版已经实现了,那么准备就在单机版基础上进行开发,然后发现spring boot1.2版本已经比较老,就升级版本,由于升级了spring boot版本,对应其他配置也进行了修改。最终修改的配置如下:
pom.xml
<properties> <java.version>1.8</java.version> <spring.version>4.3.9.RELEASE</spring.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!--原来是spring-boot-starter-redis--> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
redis.properties文件基本没有变:
# REDIS (RedisProperties) # Redis服务器地址(集群用逗号分隔) #spring.redis.host=xxx.xxx.xxx.xxx:xxxxx spring.redis.host=xxx.xxx.xxx.xxx:xxxxx,xxx.xxx.xxx.xxx:xxxxx # Redis服务器连接密码(默认为空) spring.redis.password=123456 # 连接超时时间(毫秒) spring.redis.timeout=2000 spring.redis.max-redirects=8
注意:host变成ip:port,集群多个ip和端口用“,”分割,为什么这样写spring框架中RedisClusterConfiguration类中就是这样分割。看一下源代码:
//RedisClusterConfiguration类: private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes"; //默认是配置这样的 …… public RedisClusterConfiguration(PropertySource<?> propertySource) { notNull(propertySource, "PropertySource must not be null!"); this.clusterNodes = new LinkedHashSet<RedisNode>(); //有spring.redis.cluster.nodes配置,分割这个属性,添加node if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) { appendClusterNodes(commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY) .toString())); } …… } //函数会调用,用","分割: public static String[] commaDelimitedListToStringArray(String str) { return delimitedListToStringArray(str, ","); } //然后用":"分割组装成RedisNode private void appendClusterNodes(Set<String> hostAndPorts) { for (String hostAndPort : hostAndPorts) { addClusterNode(readHostAndPortFromString(hostAndPort)); } } private RedisNode readHostAndPortFromString(String hostAndPort) { String[] args = split(hostAndPort, ":"); notNull(args, "HostAndPort need to be seperated by ':'."); isTrue(args.length == 2, "Host and Port String needs to specified as host:port"); return new RedisNode(args[0], Integer.valueOf(args[1]).intValue()); }
在cacheconfig类中变成这样的:
@Bean public RedisClusterConfiguration getClusterConfiguration() { if (host.split(",").length > 1) { //如果是host是集群模式的才进行以下操作 Map<String, Object> source = new HashMap<String, Object>(); source.put("spring.redis.cluster.nodes", host); source.put("spring.redis.cluster.timeout", timeout); source.put("spring.redis.cluster.max-redirects", redirects); //在源码的注释中可以看到是这样配置,以为这样写就不用在Connection中不用在认证,后来确定太天真了
source.put("spring.redis.cluster.password", password); return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); } else { return null; } } @Bean public JedisConnectionFactory jedisConnectionFactory() { if (host.split(",").length == 1) { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host.split(":")[0]); factory.setPort(Integer.valueOf(host.split(":")[1])); factory.setPassword(password); factory.setTimeout(timeout); return factory; } else { JedisConnectionFactory jcf = new JedisConnectionFactory(getClusterConfiguration()); jcf.setPassword(password); //集群的密码认证 return jcf; } } //这样改造之后,redisTemplate模板就不用改了,之前写的redisUtil类也不用变了
相关推荐
scratch少儿编程逻辑思维游戏源码-皮博冒险者.zip
少儿编程scratch项目源代码文件案例素材-这是之前下载的测试.zip
scratch少儿编程逻辑思维游戏源码-汽车冲突.zip
scratch少儿编程逻辑思维游戏源码-梦幻岛 3D.zip
scratch少儿编程逻辑思维游戏源码-收集水果.zip
炫酷蓝色响应式投稿说明源码.zip
机器学习算法与应用大作业-基于预处理的小麦品种的分类和聚类源码+数据+使用说明.zip是个人经导师指导并认可通过的高分设计项目,评审分98分。主要针对计算机相关专业的正在做大作业的学生和需要项目实战练习的学习者,可作为课程设计、期末大作业。。内容来源于网络分享,如有侵权请联系我删除。
vs2019_Qt5.12.12编译好的ffmpeg库,因为是c接口,别的版本理论也可以用
scratch少儿编程逻辑思维游戏源码-欧力多.zip
少儿编程scratch项目源代码文件案例素材-越野运动员.zip
GUI开发_CMake_MSVC_CLion_ElaWidgetTools_跨平台界面组件库_简化项目结构_降低上手难度_提供完整编译环境配置_支持Windows11开发_快速集成
scratch少儿编程逻辑思维游戏源码-时间先生.zip
少儿编程scratch项目源代码文件案例素材-爪猫使命召唤.zip
内容管理系统_SpringBootThymeleaf双引擎解析_梦想家CMS开源建站系统_面向政府企业组织快速搭建展示型网站如企业官网技术博客信息门户等解决建站成本高周期长问题
健康监测与运动数据自动化_云函数部署与定时任务管理_乐心健康APP账号绑定与步数同步_通过腾讯云函数和青龙面板实现自动刷步并同步至蚂蚁森林获取每日296g能量的智能脚本系统_适用于
动态雨滴玻璃掉落个人主页源码.rar
scratch少儿编程逻辑思维游戏源码-鸟群算法.zip
scratch少儿编程逻辑思维游戏源码-墨迹.zip
本校园管理系统采用的数据库是Mysql,使用springboot框架开发。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
微服务架构_SpringCloud_Alibaba_Nacos_Sentinel_Redis_MyBatisPlus_自动化构建_分布式系统_基于smart-cloud框架的微服务