SpringBoot整合redis

1.在配置文件中配置redis属性,我配置的是redis集群

#-----------redis----------#
spring.redis.cluster.nodes=103.235.220.246:6000,103.235.220.246:6001,103.235.220.246:6002
#Number of allowed cluster redirections.
#spring.redis.cluster.max-redirects=1
#连接池最大连接数
spring.redis.jedis.pool.max-active=8
#连接池最大空闲数
spring.redis.jedis.pool.max-idle=8
#连接池最小空闲数
spring.redis.jedis.pool.min-idle=0
#连接池最大阻塞等待时间
spring.redis.jedis.pool.max-wait=-1
#连接超时时间(毫秒)
spring.redis.timeout=1000

2.配置redis属性文件

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisConfigProperty {

    List nodes;

    /**
     * Get initial collection of known cluster nodes in format {@code host:port}.
     *
     * @return
     */
    public List getNodes() {
        return nodes;
    }

    public void setNodes(List nodes) {
        this.nodes = nodes;
    }
}

3.配置redisTemplate

Configuration
public class RedisConfiguration {

    private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();

    @Autowired
    private RedisConfigProperty configPro;

    @Bean
    public RedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory(
                new RedisClusterConfiguration(configPro.getNodes()));
    }

    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory());
        redisTemplate.setDefaultSerializer(STRING_SERIALIZER);
        return redisTemplate;
    }

    @Bean
    public HashOperations hashOperations() {
        return redisTemplate().opsForHash();
    }

}

4.总结

通过这次整合redis操作意识到还得好好学英文啊,只有去看官方文档才最全,能少走许多弯路

你可能感兴趣的:(SpringBoot整合redis)