189 8069 5689

SpringBootRedis自适应配置的方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的商丘网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

小编给大家分享一下SpringBoot Redis自适应配置的方法,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

核心代码段

提供一个JedisConnectionFactory  根据配置来判断 单点 集群 还是哨兵

 @Bean
 @ConditionalOnMissingBean
 public JedisConnectionFactory jedisConnectionFactory() {
  JedisConnectionFactory factory = null;
 
  String[] split = node.split(",");
  Set nodes = new LinkedHashSet<>();
  for (int i = 0; i < split.length; i++) {
   try {
    String[] split1 = split[i].split(":");
    nodes.add(new HostAndPort(split1[0], Integer.parseInt(split1[1])));
   } catch (Exception e) {
    throw new RuntimeException(String.format("出现配置错误!请确认node=[%s]是否正确", node));
   }
  }
  //获得默认的连接池构造器(怎么设计的,为什么不抽象出单独类,供用户使用呢) 有毒
  JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb =
    (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
  //指定jedisPoolConifig来修改默认的连接池构造器(真麻烦,滥用设计模式!) !!!!
  jpcb.poolConfig(jedisPoolConfig);
  //通过构造器来构造jedis客户端配置
  JedisClientConfiguration jedisClientConfiguration = jpcb.build();
 
  //如果是哨兵的模式
  if (!StringUtils.isEmpty(sentinel)) {
   logger.info("Redis use SentinelConfiguration");
   RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
   String[] sentinelArray = sentinel.split(",");
   for (String s : sentinelArray) {
    try {
     String[] split1 = s.split(":");
     redisSentinelConfiguration.addSentinel(new RedisNode(split1[0], Integer.parseInt(split1[1])));
    } catch (Exception e) {
     throw new RuntimeException(String.format("出现配置错误!请确认node=[%s]是否正确", node));
    }
   }
   factory = new JedisConnectionFactory(redisSentinelConfiguration, jedisClientConfiguration);
 
  }
  //如果是单个节点 用Standalone模式
  else if (nodes.size() == 1) {
   logger.info("Redis use RedisStandaloneConfiguration");
   for (HostAndPort n : nodes) {
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
    if (!StringUtils.isEmpty(password)) {
     redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
    }
    redisStandaloneConfiguration.setPort(n.getPort());
    redisStandaloneConfiguration.setHostName(n.getHost());
    factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
   }
  }
  //集群配置信息实现
  else {
   logger.info("Redis use RedisStandaloneConfiguration");
   RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
   nodes.forEach(n -> {
    redisClusterConfiguration.addClusterNode(new RedisNode(n.getHost(), n.getPort()));
   });
   if (!StringUtils.isEmpty(password)) {
    redisClusterConfiguration.setPassword(RedisPassword.of(password));
   }
   redisClusterConfiguration.setMaxRedirects(maxRedirect);
   factory = new JedisConnectionFactory(redisClusterConfiguration, jedisClientConfiguration);
  }
  return factory;
 }

网站名称:SpringBootRedis自适应配置的方法-创新互联
链接URL:http://cdxtjz.cn/article/ddhgpe.html

其他资讯