spring boot配置同时支持单机和集群redis

系统管理员 2022-05-08 12:34 757阅读 0赞

正式环境都是用集群版redis,开发用的单机版,领导要求通过配置文件来确定是单机还是集群,由于单机版已经实现了,那么准备就在单机版基础上进行开发,然后发现spring boot1.2版本已经比较老,就升级版本,由于升级了spring boot版本,对应其他配置也进行了修改。最终修改的配置如下:

pom.xml

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <spring.version>4.3.9.RELEASE</spring.version>
  4. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  5. </properties>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>1.5.8.RELEASE</version>
  10. </parent>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-data-redis</artifactId>
  14. <!--原来是spring-boot-starter-redis-->
  15. </dependency>
  16. <dependency>
  17. <groupId>redis.clients</groupId>
  18. <artifactId>jedis</artifactId>
  19. <version>2.9.0</version>
  20. </dependency>

redis.properties文件基本没有变:

  1. # REDIS (RedisProperties)
  2. # Redis服务器地址(集群用逗号分隔)
  3. #spring.redis.host=xxx.xxx.xxx.xxx:xxxxx
  4. spring.redis.host=xxx.xxx.xxx.xxx:xxxxx,xxx.xxx.xxx.xxx:xxxxx
  5. # Redis服务器连接密码(默认为空)
  6. spring.redis.password=123456
  7. # 连接超时时间(毫秒)
  8. spring.redis.timeout=2000
  9. spring.redis.max-redirects=8

注意:host变成ip:port,集群多个ip和端口用“,”分割,为什么这样写spring框架中RedisClusterConfiguration类中就是这样分割。看一下源代码:

  1. //RedisClusterConfiguration类:
  2. private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes"; //默认是配置这样的
  3. ……
  4. public RedisClusterConfiguration(PropertySource<?> propertySource) {
  5. notNull(propertySource, "PropertySource must not be null!");
  6. this.clusterNodes = new LinkedHashSet<RedisNode>();
  7. //有spring.redis.cluster.nodes配置,分割这个属性,添加node
  8. if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
  9. appendClusterNodes(commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)
  10. .toString()));
  11. }
  12. ……
  13. }
  14. //函数会调用,用","分割:
  15. public static String[] commaDelimitedListToStringArray(String str) {
  16. return delimitedListToStringArray(str, ",");
  17. }
  18. //然后用":"分割组装成RedisNode
  19. private void appendClusterNodes(Set<String> hostAndPorts) {
  20. for (String hostAndPort : hostAndPorts) {
  21. addClusterNode(readHostAndPortFromString(hostAndPort));
  22. }
  23. }
  24. private RedisNode readHostAndPortFromString(String hostAndPort) {
  25. String[] args = split(hostAndPort, ":");
  26. notNull(args, "HostAndPort need to be seperated by ':'.");
  27. isTrue(args.length == 2, "Host and Port String needs to specified as host:port");
  28. return new RedisNode(args[0], Integer.valueOf(args[1]).intValue());
  29. }

在cacheconfig类中变成这样的:

  1. @Bean
  2. public RedisClusterConfiguration getClusterConfiguration() {
  3. if (host.split(",").length > 1) {
  4. //如果是host是集群模式的才进行以下操作
  5. Map<String, Object> source = new HashMap<String, Object>();
  6. source.put("spring.redis.cluster.nodes", host);
  7. source.put("spring.redis.cluster.timeout", timeout);
  8. source.put("spring.redis.cluster.max-redirects", redirects);
  9. //在源码的注释中可以看到是这样配置,以为这样写就不用在Connection中不用在认证,后来确定太天真了
  10. source.put("spring.redis.cluster.password", password);
  11. return new RedisClusterConfiguration(new
  12. MapPropertySource("RedisClusterConfiguration", source));
  13. } else {
  14. return null;
  15. }
  16. }
  17. @Bean
  18. public JedisConnectionFactory jedisConnectionFactory() {
  19. if (host.split(",").length == 1) {
  20. JedisConnectionFactory factory = new JedisConnectionFactory();
  21. factory.setHostName(host.split(":")[0]);
  22. factory.setPort(Integer.valueOf(host.split(":")[1]));
  23. factory.setPassword(password);
  24. factory.setTimeout(timeout);
  25. return factory;
  26. } else {
  27. JedisConnectionFactory jcf = new JedisConnectionFactory(getClusterConfiguration());
  28. jcf.setPassword(password); //集群的密码认证
  29. return jcf;
  30. }
  31. }
  32. //这样改造之后,redisTemplate模板就不用改了,之前写的redisUtil类也不用变了

发表评论

表情:
评论列表 (有 0 条评论,757人围观)

还没有评论,来说两句吧...

相关阅读