紧接着上文 《redisTemplete config sentinel(哨兵)模式 常用配置》
开发中经常会遇到一个场景,同样一套代码,对不同客户时也许客户要求的redis集群不一样,向我们面对银行A客户时他们要求只能用他们现有的哨兵集群,但面对另一个银行客户B时又只能用cluster集群 ,所以基于最小改动原则,尽量让代码保持不变,通过配置文件修改一个属性达到目的。
本文主要来说一下如何在代码中同时存在sentinel 哨兵模式,以及cluster 模式配置,并且可以通过配置文件(例如nacos, apollo,application.yml application.properties 等)达到一键式切换
package cn.goswan.orient.gateway.config; import io.lettuce.core.ReadFrom; import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisNode; import org.springframework.data.redis.connection.RedisSentinelConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; import java.util.ArrayList; import java.util.List; /** * RedisTemplate config配置 * * @author alan.wang */ // @EnableCaching TODO @Configuration @AllArgsConstructor @AutoConfigureBefore(RedisAutoConfiguration.class) @Slf4j public class RedisTemplateConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setKeySerializer(new StringRedisSerializer()); stringRedisTemplate.setConnectionFactory(redisConnectionFactory); return stringRedisTemplate; } @Autowired RedisProperties redisProperties; @Bean public GenericObjectPoolConfig poolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle()); config.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle()); config.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive()); config.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis()); return config; } @Bean @ConditionalOnProperty(value = "spring.redis.mode",havingValue = "sentinel") public RedisSentinelConfiguration redisSentinelConfiguration() { RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration(); redisConfig.setMaster(redisProperties.getSentinel().getMaster()); if(redisProperties.getSentinel().getNodes()!=null) { List<RedisNode> sentinelNode=new ArrayList<RedisNode>(); for(String sen : redisProperties.getSentinel().getNodes()) { String[] arr = sen.split(":"); sentinelNode.add(new RedisNode(arr[0],Integer.parseInt(arr[1]))); } redisConfig.setDatabase(redisProperties.getDatabase()); redisConfig.setPassword(redisProperties.getPassword()); redisConfig.setSentinelPassword(redisConfig.getPassword()); redisConfig.setSentinels(sentinelNode); } return redisConfig; } @Bean @ConditionalOnProperty(value = "spring.redis.mode",havingValue = "sentinel") public RedisConnectionFactory redisConnectionFactory(@Qualifier("poolConfig") GenericObjectPoolConfig config, RedisSentinelConfiguration redisConfig) {//注意传入的对象名和类型RedisSentinelConfiguration LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } @Bean @ConditionalOnProperty(value = "spring.redis.mode",havingValue = "cluster") public RedisClusterConfiguration redisClusterConfiguration() { RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes()); redisClusterConfiguration.setPassword(redisProperties.getPassword()); return redisClusterConfiguration; } @Bean @ConditionalOnProperty(value = "spring.redis.mode",havingValue = "cluster") public RedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) { ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder() .enablePeriodicRefresh() .enableAllAdaptiveRefreshTriggers() .refreshPeriod(Duration.ofSeconds(5)) .build(); ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder() .topologyRefreshOptions(clusterTopologyRefreshOptions).build(); LettuceClientConfiguration lettuceClientConfiguration = LettuceClientConfiguration.builder() .readFrom(ReadFrom.REPLICA_PREFERRED) .clientOptions(clusterClientOptions).build(); return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration); } }
可以看到最重点的配置其实就是
@ConditionalOnProperty 里面的配置可以随便自定义,只要达到能区分就好,这样重启之后就会按照配置生成指定bean并注入,下面是我的配置,我们用的是spring cloud nacos 配置中心
用其他的配置中心或者配置文件也是一样的