本文介绍SpringBoot Redis的 基本使用 和 多Redis数据源配置
SpringBoot 2.x项目中引入 spring-boot-starter-data-redis
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
查看引入包,发现SpringBoot 2.x 使用的是: lettuce
。
配置 application.yml
spring: redis: host: 127.0.0.1 port: 6379 password: database: 0
使用:
@Autowired private RedisTemplate<String, String> redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
SpringBoot Redis的自动化配置位于: RedisAutoConfiguration
项目中常常用到不止一个Redis数据源,如果按照上面的简单配置,那么很难满足需要。需要我们自己来声明 ConnectionFactory
和 RedisTemplate
。
application.yml
中配置如下:
spring: redis: #redis配置 redis-a: host: 127.0.0.1 port: 6379 password: pwd@local ssl: true database: 0 redis-b: host: 127.0.0.1 port: 6479 password: pwd@local ssl: true database: 0
以上配置是在 spring.redis
下做区分两个数据源,这是为了能够方便地查找配置位置。
添加 RedisConfig.java
,如下:
@Configuration public class RedisConfig{ @Bean(name = "aRedisTemplate") public StringRedisTemplate aRedisTemplate( @Qualifier("aLettuceConnectionFactory") RedisConnectionFactory aLettuceConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(aLettuceConnectionFactory); return template; } @Bean(name = "bRedisTemplate") public StringRedisTemplate bRedisTemplate( @Qualifier("bConnectionFactory") RedisConnectionFactory bConnectionFactory) { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(bConnectionFactory); return template; } @Bean(name = "aLettuceConnectionFactory") @Primary public LettuceConnectionFactory aLettuceConnectionFactory(@Value("${spring.redis.redis-a.host}")String host, @Value("${spring.redis.redis-a.port}") int port, @Value("${spring.redis.redis-a.password}") String password, @Value("${spring.redis.redis-a.ssl}") boolean ssl, @Value("${spring.redis.redis-a.database}") int database) { LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); if (ssl) { builder.useSsl(); } RedisStandaloneConfiguration standaloneConfiguration = getRedisStandaloneConfiguration(host, port, password, database); return new LettuceConnectionFactory(standaloneConfiguration, builder.build()); } @Bean(name = "bConnectionFactory") public LettuceConnectionFactory bConnectionFactory(@Value("${spring.redis.redis-b.host}")String host, @Value("${spring.redis.redis-b.port}") int port, @Value("${spring.redis.redis-b.password}") String password, @Value("${spring.redis.redis-b.ssl}") boolean ssl, @Value("${spring.redis.redis-b.database}") int database) { LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); if (ssl) { builder.useSsl(); } RedisStandaloneConfiguration standaloneConfiguration = getRedisStandaloneConfiguration(host, port, password, database); return new LettuceConnectionFactory(standaloneConfiguration, builder.build()); } private RedisStandaloneConfiguration getRedisStandaloneConfiguration(String host,int port, String password, int database){ RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration(); standaloneConfiguration.setHostName(host); standaloneConfiguration.setPort(port); standaloneConfiguration.setPassword(password); standaloneConfiguration.setDatabase(database); return standaloneConfiguration; } }
使用:
@Autowired @Qualifier("aRedisTemplate") private StringRedisTemplate aRedisTemplate; @Autowired @Qualifier("bRedisTemplate") private StringRedisTemplate bRedisTemplate;
如果想要忽略掉默认的Redis配置,那么可以将 RedisAutoConfiguration
和 RedisReactiveAutoConfiguration
加入忽略:
@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class})