正式环境都是用集群版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类也不用变了
相关推荐
通过配置文件实现连接单机redis或集群redis demo,实现开发时使用单机redis,线上使用集群redis
spring boot+redis单机与集群.zip spring boot+redis单机与集群.zip spring boot+redis单机与集群.zip
Springboot整合Redis集群,零配置方式;另外用AOP实现了操作缓存的三套自定义注解,有兴趣的小伙伴可以试试看.
spring boot + jpa + redis集群。压缩包中包含工程代码(直接运行即可,需要修改yml文件中数据库地址和redis部署机器IP地址)、部署文档和redis 3.0+ruby的安装包
springBoot整合Redis,包括整合单机版Redis、redis-cluster集群、redis哨兵三种模式
在一些要求高一致性(任何数据变化都能及时的被查询到)的系统和应用中,就不能再使用EhCache来解决了,这个时候使用集中式缓存是个不错的选择,因此本文将介绍如何在Spring Boot的缓存支持中使用Redis进行数据缓存...
springboot整合redis集群(三种方式)源码
本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目...
springboot整合redis,用最简洁的代码实现缓存功能,亲测有效!!!绝对可行!!!
16套Java架构师,集群,高可用,高可扩展,高性能,高并发,性能优化,设计模式,数据结构,虚拟机,微服务架构,日志分析,工作流,Jvm,Dubbo ,Spring boot,Spring cloud, Redis,ActiveMQ,Nginx,Mycat,Netty,...
Ubuntu 22最新dockers部署redis哨兵模式,并整合spring boot
SpringBoot整合Redis,包括整合单机版Redis、redis-cluster集群、redis哨兵模式
Redis拾遗部分博客相关的实例代码,包括其中的抢红包案例,相关SQL也一并附上,导入之后,修改Redis的配置文件之后,可以直接运行,但是如果想要配置集群,也只需要修改连接配置即可
动态配置权限,角色和资源,权限控制到按钮粒度 采用token进行权限校验,禁用session,未登录返回401,权限不足返回403 采用redis存储token及权限信息 内置功能: 用户管理:用户查询、添加用户、修改用户、给...
- chapter1:[基本项目构建(可...- chapter9-1-6:[Spring Cloud构建微服务架构(六)集群监控] ## 开源项目推荐 - 不错的spring boot实践学习案例:https://git.oschina.net/jeff1993/springboot-learning-example
springboot整合redis集群和多索引库solr,同时将redis集群作mybatis的二级缓存源代码
Spring Boot为Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自动配置。本文详细介绍了springboot配置多个redis连接,有兴趣的可以了解一下。
此springboot--demo集成了以下组件,demo如无法运行、或缺失相应组件demo无法调用可以私聊52fighting ...9. Redis单机、redis集群、redis哨兵模式 10. swagger2 11. okhttp 12. Log4j2 13. poi excel
springboot + redis 集群配置+测试case; linux 上安装部署 redis 集群图文文档;