背景
有些时候在一个项目里,由于业务问题,可能仅仅操作一个Redis数据源已经不能满足,比如某个运营系统,对接着多个不同的服务,处理数据时又不想通过远程调用,那只能增加一个数据源来解决问题,像MySQL的多数据源一样。
yml配置示例
这里连接池选用的是lettuce。
1 redis: 2 host: localhost 3 port: 6379 4 password: 123456 5 timeout: 60000 6 database: 10 7 lettuce: 8 pool: 9 min-idle: 0 10 max-idle: 10 11 max-wait: -1 12 max-active: 200 13 time-between-eviction-runs: -1 14 redis-live: 15 host: localhost 16 port: 6379 17 password: 123456 18 database: 0
实现代码
关于序列化使用的是jackson。
以下提供了创建RedisTemplate以及StringRedisTemplate。
关于二者的区别:
两者的关系是StringRedisTemplate继承RedisTemplate。
两者的数据是不共通的;也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。
SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。
1 /** 2 * <p> 3 * RedisLettuce 连接实例配置 4 * </p> 5 * 6 * @author zhengshangjin 7 * @version 1.0.0 8 * @since 1.0.0 9 * created on 2020-04-23 10 */ 11 @Configuration 12 @EnableCaching 13 public class RedisLettuceConfig { 14 15 /** 16 * live数据源 17 */ 18 @Value("${spring.redis-live.host}") 19 private String redisLiveHost; 20 21 @Value("${spring.redis-live.port}") 22 private int redisLivePort; 23 24 @Value("${spring.redis-live.password}") 25 private String redisLivePass; 26 27 @Value("${spring.redis-live.database}") 28 private int redisLiveDb; 29 30 /** 31 * 公共配置 32 */ 33 @Value("${spring.redis.timeout}") 34 private long timeout; 35 36 @Value("${spring.redis.lettuce.pool.min-idle}") 37 private int minIdle; 38 39 @Value("${spring.redis.lettuce.pool.max-idle}") 40 private int maxIdle; 41 42 @Value("${spring.redis.lettuce.pool.max-active}") 43 private int maxActive; 44 45 @Value("${spring.redis.lettuce.pool.max-wait}") 46 private int maxWait; 47 48 /** 49 * 装配 RedisTemplate 50 * <p> 51 * 这里根据默认连接配置 装配实例 52 * </> 53 * 54 * @param redisConnectionFactory 默认 55 * @return redisTemplate 56 * @author zhengshangjin 57 * created on 2020-04-23 58 */ 59 @Bean(name = "redisTemplate") 60 public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { 61 return createRedisTemplate(redisConnectionFactory); 62 } 63 64 /** 65 * 装配 StringRedisTemplate 66 * <p> 67 * 这里根据默认连接配置 装配实例 68 * </> 69 * @param redisConnectionFactory 默认 70 * @return StringRedisTemplate 71 * @author zhengshangjin 72 * created on 2020-04-23 73 */ 74 @Bean(name = "stringRedisTemplate") 75 public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { 76 return createStringRedisTemplate(redisConnectionFactory); 77 } 78 79 /** 80 * 装配 Live数据源 81 * 82 * @return liveStringRedisTemplate 83 * @author zhengshangjin 84 * created on 2020-04-23 85 */ 86 @Bean(name = "liveStringRedisTemplate") 87 public StringRedisTemplate liveStringRedisTemplate() { 88 return createStringRedisTemplate(redisLiveHost, redisLivePort, redisLivePass, redisLiveDb); 89 } 90 91 /** 92 * 创建 RedisTemplate 93 * 94 * @param redisConnectionFactory redisConnectionFactory 95 * @return RedisTemplate 96 * @author zhengshangjin 97 * created on 2020-04-23 98 */ 99 public RedisTemplate<Object, Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { 100 ObjectMapper objectMapper = new ObjectMapper(); 101 objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 102 objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); 103 104 Jackson2JsonRedisSerializer<?> serializer = new Jackson2JsonRedisSerializer<>(Object.class); 105 serializer.setObjectMapper(objectMapper); 106 107 RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); 108 redisTemplate.setConnectionFactory(redisConnectionFactory); 109 redisTemplate.setKeySerializer(new StringRedisSerializer()); 110 redisTemplate.setValueSerializer(serializer); 111 redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 112 redisTemplate.setHashValueSerializer(serializer); 113 redisTemplate.afterPropertiesSet(); 114 return redisTemplate; 115 } 116 117 /** 118 * 创建 StringRedisTemplate 119 * 120 * @param redisConnectionFactory redisConnectionFactory 121 * @return StringRedisTemplate 122 * @author zhengshangjin 123 * created on 2020-04-23 124 */ 125 public StringRedisTemplate createStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { 126 StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); 127 stringRedisTemplate.setConnectionFactory(redisConnectionFactory); 128 return stringRedisTemplate; 129 } 130 131 /** 132 * 创建 StringRedisTemplate 133 * 134 * @param host 主机 135 * @param port 端口 136 * @param password 密码 137 * @param database 库 138 * @return StringRedisTemplate 139 * @author zhengshangjin 140 * created on 2020-04-23 141 */ 142 public StringRedisTemplate createStringRedisTemplate(String host, int port, String password, int database) { 143 // 基本配置 144 RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); 145 configuration.setHostName(host); 146 configuration.setPort(port); 147 configuration.setDatabase(database); 148 if (ObjectUtils.isNotEmpty(password)) { 149 RedisPassword redisPassword = RedisPassword.of(password); 150 configuration.setPassword(redisPassword); 151 } 152 153 // 连接池通用配置 154 GenericObjectPoolConfig<?> genericObjectPoolConfig = new GenericObjectPoolConfig<>(); 155 genericObjectPoolConfig.setMaxTotal(maxActive); 156 genericObjectPoolConfig.setMinIdle(minIdle); 157 genericObjectPoolConfig.setMaxIdle(maxIdle); 158 genericObjectPoolConfig.setMaxWaitMillis(maxWait); 159 160 // Lettuce Pool 161 LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); 162 builder.poolConfig(genericObjectPoolConfig); 163 builder.commandTimeout(Duration.ofSeconds(timeout)); 164 LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); 165 connectionFactory.afterPropertiesSet(); 166 167 StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); 168 stringRedisTemplate.setConnectionFactory(connectionFactory); 169 return stringRedisTemplate; 170 } 171 172 }
注入使用
根据@Qualifier指定beanname来获取
@Autowired @Qualifier("liveStringRedisTemplate") public StringRedisTemplate stringRedisTemplate;