Redis数据源配置
mop文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
单数据源配置
-
yml文件
spring: redis: db1: database: 162 #redis缓存数据库下标 hostName: 127.0.0.1 #redis缓存地址 port: 6379 #redis缓存端口 password: root #reids登录密码 lettuce: pool: max-active: 8 #连接池最大连接数(使用负值表示没有限制) 默认 8 max-idle: 8 #连接池中的最大空闲连接 默认 8 max-wait: -1 #连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 min-idle: 0 #连接池中的最小空闲连接 默认 0
- javaBean配置
/** * @author : fzz */ @Configuration public class RedisConfig { private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); template.afterPropertiesSet(); return template; } private RedisTemplate<Serializable, Object> getObjectRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig(); } @Bean @ConfigurationProperties(prefix = "spring.redis.db1") public RedisStandaloneConfiguration commentRedisConfig() { return new RedisStandaloneConfiguration(); } @Bean("db1Factory") public LettuceConnectionFactory commentFactory(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(commentRedisConfig, clientConfiguration); } @Bean("db1StringRedisTemplate") public StringRedisTemplate countryStringRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) { return getStringRedisTemplate(factory); } @Bean("db1ObjectRedisTemplate") public RedisTemplate<Serializable, Object> countryObjectRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) { return getObjectRedisTemplate(factory); } }
多数据源
-
yml文件
spring: redis: db1: database: 162 #redis缓存数据库下标 hostName: 127.0.0.1 #redis缓存地址 port: 6379 #redis缓存端口 password: root #reids登录密码 db2: database: 162 #redis缓存数据库下标 hostName: 127.0.0.1 #redis缓存地址 port: 6379 #redis缓存端口 password: root #reids登录密码 lettuce: pool: max-active: 8 #连接池最大连接数(使用负值表示没有限制) 默认 8 max-idle: 8 #连接池中的最大空闲连接 默认 8 max-wait: -1 #连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 min-idle: 0 #连接池中的最小空闲连接 默认 0
- javaBean配置
/** * @author : fzz */ @Configuration public class RedisConfig { private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); template.afterPropertiesSet(); return template; } private RedisTemplate<Serializable, Object> getObjectRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") public GenericObjectPoolConfig redisPool() { return new GenericObjectPoolConfig(); } @Bean @ConfigurationProperties(prefix = "spring.redis.db1") public RedisStandaloneConfiguration commentRedisConfig() { return new RedisStandaloneConfiguration(); } @Bean("db1Factory") @Primary //多个相同的bean时,优先选择带有该注解的bean public LettuceConnectionFactory commentFactory(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(commentRedisConfig, clientConfiguration); } @Bean("db1StringRedisTemplate") @Primary public StringRedisTemplate countryStringRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) { return getStringRedisTemplate(factory); } @Bean("db1ObjectRedisTemplate") @Primary public RedisTemplate<Serializable, Object> countryObjectRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) { return getObjectRedisTemplate(factory); } @Bean @ConfigurationProperties(prefix = "spring.redis.db2") public RedisStandaloneConfiguration commentRedisConfig2() { return new RedisStandaloneConfiguration(); } @Bean("db2Factory") public LettuceConnectionFactory commentFactory2(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig2) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(commentRedisConfig2, clientConfiguration); } @Bean("db2StringRedisTemplate") public StringRedisTemplate countryStringRedisTemplate2(@Qualifier("db2Factory") RedisConnectionFactory factory) { return getStringRedisTemplate(factory); } @Bean("db2ObjectRedisTemplate") public RedisTemplate<Serializable, Object> countryObjectRedisTemplate2(@Qualifier("db2Factory") RedisConnectionFactory factory) { return getObjectRedisTemplate(factory); } }
使用
@Autowired //使用带有@Primary模板
protected StringRedisTemplate stringRedisTemplate;
@Autowired
protected RedisTemplate<Serializable, Object> objectRedisTemplate;
@Autowired
@Qualifier("db2StringRedisTemplate")
protected StringRedisTemplate stringRedisTemplate2;
@Autowired
@Qualifier("db2ObjectRedisTemplate") //指定使用哪个
protected RedisTemplate<Serializable, Object> objectRedisTemplate2;