springboot整合redis

注入redisTemplate为空解决办法

1. 官网

2. 集群文档

3. 整合

  • 引入依赖


    org.springframework.boot
    spring-boot-starter-data-redis

  • redis配置文件
#=========redis基础配置=========
spring.redis.database=0
spring.redis.host=192.168.106.130
# redis密码
spring.redis.password=123456
spring.redis.port=6379

# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000

#=========redis线程池设置=========
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.jedis.pool.max-idle=200

#连接池中的最小空闲连接,默认值也是0。
spring.redis.jedis.pool.min-idle=200

# 如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.jedis.pool.max-active=2000

# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
spring.redis.jedis.pool.max-wait=1000
  • 注入redis配置类
    作用:springboot帮我们注入的redisTemplate类,泛型里面只能写,此配置类可以 注入RedisTemplate 类型,基本满足开发需求
package com.standard.management;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis配置类
 *
 * @author 伍磊
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public KeyGenerator KeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);
        cacheManager.setTransactionAware(true);
        return cacheManager;
    }


    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

 
 
  • 测试连接是否成功

4. 相关文档

  • springboot中使用redis(主要讲解RedisTemplate
  • SpringBoot中Redis的set、map、list、value、实体类等基本操作介绍
  • Springboot中使用redis,自动缓存、更新、删除
  • Spring中使用RedisTemplate操作Redis(spring-data-redis
  • springboot操作redis list集合

5. 可能出现问题

  • 连接不上 ---> 确认外网能否连接 ./src/redis-cli -h 192.168.106.130 -p 6379 --raw

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