背景
其实程序里配置多数据源的需求还是挺多的,我们有一个项目是双数据源的,但是看起来,代码乱糟糟的,所以查了查,找了一种配置看起来清爽一点的,下面上代码
代码
代码分两部分,yml配置和config代码配置
yml
spring: redis: database: 0 host: xxxx.xxx.aliyuncs.com port: 6379 password: password lettuce: pool: max-active: 100 max-idle: 10 max-wait: 3000 min-idle: 10 timeout: 3000 redis2: database: 0 host: xxxx.xxx2.aliyuncs.com port: 6379 password: password2
代码配置
package com.airdoc.auth.config; import org.apache.commons.lang3.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; 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.core.StringRedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /** * @author author * @date 2022/7/27 * @apiNote */ @EnableCaching @Configuration public class MultiRedisConfig { @Value("${spring.redis.lettuce.pool.max-idle}") int maxIdle; @Value("${spring.redis.lettuce.pool.max-active}") int maxActive; @Value("${spring.redis.lettuce.pool.max-wait}") long maxWaitMillis; @Value("${spring.redis.lettuce.pool.min-idle}") int minIdle; @Value("${spring.redis.timeout}") int timeout; @Bean(name = "redisTemplate") public <T> RedisTemplate<String, T> redisTemplate(@Value("${spring.redis.database}") int database, @Value("${spring.redis.host}") String hostName, @Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password) { RedisTemplate<String, T> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory(database, hostName, port, password)); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } @Bean(name = "gbRedisTemplate") public StringRedisTemplate gbRedisTemplate(@Value("${spring.redis2.database}") int database, @Value("${spring.redis2.host}") String hostName, @Value("${spring.redis2.port}") int port, @Value("${spring.redis2.password}") String password) { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory(database, hostName, port, password)); return temple; } /** * 使用lettuce配置Redis连接信息 * * @param database Redis数据库编号 * @param hostName 服务器地址 * @param port 端口 * @param password 密码 * @return RedisConnectionFactory */ public RedisConnectionFactory connectionFactory(int database, String hostName, int port, String password) { RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(hostName); configuration.setPort(port); if (StringUtils.isNotBlank(password)) { configuration.setPassword(password); } if (database != 0) { configuration.setDatabase(database); } GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); genericObjectPoolConfig.setMaxIdle(maxIdle); genericObjectPoolConfig.setMinIdle(minIdle); genericObjectPoolConfig.setMaxTotal(maxActive); genericObjectPoolConfig.setMaxWaitMillis(maxWaitMillis); LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofMillis(timeout)) .poolConfig(genericObjectPoolConfig) .build(); LettuceConnectionFactory lettuce = new LettuceConnectionFactory(configuration, clientConfig); lettuce.afterPropertiesSet(); return lettuce; } }
总结
其实实现方式还是挺多的,还有一种我看也挺好玩,有兴趣可以看一下,指路 :Redis多数据源