(文章主要是为自己记录在项目中用到的技术,技术小白,若有不对的地方请多多指教)
1. 首先去官网下载Redis的安装包,官方下载地址(复制链接打开新的标签进去):
https://github.com/tporadowski/redis/releases
网上找的免费redis可视化工具,百度网盘下载地址(复制链接打开新的标签进去):
http://pan.baidu.com/s/1kU8sY3P
2. 解压后找到文件夹直接打开redis-server.exe即可(注意窗口不要关闭,关闭后服务停止)
SpringBoot 2.x 已经整合了lettuce连接池
1. 直接在在application.yml文件配置,如下:
spring: redis: database: 0 #redis数据库索引,默认为0 host: 192.168.1.168 #redis服务器地址 port: 6379 #redis服务器连接端口 password: #redis服务器连接密码,默认为null timeout: 5000 #redis连接超时时间 lettuce: pool: #连接池配置 max-active: 20 #连接池最大连接数 max-wait: -1 #连接池最大阻塞等待时间,-1表示无限等待 max-idle: 8 #连接池中的最大空闲连接数 min-idle: 1 #连接池中的最小空闲连接数
2. 解决redis序列化异常,代码如下:
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean @SuppressWarnings(value = { "unchecked", "rawtypes" }) public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); // 1. 配置连接工厂 redisTemplate.setConnectionFactory(connectionFactory); // 2. 使用Jackson2JsonRedisSerialize替换默认序列化方式(默认JDK序列化方式) Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); ObjectMapper om = new ObjectMapper(); // 3. 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 4. bugFix Jackson2反序列化数据处理LocalDateTime类型时出错 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dtf)); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dtf)); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dtf2)); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dtf2)); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(dtf3)); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(dtf3)); om.registerModule(javaTimeModule); // 5. 序列化类,对象映射设置 jackson2JsonRedisSerializer.setObjectMapper(om); //使用StringRedisSerializer来序列化和反序列化redis的key值 redisTemplate.setKeySerializer(stringRedisSerializer); //使用jackson2JsonRedisSerializer来序列化和反序列化redis的value值 redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); // 设置hash key 和value序列化模式 redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); //初始化RedisTemplate(使用RedisTemplate需要先调用此方法) redisTemplate.afterPropertiesSet(); return redisTemplate; } }
3. 示例,代码如下:
@Service public class SellerLoginService { @Resource private RedisTemplate redisTemplate; public BaseOutput<UserLoginOutput> userLogin(UserLoginInput input) { redisTemplate.opsForSet().add("login:"+input.getMobile(),input.getPassword()); return new BaseOutput<>(RetCode.SUCCESS.getRetCode(),RetCode.SUCCESS.message,output); } }