// 连接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*/@Beanpublic 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*/@Beanpublic 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@Primarypublic 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;}@Beanpublic 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>
使用如下:
//直接注入即可使用@Autowiredprivate RedisTemplate<String, Object> redisSingleNodeTemplate;@Autowiredprivate RedisTemplate<String, Object> redisClusterTemplate;