SpringBoot 項目使用 redis
1、 直接引入 spring-boot-starter-data-redis 依賴
gradle引入依賴
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
2、在application.yml 增加redis配置
spring:
redis:
host: 10.111.11.111
port: 6379
database:
1
3、直接在Srevice層注入redisTemplate即可使用
@Autowired
RedisTemplate<String,String> redisTemplate;
上面這種是基礎(chǔ)的用法,但如果我的數(shù)據(jù)存放在不同的redis庫中呢?上面的方式顯然只能從數(shù)據(jù)庫1 中取數(shù)據(jù)。
如果配置多個數(shù)據(jù)庫需要增加Redis配置
1、修改application.yml配置文件
spring:
redis:
host: 10.111.111.111
port: 6379
database:
db5: 5
db6: 6
db7: 7
timeout: 3000
pool:
max-active: 100
max-idle: 3
min-idle: 0
max-wait: -1
password:
2、增加配置類 RedisConfig
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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.time.Duration;
@Configuration
public class RedisConfig {
@Value("${spring.redis.database.db5}")
private int db5;
@Value("${spring.redis.database.db6}")
private int db6;
@Value("${spring.redis.database.db7}")
private int db7;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWait;
@Bean
public GenericObjectPoolConfig getPoolConfig(){
// 配置redis連接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(maxWait);
return poolConfig;
}
@Bean(name = "redisTemplate7")
public StringRedisTemplate getRedisTemplate7(){
return getStringRedisTemplate(db7);
}
@Bean(name = "redisTemplate5")
public StringRedisTemplate getRedisTemplate5(){
return getStringRedisTemplate(db5);
}
@Bean(name = "redisTemplate6")
public StringRedisTemplate getRedisTemplate6(){
return getStringRedisTemplate(db6);
}
private StringRedisTemplate getStringRedisTemplate(int database) {
// 構(gòu)建工廠對象
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
config.setHostName(host);
config.setPort(port);
//config.setPassword(RedisPassword.of(password));
LettucePoolingClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
.commandTimeout(Duration.ofSeconds(timeout))
.poolConfig(getPoolConfig())
.build();
LettuceConnectionFactory factory = new LettuceConnectionFactory(config, clientConfig);
// 設(shè)置使用的redis數(shù)據(jù)庫
factory.setDatabase(database);
// 重新初始化工廠
factory.afterPropertiesSet();
return new StringRedisTemplate(factory);
}
}
3、在配置過程中遇到的問題
關(guān)于RedisConfig配置類中的 GenericObjectPoolConfig類找不到的錯誤,這個類需要單獨引入依賴
如果項目中有這個依賴的可以忽略
implementation 'org.apache.commons:commons-pool2'
第二個問題就是,配置好以后啟動提示依賴注入的時候RedisTemplate 找到多個實現(xiàn)類,啟動報錯
Description:
Field redisTemplate in net.cnki.ds.management.userManage.service.impl.DownloadAuthorityServiceImpl required a single bean, but 3 were found:
- redisTemplate7: defined by method 'getRedisTemplate7' in class path resource [net/cnki/ds/management/userManage/config/RedisConfig.class]
- redisTemplate5: defined by method 'getRedisTemplate5' in class path resource [net/cnki/ds/management/userManage/config/RedisConfig.class]
- redisTemplate6: defined by method 'getRedisTemplate6' in class path resource [net/cnki/ds/management/userManage/config/RedisConfig.class]
Action:
這個問題的原因是使用@Autowired 進(jìn)行自動注入的時候,他會去容器中查詢這個類型的Bean,如果查到一個會直接裝載,但是在上面配置中,雖然給Bean指定了name,但是自動注入的時候并沒有指定要注入哪個,所以會查出來三個,發(fā)生上面報錯。
解決方法:可以使用@Primary 注解,標(biāo)識一下查找到多個庫時,選擇一個主要的進(jìn)行注入。

image.png
但這樣一直注入的都會是數(shù)據(jù)庫7 顯然也不滿足需求。
所以還需要在@Autowired 上聯(lián)合 使用@Qualifier 注解
@Autowired
@Qualifier("redisTemplate7")
RedisTemplate<String,String> redisTemplate;
@Qualifier注解就是 標(biāo)識我們需要的具體是哪個實現(xiàn), @Qualifier 里面的name 要和 容器里的bean name 一致。但是需要注意的是如果指定的bean name不存在,啟動是會報錯的,加了@Primary注解也是沒用的。