您好,登錄后才能下訂單哦!
這篇文章主要介紹“Redis怎么配置多個數據庫”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Redis怎么配置多個數據庫”文章能幫助大家解決問題。
redis 默認有 0-16 號數據庫,一般我們操作redis時,用的是 0號數據庫,但是有時我們的項目想同時操作多個數據庫,又不想每次訪問其它庫數據時 select 切換數據庫,這樣太繁瑣。
因此我們需要配置多個Jedis Client,但是jedis是容易阻塞,效率不太好,所以我這邊采用了 Lettuce Client,它是 Reactive 的連接方式,效率比較高。但是怎么使用到 Lettuce Client,其實一般我們添加 spring-boot-starter-data-redis 依賴,通過RedisTemplate 去使用Redis的功能時,當版本很高的時候默認RedisTemplate底層用的就是通過Lettuce Client 去建立連接和操作數據。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.5.RELEASE</version> </dependency>
下面我的截圖使用了四個數據源,分別是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; /** * reactive redis * @Author:wangqipeng * @Date:14:38 2019-07-03 */ @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怎么配置多個數據庫”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。