项目github地址:https://github.com/5-Ason/aso...
具体可看 ./db/db-redis 和 ./db/db-cache 两个模块java
// TODO 在整合redis以前须要先本地配置好redis环境,迟点有时间补一下linux下下载安装配置redis
本文主要实现的是对数据操做进行独立模块得整合,详情请看个人另外一篇博文:
【技术杂谈】springcloud微服务之数据操做独立模块化linux
独立部署Redis非关系型数据库做为内存缓存模块,实现SpringBoot项目中依赖缓存模块进行缓存操做,并进行简单测试。git
<!-- Spring Boot Redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Spring Boot Cache 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
由于个人项目使用的是springcloud分布式配置
因此配置文件在 ./config-server/config-repo/data-dev.yml,具体配置以下:github
# ====================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
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<String, String> functionDomainRedisTemplate(@Qualifier(value = "jedisConnectionFactory") RedisConnectionFactory factory) { log.info("初始化RedisTemplate"); RedisTemplate<String, String> 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; } }
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<Object> { @Override public byte[] serialize(Object t) throws SerializationException { if (t == null) { return new byte[0]; } return SerializeUtil.serialize(t); } @Override public Object deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length == 0) { return null; } return SerializeUtil.unserialize(bytes); } }
package com.ason; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * 定义序列化 * Created by Ason on 2017-09-23. */ public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } public static Object unserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.ason; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.core.RedisTemplate; import java.lang.reflect.Method; /** * Created by Ason on 2017/9/25. * 这里实现CachingConfigurerSupport主要是方便使用自定义keyGenerator */ @Configuration @EnableCaching // 启用缓存 public class RedisCacheConf extends CachingConfigurerSupport { @Autowired private RedisTemplate redisTemplate; private static final Log log = LogFactory.getLog(RedisConf.class); /** * 配置redis缓存管理对象 * @return */ @Bean(name = "cacheManager") public CacheManager cacheManager() { log.info("初始化CacheManager"); RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // Map<String, Long> expires = new HashMap<>(); // expires.put("cache:user", 36000L); // cacheManager.setExpires(expires); //设置缓存过时时间 //cacheManager.setDefaultExpiration(10000); return cacheManager; } /** * 生成key的策略 * 此方法将会根据类名+方法名+全部参数的值生成惟一的一个key,即便@Cacheable中的value属性同样,key也会不同。 * @return */ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } }
由于是将redis的配置单独做为一个模块db-redis,CacheManager的配置模块db-cache依赖db-redis:redis
<dependency> <groupId>com.ason</groupId> <artifactId>db-redis</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
因此个人rms-service微服务想进行缓存的操做,需依赖db-cache模块:spring
<dependency> <groupId>com.ason</groupId> <artifactId>db-cache</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
同时,还须要读取redis配置的属性,上面个人配置是放在 ./config-server/config-repo/data-dev.yml 下,因此在rms-service微服务下的 bootstrap.yml 配置文件中,须要指定配置中心服务的地址以及配置文件的name和profile:数据库
spring: # 配置中心服务的地址 cloud: config: name: data profile: ${spring.profiles.active} # 要读取的配置文件profile属性 # uri: http://127.0.0.1:7001 #label: ${spring.profiles.active} discovery: enabled: true # 默认false,设为true表示使用注册中心中的configserver配置而不本身配置configserver的uri serviceId: config-server profiles: active: dev
接下来,便可使用缓存相关的注解在 rms-service 微服务下使用缓存,在 RmsUserController(仅为测试,实际开发中根据需求作调整) 添加:apache
/** * 查询单个用户 */ @Cacheable(value = "usercache", key = "#account") @PostMapping(value = "/account", produces = "application/json;charset=UTF-8") public String findUserByAccout(@RequestParam("account") String account) throws Exception { return ResultBody.success(rmsUserService.selectUserByAccout(account)); }
启动项目,post请求访问 http://localhost:8888/rms/use...json
接下来,再去redis看一下,发现11@qq.com已做为key保存起来了:bootstrap
有些情形下注解式缓存会不起做用的(本人第一次整合测试遇到的坑):
同一个bean内部方法调用、子类调用父类中有缓存注解的方法等。
至此,已完成 springboot 整合独立模块 redis 作缓存
详情请看github地址:https://github.com/5-Ason/aso...