springBoot系列教程03:redis的集成及使用
1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html
2.POM中引入redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
3.增加redis配置(使用集群方式)
#redis pool config
#spring.redis.hostName=192.168.0.32
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.database=13
#spring.redis.pool.maxActive=8
#spring.redis.pool.maxWait=-1
#spring.redis.pool.maxIdle=8
#spring.redis.pool.minIdle=0
#spring.redis.timeout=0
#spring.redis.expire.time=20
#redis cluster config
spring.redis.cluster.nodes=192.168.0.45:7001,192.168.0.45:7002,192.168.0.45:7003,192.168.0.45:7004,192.168.0.45:7005,192.168.0.45:7006
#spring.redis.cluster.nodes=192.168.0.81:7001,192.168.0.81:7002,192.168.0.81:7003,192.168.0.81:7004,192.168.0.81:7005,192.168.0.81:7006
spring.redis.cluster.timeout=2000
spring.redis.cluster.max-redirects=1
spring.redis.expire.time=20
4.配置redis集群链接并设置缓存(部分配置及内容会在后续文章中讲解到)
package com.xiao.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.MapPropertySource; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.jedis.JedisClusterConnection; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.HashMap; import java.util.Map; //链接redis集群的时候 @Configuration @EnableCaching @RefreshScope public class RedisClusterConfig { @Value("${spring.redis.cluster.nodes}") private String clusterNodes; @Value("${spring.redis.cluster.max-redirects}") private int redirects; @Value("${spring.redis.expire.time}") private int redisExpireTime; @Bean @RefreshScope public RedisClusterConfiguration redisClusterConfiguration() { Map<String, Object> source = new HashMap<>(); source.put("spring.redis.cluster.nodes", clusterNodes); source.put("spring.redis.cluster.max-redirects", redirects); return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source)); } @Bean public JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(redisClusterConfiguration()); } @Bean public JedisClusterConnection jedisClusterConnection() { return (JedisClusterConnection) jedisConnectionFactory().getConnection(); } @Bean public RedisTemplate<String, String> redisTemplate() { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(jedisConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer()); return redisTemplate; } @Bean public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(redisExpireTime); return cacheManager; } }
5.通过下列代码进行测试
@Autowired
StringRedisTemplate stringRedisTemplate;
@RequestMapping(value = "/redis/setget")
public Result redisSetGet(@RequestParam(value = "key", required = true) String key) {
stringRedisTemplate.opsForValue().set(key, UUID.randomUUID().toString());
stringRedisTemplate.expire(key, 10, TimeUnit.MINUTES);
return new Result("从redis中获取到的值为:" + stringRedisTemplate.opsForValue().get(key));
}
结果如下:
通过redis的可视化工具 查看如下
redis一般而言用string就可以了,对象可以通过JSON转换后再存储