// 连接redis单节点配置类
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
/**
* @description: 非集群的Redis连接工厂
* @return: org.springframework.data.redis.connection.RedisConnectionFactory
* @author: hezha
* @time: 2023/10/24 15:31
*/
@Bean
public RedisConnectionFactory redisSingleNodeConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(port);
config.setPassword(password);
config.setDatabase(0);
return new JedisConnectionFactory(config);
}
/**
* @description: 连接非集群的Redis的RedisTemplate
* @return: org.springframework.data.redis.core.RedisTemplate<java.lang.String,java.lang.Object>
* @author: hezha
* @time: 2023/10/24 15:31
*/
@Bean
public RedisTemplate<String, Object> redisSingleNodeTemplate(@Qualifier("redisSingleNodeConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 设置key的序列化方式
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
// 设置value的序列化方式
Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
template.setValueSerializer(jsonSerializer);
template.setHashValueSerializer(jsonSerializer);
return template;
}
}
//连接redis集群的配置类
@Configuration
public class RedisClusterConfig {
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Value("${spring.redis.password}")
private String redisAuth;
@Bean
@Primary
public RedisConnectionFactory redisClusterConnectionFactory() {
RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration();
String[] split = clusterNodes.split(",");
for(int i = 0; i< split.length; i++){
String[] strings = split[i].split(":");
String host = strings[0];
Integer post = Integer.parseInt(strings[1]);
redisClusterConfig.clusterNode(host, post);
}
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfig);
// 配置其他 JedisConnectionFactory 属性,如密码、超时等
jedisConnectionFactory.setPassword(redisAuth);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisClusterTemplate(RedisConnectionFactory redisClusterConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisClusterConnectionFactory);
// 设置key的序列化方式
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
// 设置value的序列化方式
Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setValueSerializer(jsonSerializer);
redisTemplate.setHashValueSerializer(jsonSerializer);
return redisTemplate;
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Redis集群 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
使用如下:
//直接注入即可使用
@Autowired
private RedisTemplate<String, Object> redisSingleNodeTemplate;
@Autowired
private RedisTemplate<String, Object> redisClusterTemplate;