SpringBoot整合redis集群和redis单节点

// 连接redis单节点配置类

@Configuration
public class RedisConfig {



    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;
    @Value("${spring.redis.password}")
    private String password;
    /**
     * @description: 非集群的Redis连接工厂
     * @return: org.springframework.data.redis.connection.RedisConnectionFactory
     * @author: hezha
     * @time: 2023/10/24 15:31
     */
    @Bean
    public RedisConnectionFactory redisSingleNodeConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setPassword(password);
        config.setDatabase(0);
        return new JedisConnectionFactory(config);
    }

   /**
    * @description: 连接非集群的Redis的RedisTemplate
    * @return: org.springframework.data.redis.core.RedisTemplate
    * @author: hezha
    * @time: 2023/10/24 15:31
    */
    @Bean
    public RedisTemplate redisSingleNodeTemplate(@Qualifier("redisSingleNodeConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 设置key的序列化方式
        RedisSerializer stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);
        // 设置value的序列化方式
        Jackson2JsonRedisSerializer jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);
        return template;
    }

}
 
  

//连接redis集群的配置类

@Configuration
public class RedisClusterConfig {
    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;
    @Value("${spring.redis.password}")
    private String redisAuth;



    @Bean
    @Primary
    public RedisConnectionFactory redisClusterConnectionFactory() {
        RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration();
        String[] split = clusterNodes.split(",");
        for(int i = 0; i< split.length; i++){
            String[] strings = split[i].split(":");
            String host = strings[0];
            Integer post = Integer.parseInt(strings[1]);
            redisClusterConfig.clusterNode(host, post);
        }

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfig);
        // 配置其他 JedisConnectionFactory 属性,如密码、超时等
        jedisConnectionFactory.setPassword(redisAuth);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate redisClusterTemplate(RedisConnectionFactory redisClusterConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisClusterConnectionFactory);

        // 设置key的序列化方式
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        // 设置value的序列化方式
        Jackson2JsonRedisSerializer jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        redisTemplate.setValueSerializer(jsonSerializer);
        redisTemplate.setHashValueSerializer(jsonSerializer);

        return redisTemplate;
    }
} 
  

pom.xml

 
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis-reactive
        
        
            redis.clients
            jedis
            3.6.1
        
        
            com.alibaba
            fastjson
            1.2.54
        
        
            org.apache.commons
            commons-pool2

        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

使用如下:

//直接注入即可使用
  
@Autowired
    private  RedisTemplate redisSingleNodeTemplate;

    @Autowired
    private RedisTemplate redisClusterTemplate;

你可能感兴趣的:(java,redis)