Redis服务器搭建请参考: (一)Redis学习笔记 — redis在linux下的安装及基本配置
Redis集群搭建请参考: (二)Redis学习笔记 — redis集群配置
SpringBoot:
<!-- springboot 项目父工程,我使用2.3.4.RELEASE版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
公共配置文件:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
/**
* 封装自己的RedisTemplate
*
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 指定Object对象序列化工具类
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 指定String字符串序列化工具类
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);
// redisTemplate.setValueSerializer(genericToStringSerializer);
// redisTemplate.setHashValueSerializer(genericToStringSerializer);
return redisTemplate;
}
@Bean
public CacheManager cacheManager(ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
CacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory.getObject()).build();
return cacheManager;
}
}
application.properties 配置文件:
(1)单个redis服务的配置文件
# 修改默认端口
server.port=8081
# redis 配置
spring.redis.host=47.114.133.191
spring.redis.port=6379
spring.redis.password=123456
(2)主从模式redis服务的配置文件
# 修改默认端口
server.port=8081
# redis 集群配置 主从复制 模式
# 该节点是redis主从模式下master节点
spring.redis.host=47.114.133.191
spring.redis.port=7003
(3)sentinel模式redis服务的配置文件
# 修改默认端口
server.port=8081
# redis 集群配置 哨兵(sentinel) 模式
# 该节点是哨兵(sentinel)节点的IP和PORT,多个用“,”隔开
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=47.114.133.191:26379
(4)cluster模式redis服务的配置文件
# 修改默认端口
server.port=8081
# redis 集群配置 cluster 模式
# 这里配置所有redis服务的节点(包括master和slave)
spring.redis.cluster.nodes=47.114.133.191:6379,47.114.133.191:7000,47.114.133.191:7002,47.114.133.191:7003,47.114.133.191:7004,47.114.133.191:7005
# 这里配置的是重定向次数,一般等于主节点数量就行,过多无意义
spring.redis.cluster.max-redirects=3
测试:
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisApplicationTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisTest01() {
log.info("测试类启动");
redisTemplate.opsForValue().set("name", "姚占伟");
redisTemplate.opsForValue().set("name1", "yaozhanwei");
redisTemplate.opsForValue().set("name2", "yaolong1");
redisTemplate.opsForValue().setIfAbsent("name3", "youyou");
redisTemplate.opsForValue().setIfAbsent("name4", "yaozhanwei");
String name = (String) redisTemplate.opsForValue().get("name");
log.info("value = " + name + ", length = " + name.length());
String name1 = (String) redisTemplate.opsForValue().get("name1");
log.info("value = " + name1 + ", length = " + name1.length());
String name2 = (String) redisTemplate.opsForValue().get("name2");
log.info("value = " + name2 + ", length = " + name2.length());
String name3 = (String) redisTemplate.opsForValue().get("name3");
log.info("value = " + name3 + ", length = " + name3.length());
}
}