springboot 整合 redisTemplate

springboot 整合 redis 大部分场景都是用在缓存场景当中,并结合 @Cacheable 注解使用,本篇文章主要是将springboot 整合 RedisTemplate 为一个 redis 的工具类,因为在日常的开发中,我们经常会将一些数据主动的存到 redis 当中。
接下来看一下如何使用 Springboot 整合 redis。

  1. maven 依赖
    关于 springboot 的可以去官网查找。
<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> 
  1. 配置文件 application.properties
    应为我搭建的redis 没有添加密码,所以将密码的配置注释掉。
    如果是自己实验,搭建 redis 可以使用 docker,很方便,参考我的博客:使用 docker 运行 redis
#redis配置开始
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=123456
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1024
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=200
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=10000
#redis配置结束
# spring.redis.block-when-exhausted=true
# spring.profiles.active=local
  1. 示例代码
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

@Controller
@RequestMapping("/")
public class HelloV2Controller {

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("putStock")
    @ResponseBody
    public String delStock() {
        redisTemplate.opsForValue().set("stock", 10);
        return "success";
    }

}

默认会帮我们注册 redisTemplate 的bean,访问 http://localhost:8080/putStock

查看数据库的内容:
在这里插入图片描述
发现其不是我们看到的结果,发现其乱码了。原因是SpringBoot自动帮我们在容器中生成了一个RedisTemplate和一个StringRedisTemplate。但是,这个RedisTemplate的泛型是<Object,Object>;我们需要一个泛型为<String,Object>形式的RedisTemplate。并且,这个RedisTemplate没有设置数据存在Redis时,key及value的序列化方式。
那自己从新定义一个 RedisTemplate:

@Configuration
public class RedisTemplateConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

再次调用 http://localhost:8080/putStock
在这里插入图片描述
完成,接下来就可以基于 RedisTemplate 开发自己的 redis 工具类了。

关于 redisTemplate 的集群以及哨兵等模式的配置,参考官方文档地址:
https://docs.spring.io/spring-data/redis/docs/2.2.2.RELEASE/reference/html/#get-started

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值