问题描述
最近遇到一个 Redis 多数据源切换问题,不过我这个没有那么动态切换需求,所以就写了一种比较硬编码的方式来做『切换』
其实大概的场景是这样的:不同的开发环境调用 db0、生产环境调用 db1,但是因为业务原因,当中有一部分的业务需要共享数据(中心数据),所以想把 db15 当作他们的共享数据库。
原因分析
思路很简单,只要生产 2 个 RedisTemplate 即可:
- 调用默认的 db RedisTemplate
- 调用共享的 db RedisTemplate
解决方案
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
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.GenericToStringSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.time.Duration;/*** @author Lux Sun* @date 2020/5/9*/
@Slf4j
@Configuration
public class RedisConfig {@Resourceprivate JedisConnectionFactory jedisConnectionFactory;@Beanpublic RedisTemplate<String, Object> comnRedisObjectTemplate(@Value("${spring.redis.host}") String host,@Value("${spring.redis.port}") int port) {RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();redisConfig.setHostName(host);redisConfig.setPort(port);// 指定 database 15redisConfig.setDatabase(15);GenericObjectPoolConfig config = new GenericObjectPoolConfig();JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder().usePooling().build();JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisConfig, clientConfiguration);RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory);Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(serializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}@Beanpublic RedisTemplate<String, Object> redisObjectTemplate() {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(jedisConnectionFactory);Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(serializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}