background
Sometimes in a project, due to business problems, it may not be enough to operate only one Redis data source. For example, for an operating system, it can only add one data source to solve the problem, just like MySQL’s multi-data source.
Configuration of the sample
The connection pool is lettuce.
redis:
host: localhost
port: 6379
password: 123456
timeout: 60000
database: 10
lettuce:
pool:
min-idle: 0
max-idle: 10
max-wait: - 1
max-active: 200
time-between-eviction-runs: - 1
redis-live:
host: localhost
port: 6379
password: 123456
database: 0
Copy the code
The implementation code
Jackson is used for serialization.
Creating a RedisTemplate and StringRedisTemplate are provided below.
On the difference between the two:
-
The relationship between the two is that StringRedisTemplate inherits RedisTemplate.
-
The data are not shared; So a StringRedisTemplate can only manage data in a StringRedisTemplate, and a RedisTemplate can only manage data in a RedisTemplate.
-
There are two default serialization strategies used by SDR, one is the serialization strategy of String and the other is the serialization strategy of JDK.
StringRedisTemplate defaults to String serialization, which serializes keys and values. The RedisTemplate uses the JDK’s serialization strategy by default, which is used to serialize saved keys and values.
/** * <p> * RedisLettuce instance configuration * </p> **@author zhengshangjin
* @version 1.0.0
* @since 1.0.0
* created on 2020-04-23
*/
@Configuration
@EnableCaching
public class RedisLettuceConfig {
/** * live data source */
@Value("${spring.redis-live.host}")
private String redisLiveHost;
@Value("${spring.redis-live.port}")
private int redisLivePort;
@Value("${spring.redis-live.password}")
private String redisLivePass;
@Value("${spring.redis-live.database}")
private int redisLiveDb;
/** * Public configuration */
@Value("${spring.redis.timeout}")
private long timeout;
@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;
@Value("${spring.redis.lettuce.pool.max-wait}")
private int maxWait;
/** * Assemble the RedisTemplate * <p> * here the assembly instance is configured according to the default connection * </> **@paramThe default * redisConnectionFactory@return redisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
return createRedisTemplate(redisConnectionFactory);
}
/** * Assemble StringRedisTemplate * <p> * here the assembly instance is configured according to the default connection * </> *@paramThe default * redisConnectionFactory@return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "stringRedisTemplate")
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
return createStringRedisTemplate(redisConnectionFactory);
}
/** * Assemble Live data source **@return liveStringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
@Bean(name = "liveStringRedisTemplate")
public StringRedisTemplate liveStringRedisTemplate(a) {
return createStringRedisTemplate(redisLiveHost, redisLivePort, redisLivePass, redisLiveDb);
}
/** * Create RedisTemplate **@param redisConnectionFactory redisConnectionFactory
* @return RedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public RedisTemplate<Object, Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
ObjectMapper objectMapper = newObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); Jackson2JsonRedisSerializer<? > serializer =new Jackson2JsonRedisSerializer<>(Object.class);
serializer.setObjectMapper(objectMapper);
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/** * Create StringRedisTemplate **@param redisConnectionFactory redisConnectionFactory
* @return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public StringRedisTemplate createStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
return stringRedisTemplate;
}
/** * Create StringRedisTemplate **@paramThe host host *@paramThe port port *@param"Password," password *@paramThe database library *@return StringRedisTemplate
* @author zhengshangjin
* created on 2020-04-23
*/
public StringRedisTemplate createStringRedisTemplate(String host, int port, String password, int database) {
// Basic configuration
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(port);
configuration.setDatabase(database);
if (ObjectUtils.isNotEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
// Common connection pool configurationGenericObjectPoolConfig<? > genericObjectPoolConfig =new GenericObjectPoolConfig<>();
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
// Lettuce Pool
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
builder.poolConfig(genericObjectPoolConfig);
builder.commandTimeout(Duration.ofSeconds(timeout));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
connectionFactory.afterPropertiesSet();
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(connectionFactory);
returnstringRedisTemplate; }}Copy the code
Injection use
Beanname as specified by @qualifier
@Autowired
@Qualifier("liveStringRedisTemplate")
public StringRedisTemplate stringRedisTemplate;
Copy the code