創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的商丘網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!小編給大家分享一下SpringBoot Redis自適應(yīng)配置的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
核心代碼段
提供一個(gè)JedisConnectionFactory 根據(jù)配置來判斷 單點(diǎn) 集群 還是哨兵
@Bean @ConditionalOnMissingBean public JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory factory = null; String[] split = node.split(","); Setnodes = 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("出現(xiàn)配置錯(cuò)誤!請(qǐng)確認(rèn)node=[%s]是否正確", node)); } } //獲得默認(rèn)的連接池構(gòu)造器(怎么設(shè)計(jì)的,為什么不抽象出單獨(dú)類,供用戶使用呢) 有毒 JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder(); //指定jedisPoolConifig來修改默認(rèn)的連接池構(gòu)造器(真麻煩,濫用設(shè)計(jì)模式?。??。。?! jpcb.poolConfig(jedisPoolConfig); //通過構(gòu)造器來構(gòu)造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("出現(xiàn)配置錯(cuò)誤!請(qǐng)確認(rèn)node=[%s]是否正確", node)); } } factory = new JedisConnectionFactory(redisSentinelConfiguration, jedisClientConfiguration); } //如果是單個(gè)節(jié)點(diǎn) 用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); } } //集群配置信息實(shí)現(xiàn) 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; }