Spring Boot自2.0版本开始默认使用Lettuce作为Redis的客户端,多RedisTemplate配置方法如下:
先对lettuce连接池配置,基于lettuce配置多数据源的连接工厂,使用@Primary 标注为首选的bean,以及指定并名称,再基于连接工厂配置多数据源的redisTemplate。
点击查看代码
package com.hnltd.auth.config;
/**
* @author zhuo
* @date 2023-07-25 16:05:00
* <Description> 多RedisTemplate配置
*/
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisRepositoryConfig {
/**
* 配置lettuce连接池
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
public GenericObjectPoolConfig<?> redisPool() {
return new GenericObjectPoolConfig<>();
}
/**
* 配置第一个数据源的连接工厂
* 这里注意:需要添加@Primary 指定bean的名称,目的是为了创建两个不同名称的LettuceConnectionFactory
*
* @return
*/
@Bean("tokenFactory")
@Primary
public LettuceConnectionFactory tokenFactory(@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.password}") String password,
GenericObjectPoolConfig<?> config) {
RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
redisDataConfig.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
return new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
}
//第二个数据源的连接工厂
@Bean("oauthFactory")
public LettuceConnectionFactory oauthFactory(@Value("${oauth.redis.host}") String host,
@Value("${oauth.redis.port}") int port,
@Value("${oauth.redis.password}") String password,
GenericObjectPoolConfig<?> config) {
RedisStandaloneConfiguration redisDataConfig = new RedisStandaloneConfiguration(host, port);
redisDataConfig.setPassword(RedisPassword.of(password));
LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
return new LettuceConnectionFactory(redisDataConfig, clientConfiguration);
}
/**
* 配置第一个数据源的RedisTemplate
* 注意:这里指定使用名称=factory 的 RedisConnectionFactory
* 并且标识第一个数据源是默认数据源 @Primary
*
* @param dataFactory
* @return
*/
@Bean("tokenTemplate")
@Primary
public RedisTemplate<String, Object> redisDataTemplate(@Qualifier("tokenFactory") RedisConnectionFactory dataFactory) {
return getRedisDataTemplate(dataFactory);
}
//第二个数据源
@Bean("oauthTemplate")
public RedisTemplate<String, Object> redisConcurrencyTemplate(@Qualifier("oauthFactory") RedisConnectionFactory concurrencyFactory) {
return getRedisDataTemplate(concurrencyFactory);
}
/**
* 设置序列化方式
*
* @return
*/
private RedisTemplate<String, Object> getRedisDataTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}