package com.ks.app.config;
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
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;
|
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPoolConfig;
|
|
import java.time.Duration;
|
|
|
@Configuration
|
public class RedisConfig extends CachingConfigurerSupport {
|
|
@Value("${spring.redis.host}")
|
private String addr;
|
@Value("${spring.redis.port}")
|
private int port;
|
@Value("${spring.redis.timeout}")
|
private int timeout;
|
@Value("${spring.redis.password}")
|
private String auth;
|
@Value("${spring.redis.database}")
|
private int database;
|
@Value("${spring.redis.lettuce.pool.max-active}")
|
private int maxActive;
|
@Value("${spring.redis.lettuce.pool.max-wait}")
|
private int maxWait;
|
@Value("${spring.redis.lettuce.pool.max-idle}")
|
private int maxIdle;
|
@Value("${spring.redis.lettuce.pool.min-idle}")
|
private int minIdle;
|
@Value("${spring.redis.lettuce.shutdown-timeout}")
|
private int shutDownTimeout;
|
|
|
@Bean
|
public LettuceConnectionFactory lettuceConnectionFactory() {
|
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
|
genericObjectPoolConfig.setMaxIdle(maxIdle);
|
genericObjectPoolConfig.setMinIdle(minIdle);
|
genericObjectPoolConfig.setMaxTotal(maxActive);
|
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
|
genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(1000);
|
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
redisStandaloneConfiguration.setDatabase(database);
|
redisStandaloneConfiguration.setHostName(addr);
|
redisStandaloneConfiguration.setPort(port);
|
redisStandaloneConfiguration.setPassword(RedisPassword.of(auth));
|
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
|
.commandTimeout(Duration.ofMillis(timeout))
|
.shutdownTimeout(Duration.ofMillis(shutDownTimeout))
|
.poolConfig(genericObjectPoolConfig)
|
.build();
|
|
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
|
//允许多个连接公用一个物理连接,默认为true
|
// factory.setShareNativeConnection(true);
|
// 检验链接是否可用,默认为false
|
factory.setValidateConnection(true);
|
return factory;
|
}
|
|
@Bean
|
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
template.setConnectionFactory(lettuceConnectionFactory);
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
ObjectMapper om = new ObjectMapper();
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
|
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
// key采用String的序列化方式
|
template.setKeySerializer(stringRedisSerializer);
|
// hash的key也采用String的序列化方式
|
template.setHashKeySerializer(stringRedisSerializer);
|
// value序列化方式采用jackson
|
template.setValueSerializer(jackson2JsonRedisSerializer);
|
// hash的value序列化方式采用jackson
|
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
template.afterPropertiesSet();
|
return template;
|
}
|
|
|
@Bean
|
public JedisPool jedisPool() {
|
JedisPoolConfig poolConfig = new JedisPoolConfig();
|
poolConfig.setMaxTotal(maxActive);
|
poolConfig.setMaxIdle(maxIdle);
|
poolConfig.setTestOnBorrow(true);
|
return new JedisPool(poolConfig, addr, port, timeout, auth, database);
|
}
|
|
|
}
|