SpringBoot集成Redis,并自定义对象序列化

SpringBoot项目使用redis非常简单,pom里面引入redis的场景启动器,在启动类上加@EnableCaching注解,项目启动会自动匹配上redis,这样项目中就可以愉快地使用了,
使用方法:要么使用@Cacheable一类的注解自动缓存,要么使用RedisTemplate手动缓存。
前提是你的本机或者是远程主机要先搭好redis环境
虽然SpringBoot好用,但这里也有好多坑,SpringBoot和MySQL一样,易学难精,阳哥说的对,练武不练功,到老一场空。
下面,我将详细记录整合步骤

  1. 创建一个SpringBoot项目
    pom文件中引入cache和redis依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.新建cache数据库,创建员工表employee,写好DAO,service,controller,配置文件application.yml中配好数据源和redis地址

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3305/cache
    username: root
    password: 123456
  redis:
    host: 127.0.0.1
    port: 6379
    password: hehuiredis177
    
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations:
  - classpath:mybatis/mapper/*.xml
  
debug: true

3.SpringBoot主启动类上加@EnableCaching注解,service类@Cacheable(cacheNames=“employee”)注解

@Service
public class EmployeeService {

	@Autowired private EmployeeDao employeeDao;
	
	@Cacheable(cacheNames="employee")
	public List<Employee> findAll(){
		System.out.println("查询所有员工......");
		return employeeDao.findAll();
	}
	
	@Cacheable(cacheNames="employee--id")
	public Employee selectEmp(Integer id){
		System.out.println("查询员工:"+id);
		return employeeDao.selectById(id);
	}
	
}

3.启动项目(application.yml加上debug:true可以将自动配置报告打印在控制台方便观察)
在这里插入图片描述
项目启动时,自动配置报告提示Redis已经matched(这里说一下,你如果没有引入redis而又在启动类上加了@EnableCaching注解,SpringBoot会自动给你匹配一个SimpleCacheConfiguration缓存,它的底层用了一个key-value的Map,不能像redis一样持久化,您得注意,好好权衡一下用不用,笔者不建议使用,像SQLite、H2这些一样,玩具型的,只适合个人博客等非正式场合使用,有轻量级的优点,也有不可靠,不好管理的缺点)

4.然后访问 http://localhost:8080/emp/1
在这里插入图片描述
500错误,服务端有问题,仔细阅读报错信息,是实体类没有序列化导致的,好吧,那我就实现Serializable接口

5.练武不练功,到老一场空,实现序列化Serializable就可以了吗?是可以了,但这只是巧合,再去访问,确实可以
在这里插入图片描述
去看redis
在这里插入图片描述
什么东西啊,看不懂…
在这里插入图片描述
但第二次以后确实走了缓存,离成功又近了一步
再去试一下查所有
在这里插入图片描述
也可以
缓存也有
在这里插入图片描述
6.为什么实现Serializable接口就可以了,这就得开始扒源码了,开始练功…
去看RedisTemplate这个类
在这里插入图片描述
对象默认实现序列化
怎么实现的呢?
接着往下看
在这里插入图片描述
默认用的是jdk的序列化机制
所以实现Serializable接口就可以,巧合!!!

7.继续练功,redis里这么乱,看不懂,这显然不是我们想要的,我们要的是json,轻量易读的json才是我们的目标,欲存json,必须改变序列化机制,把jdk的序列化替换掉
继续读源码
找到RedisAutoConfiguration这个类,看它是怎么自动配置的
里面有个内部类RedisConfiguration
在这里插入图片描述
初始化的时候,把RedisTemplate和StringRedisTemplate放在了IOC容器里,也就是说我们可以在我们自己的代码里注入这两个类,然后手动缓存
而@Cacheable注解使用的是RedisTemplate,那么分析一下RedisTemplate

@Bean
		@ConditionalOnMissingBean(name = "redisTemplate")
		public RedisTemplate<Object, Object> redisTemplate(
				RedisConnectionFactory redisConnectionFactory)
				throws UnknownHostException {
			RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
			template.setConnectionFactory(redisConnectionFactory);
			return template;
		}

RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
直接new了一个RedisTemplate然后把它返回,呵呵!就从这里入手
@ConditionalOnMissingBean(name = “redisTemplate”)表示我们可以自定义RedisTemplate,我们有RedisTemplate,就用我们的,否则就用默认,我们不喜欢你的,要json就要自己配一个RedisTemplate
自己写

package com.hehui.config;

import java.net.UnknownHostException;

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;

/**
 * 自定义redis配置
 * @author hehui 
 * @date 2019年3月12日
 *
 */
@Configuration
public class MyRedisConfig {

	@Bean
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
		template.setConnectionFactory(redisConnectionFactory);
		Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
		template.setDefaultSerializer(serializer);
		return template;
	}
	
}

Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(Object.class);
template.setDefaultSerializer(serializer);
只多了这两行代码,其它什么都没变。
为什么要用Jackson2JsonRedisSerializer呢?
因为Jackson2JsonRedisSerializer实现了RedisSerializer接口
在这里插入图片描述
8.再访问 http://localhost:8080/emp/1
在这里插入图片描述
观察redis
在这里插入图片描述
key是我们要的key,value是我们要的value
再刷新页面
在这里插入图片描述
不会吧!!!
去缓存找,找到了,反序列化失败…
转换异常,看来不能用Jackson2JsonRedisSerializer,换成GenericJackson2JsonRedisSerializer
换了之后,刷新页面
在这里插入图片描述
报错信息变了,这是不能读取json,现在redis里的json是Jackson2JsonRedisSerializer序列化的,GenericJackson2JsonRedisSerializer当然不认识咯,清了redis再试
在这里插入图片描述
好了。

9.试试查所有 http://localhost:8080/list
在这里插入图片描述
报错!!!
SimpleKey and no properties discovered 没有key 什么情况?
于是继续读源码,看key是咋生成的

/**
	 * Generate a key based on the specified parameters.
	 */
	public static Object generateKey(Object... params) {
		if (params.length == 0) {
			return SimpleKey.EMPTY;
		}
		if (params.length == 1) {
			Object param = params[0];
			if (param != null && !param.getClass().isArray()) {
				return param;
			}
		}
		return new SimpleKey(params);
	}

是根据参数来的,我查询所有,是没有参数的,这样key就为空
if (params.length == 0) {
return SimpleKey.EMPTY;
}
这怎么行,开发中,不传参的方法多的很,这样生成key肯定不行撒
于是重写key的生成策略,用【类名+方法名+参数名】这样就可以保证key不为空

package com.hehui.config;

import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.util.Arrays;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
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.GenericJackson2JsonRedisSerializer;

/**
 * 自定义redis配置
 * @author hehui 
 * @date 2019年3月12日
 *
 */
@Configuration
public class MyRedisConfig extends CachingConfigurerSupport{

	@Bean
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
		template.setConnectionFactory(redisConnectionFactory);
//		Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
		GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
		template.setDefaultSerializer(serializer);
		return template;
	}

	@Bean
	@Override
	public KeyGenerator keyGenerator() {
		return new KeyGenerator() {
            public Object generate(Object target, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName()).append(".").append(method.getName()).append(Arrays.toString(objects));
                return sb.toString();
            }
        };
	}
	
	
	
}

extends CachingConfigurerSupport并重写keyGenerator方法
测试通过
在这里插入图片描述
redis
在这里插入图片描述
json中还带有@Class相当好。

10.好啦,相当曲折地把redis整合并自定义配置到SpringBoot中。

源码github地址:https://github.com/hehuihh/springboot_redis