1.爲了高可用,先安裝redis集羣 參考個人另外一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.htmlhtml
2.POM中引入redisjava
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.7.RELEASE</version> </dependency>
3.增長redis配置(使用集羣方式)node
#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集羣連接並設置緩存(部分配置及內容會在後續文章中講解到)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.經過下列代碼進行測試spring
@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的可視化工具 查看以下app
redis通常而言用string就能夠了,對象能夠經過JSON轉換後再存儲dom