前言
redis 默认有 0-16 号数据库,一般我们操作redis时,用的是 0号数据库,但是有时我们的项目想同时操作多个数据库,又不想每次访问其它库数据时 select 切换数据库,这样太繁琐。
因此我们需要配置多个Jedis Client,但是jedis是容易阻塞,效率不太好,所以我这边采用了 Lettuce Client,它是 Reactive 的连接方式,效率比较高。但是怎么使用到 Lettuce Client,其实一般我们添加 spring-boot-starter-data-redis 依赖,通过RedisTemplate 去使用Redis的功能时,当版本很高的时候默认RedisTemplate底层用的就是通过Lettuce Client 去建立连接和操作数据。
以下是自己实际正在使用的多数据源配置,且能正常运行。
一、添加pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
二、多数据源的配置和添加到spring容器中
下面我的截图使用了四个数据源,分别是1,2,3,4号库。
1)新建一个configuration 配置类
2)new RedisStandaloneConfiguration(host, port); 初始化一个Redis配置,并接下来选择好库号。
3)初始化一个 LettuceConnectionFactory 。
4)实例化一个 RedisTemplate ,并设置键值序列化的方式,这里key和value都是字符串的,所以序列化器选择 StringRedisSerializer。
5)给 RedisTemplate 设置第三步创建的 LettuceConnectionFactory,并以@Bean注解注入到spring容器中,使用时直接通过方法名字在spring容器中查找,装配到引用它的实例中。
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
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.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
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.serializer.StringRedisSerializer;
import org.springframework.util.ObjectUtils;
import java.time.Duration;
@Configuration
public class RedisDatasourceConfiguration {
@Value("${redis.isCleanRedisCache:false}")
private String cleanRedisCache;
@Value("${redis.host:127.0.0.1}")
public String host;
@Value("${redis.port:6379}")
public Integer port;
private String password;
@Value("${redis.timeout:2000}")
public Integer timeout;
public Integer maxIdle = 16;
public Integer minIdle = 5;
public Integer maxTotal = 30;
@Bean
public RedisTemplate<String, String> stringRedisTemplate1() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
configuration.setDatabase(1);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
return createRedisTemplate(creatFactory(configuration));
}
@Bean
public RedisTemplate<String, String> stringRedisTemplate2() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
configuration.setDatabase(2);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
return createRedisTemplate(creatFactory(configuration));
}
@Bean
public RedisTemplate<String, String> stringRedisTemplate3() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
configuration.setDatabase(3);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
return createRedisTemplate(creatFactory(configuration));
}
@Bean
public RedisTemplate<String, String> stringRedisTemplate4() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
configuration.setDatabase(4);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
return createRedisTemplate(creatFactory(configuration));
}
@Bean
public RedisTemplate<String, String> stringRedisTemplate5() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
configuration.setDatabase(5);
if (!ObjectUtils.isEmpty(password)) {
RedisPassword redisPassword = RedisPassword.of(password);
configuration.setPassword(redisPassword);
}
return createRedisTemplate(creatFactory(configuration));
}
private RedisTemplate<String, String> getSerializerRedisTemplate(){
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
private RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = getSerializerRedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
private GenericObjectPoolConfig getGenericObjectPoolConfig(){
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
genericObjectPoolConfig.setMaxTotal(maxTotal);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMaxWaitMillis(timeout);
return genericObjectPoolConfig;
}
private LettuceConnectionFactory creatFactory(RedisStandaloneConfiguration configuration){
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
builder.poolConfig(getGenericObjectPoolConfig());
// LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder();
// builder.clientResources(clientResources());
// builder.commandTimeout(Duration.ofSeconds(3000));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build());
connectionFactory.afterPropertiesSet();
return connectionFactory;
}
}
三、使用方式
这里引用是2号库,即上面通过@Bean 加载到spring容器中的。
结语:
通过看源码知道怎么配置多数据源,积累了一些经验。
到此这篇关于Redis 操作多个数据库的配置的方法实现的文章就介绍到这了,更多相关Redis 操作多数据库内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!