springboot2整合redis

5 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍了如何在SpringBoot2项目中整合Redis,包括安装Redis服务,设置配置文件application.properties,以及解决配置和使用RedisTemplate过程中遇到的问题。适合初学者参考。
摘要由CSDN通过智能技术生成

springboot2整合redis

1.安装redis服务

由于我是安装在本机,所以是在github上搞的msi的文件
地址:https://github.com/microsoftarchive/redis/releases
下载msi文件,会直接注册为windows的服务
安装的时候,指定安装目录,在安装目录下找到redis.windows-server.conf文件
在这里插入图片描述
打开之后搜索requirepass,打开注释,设置密码
在windows任务管理器-服务中找到redis,并重启
使用redis可视化工具连接redis成功的话redis就安装成功咯。

配置

application.properties配置文件
# Redis数据库索引(默认为0)
spring.redis.database=5
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=60s

由于spring2.0的时间相关的配置的参数类型都改为了jdk1.8的Duration,所以这里的超时参数是60s
如果不想写配置类的话,springboot自动的配置了,直接注入RedisTemplate就可以使用,但是为了使用RedisTemplate(String, Object)的方便,俺这里写了个配置类。

package com.wudiqiang.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

/**
 * @Author: 着凉的皮皮虾
 * @Date: 2019/7/14 0:18
 */
@Configuration
public class RedisConfig {
    @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;
    }
}

再然后注入就可以用了噻,中间遇到了几个问题,不过也都比较完美的解决了,具体啥问题,当时我没记录,过去了短短半个多小时,忘记了。
这是本人的第一篇博客,如果有啥写的不好的地方,还请大家多多指教,有问题的也可以在底下留言,我看到了会尽快答复的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值