Redis 操作多個數(shù)據(jù)庫的配置的方法實現(xiàn)
前言
redis 默認(rèn)有 0-16 號數(shù)據(jù)庫,一般我們操作redis時,用的是 0號數(shù)據(jù)庫,但是有時我們的項目想同時操作多個數(shù)據(jù)庫,又不想每次訪問其它庫數(shù)據(jù)時 select 切換數(shù)據(jù)庫,這樣太繁瑣。
因此我們需要配置多個Jedis Client,但是jedis是容易阻塞,效率不太好,所以我這邊采用了 Lettuce Client,它是 Reactive 的連接方式,效率比較高。但是怎么使用到 Lettuce Client,其實一般我們添加 spring-boot-starter-data-redis 依賴,通過RedisTemplate 去使用Redis的功能時,當(dāng)版本很高的時候默認(rèn)RedisTemplate底層用的就是通過Lettuce Client 去建立連接和操作數(shù)據(jù)。
以下是自己實際正在使用的多數(shù)據(jù)源配置,且能正常運行。
一、添加pom 依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.5.RELEASE</version> </dependency>
二、多數(shù)據(jù)源的配置和添加到spring容器中
下面我的截圖使用了四個數(shù)據(jù)源,分別是1,2,3,4號庫。
1)新建一個configuration 配置類
2)new RedisStandaloneConfiguration(host, port); 初始化一個Redis配置,并接下來選擇好庫號。
3)初始化一個 LettuceConnectionFactory 。
4)實例化一個 RedisTemplate ,并設(shè)置鍵值序列化的方式,這里key和value都是字符串的,所以序列化器選擇 StringRedisSerializer。
5)給 RedisTemplate 設(shè)置第三步創(chuàng)建的 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容器中的。
結(jié)語:
通過看源碼知道怎么配置多數(shù)據(jù)源,積累了一些經(jīng)驗。
到此這篇關(guān)于Redis 操作多個數(shù)據(jù)庫的配置的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)Redis 操作多數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows操作系統(tǒng)下Redis服務(wù)安裝圖文教程
這篇文章主要介紹了Windows操作系統(tǒng)下Redis服務(wù)安裝圖文教程,文中給大家提供了redis的下載地址,安裝程序步驟,需要的朋友可以參考下2018-03-03Redis分布式鎖的實現(xiàn)方式(redis面試題)
這篇文章主要介紹了Redis分布式鎖的實現(xiàn)方式(面試常見),需要的朋友可以參考下2020-01-01redis中隊列消息實現(xiàn)應(yīng)用解耦的方法
這篇文章主要給大家介紹了關(guān)于redis中隊列消息實現(xiàn)應(yīng)用解耦的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09