摘要:至此,已完成整合独立模块做缓存详情请看地址相关文章系列整合独立模块
项目github地址:https://github.com/5-Ason/aso...
具体可看 ./db/db-redis 和 ./db/db-cache 两个模块
// TODO 在整合redis之前需要先本地配置好redis环境,迟点有时间补一下linux下下载安装配置redis
本文主要实现的是对数据操作进行独立模块得整合,详情请看我的另一篇博文:
【技术杂谈】springcloud微服务之数据操作独立模块化
独立部署Redis非关系型数据库作为内存缓存模块,实现SpringBoot项目中依赖缓存模块进行缓存操作,并进行简单测试。
2、添加依赖
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-cache
3、配置属性
因为我的项目使用的是springcloud分布式配置
所以配置文件在 ./config-server/config-repo/data-dev.yml,具体配置如下:
# ====================redis====================
redis:
# Redis服务器地址
host: ason-hostname
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
# 连接超时时间(毫秒)
timeout: 0
# Redis数据库索引(默认为0)
database: 0
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
4、RedisConf配置
package com.ason;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by Ason on 2017-09-23.
*/
@Configuration
public class RedisConf {
private static final Log log = LogFactory.getLog(RedisConf.class);
@Value("${redis.host}")
private String host; // Redis服务器地址
@Value("${redis.port}")
private int port; // Redis服务器连接端口
@Value("${redis.password}")
private String password; // Redis服务器连接密码(默认为空)
@Value("${redis.timeout}")
private int timeout; // 连接超时时间(毫秒)
@Value("${redis.database}")
private int database; // 连接超时时间(毫秒)
@Value("${redis.pool.max-active}")
private int maxTotal; // 连接池最大连接数(使用负值表示没有限制)
@Value("${redis.pool.max-wait}")
private int maxWaitMillis; // 连接池最大阻塞等待时间(使用负值表示没有限制)
@Value("${redis.pool.max-idle}")
private int maxIdle; // 连接池中的最大空闲连接
@Value("${redis.pool.min-idle}")
private int minIdle; // 连接池中的最小空闲连接
/**
* 配置JedisPoolConfig
* @return JedisPoolConfig实体
*/
@Bean(name = "jedisPoolConfig")
public JedisPoolConfig jedisPoolConfig() {
log.info("初始化JedisPoolConfig");
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(this.maxTotal); // 连接池最大连接数(使用负值表示没有限制)
jedisPoolConfig.setMaxWaitMillis(this.maxWaitMillis); // 连接池最大阻塞等待时间(使用负值表示没有限制)
jedisPoolConfig.setMaxIdle(this.maxIdle); // 连接池中的最大空闲连接
jedisPoolConfig.setMinIdle(this.minIdle); // 连接池中的最小空闲连接
// jedisPoolConfig.setTestOnBorrow(true);
// jedisPoolConfig.setTestOnCreate(true);
// jedisPoolConfig.setTestWhileIdle(true);
return jedisPoolConfig;
}
/**
* 实例化 RedisConnectionFactory 对象
* @param poolConfig
* @return
*/
@Bean(name = "jedisConnectionFactory")
public RedisConnectionFactory jedisConnectionFactory(@Qualifier(value = "jedisPoolConfig") JedisPoolConfig poolConfig) {
log.info("初始化RedisConnectionFactory");
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);
jedisConnectionFactory.setHostName(this.host);
jedisConnectionFactory.setPort(this.port);
jedisConnectionFactory.setDatabase(this.database);
return jedisConnectionFactory;
}
/**
* 实例化 RedisTemplate 对象
* @return
*/
@Bean(name = "redisTemplate")
public RedisTemplate functionDomainRedisTemplate(@Qualifier(value = "jedisConnectionFactory") RedisConnectionFactory factory) {
log.info("初始化RedisTemplate");
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new EntityRedisSerializer());
redisTemplate.setValueSerializer(new EntityRedisSerializer());
redisTemplate.afterPropertiesSet();
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}
RedisConf中涉及到的序列化相关的类
EntityRedisSerializer
package com.ason;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
* 自定义Redis序列化
* Created by Ason on 2017-09-23.
*/
public class EntityRedisSerializer implements RedisSerializer